Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion actions/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,40 @@ def on_ready(self):
def get_config_rows(self):
self.message_row = Adw.EntryRow(
title=self.plugin_base.lm.get("actions.message.message"))
self.channel_row = Adw.EntryRow(
title=self.plugin_base.lm.get("actions.message.channel"))

self.message_row.connect("notify::text", self._on_message_change)
self.channel_row.connect("notify::text", self._on_channel_change)

self._load_config()

super_rows = super().get_config_rows()
super_rows.append(self.message_row)
super_rows.append(self.channel_row)
return super_rows

def _load_config(self):
settings = self.get_settings()
self.message_row.set_text(settings.get('message', ''))
self.channel_row.set_text(settings.get('channel', ''))

def _on_message_change(self, entry, _):
settings = self.get_settings()
settings['message'] = entry.get_text()
self.set_settings(settings)

def _on_channel_change(self, entry, _):
settings = self.get_settings()
settings['channel'] = entry.get_text()
self.set_settings(settings)

def on_key_down(self):
settings = self.get_settings()
message = settings.get('message', '')
channel = settings.get('channel', '')
try:
self.plugin_base.backend.send_message(message)
self.plugin_base.backend.send_message(message, channel)
except Exception as ex:
log.error(ex)
self.show_error(3)
1 change: 1 addition & 0 deletions locales/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"actions.base.credentials.authenticated": "Authenticated successfully",
"actions.base.credentials.failed": "Authentication failed",
"actions.message.message": "Message",
"actions.message.channel": "Channel",
"actions.chat_mode.mode_row.label": "Chat Mode",
"actions.info.link.label": "Checkout how to configure this plugin on",
"actions.info.link.text": "GitHub"
Expand Down
18 changes: 16 additions & 2 deletions twitch_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(self):
self.httpd: HTTPServer = None
self.httpd_thread: threading.Thread = None
self.auth_code: str = None
self.cached_channels: dict = {}

def set_token_path(self, path: str) -> None:
self.token_path = path
Expand All @@ -64,6 +65,18 @@ def on_disconnect(self, conn):
self.httpd.shutdown()
super().on_disconnect(conn)

def get_channel_id(self, user_name: str) -> str | None:
if user_name in self.cached_channels:
return self.cached_channels[user_name]

users = self.twitch.get_users(None, [user_name])
if users:
channel_id = users[0].user_id
self.cached_channels[user_name] = channel_id
return str(channel_id)

return None

def create_clip(self) -> None:
if not self.twitch:
return
Expand Down Expand Up @@ -107,11 +120,12 @@ def get_chat_settings(self) -> dict:
'slow_mode': current.slow_mode
}

def send_message(self, message: str) -> None:
def send_message(self, message: str, user_name: str) -> None:
if not self.twitch:
return
self.validate_auth()
self.twitch.send_chat_message(self.user_id, self.user_id, message)
channel_id = self.get_channel_id(user_name) or self.user_id
self.twitch.send_chat_message(channel_id, self.user_id, message)

def update_client_credentials(self, client_id: str, client_secret: str) -> None:
if None in (client_id, client_secret) or "" in (client_id, client_secret):
Expand Down