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
41 changes: 38 additions & 3 deletions DiscordActionBase.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from loguru import logger as log
from gi.repository import Gtk, Adw
import gi
import threading

from src.backend.PluginManager.ActionBase import ActionBase

Expand All @@ -14,8 +16,16 @@ def __init__(self, *args, **kwargs):
self.client_secret: str = ""

def get_config_rows(self) -> list:
authed = self.plugin_base.backend.is_authed()
if not authed:
label = "actions.base.credentials.no-credentials"
css_style = "discord-controller-red"
else:
label = "actions.base.credentials.authenticated"
css_style = "discord-controller-green"

self.status_label = Gtk.Label(
label=self.plugin_base.lm.get("actions.base.no-credentials"))
label=self.plugin_base.lm.get(label), css_classes=[css_style])
self.client_id_row = Adw.EntryRow(
title=self.plugin_base.lm.get("actions.base.client_id"), text=self.client_id)
self.client_secret_row = Adw.PasswordEntryRow(
Expand All @@ -35,6 +45,7 @@ def get_config_rows(self) -> list:
"actions.base.credentials.title"))
group.add(self.client_id_row)
group.add(self.client_secret_row)
group.add(self.status_label)
group.add(self.auth_button)

self.load_config()
Expand Down Expand Up @@ -67,5 +78,29 @@ def on_auth_clicked(self, _):
settings = self.plugin_base.get_settings()
client_id = settings.get('client_id')
client_secret = settings.get('client_secret')
self.plugin_base.backend.update_client_credentials(
client_id, client_secret)
self.auth_button.set_sensitive(False)
self.plugin_base.auth_callback_fn = self.on_auth_completed
threading.Thread(target=self.plugin_base.backend.update_client_credentials, daemon=True,
name="update_client_credentials", args=[client_id, client_secret]).start()

def _set_status(self, message: str, is_error: bool = False):
self.status_label.set_label(message)
if is_error:
self.status_label.remove_css_class("discord-controller-green")
self.status_label.add_css_class("discord-controller-red")
else:
self.status_label.remove_css_class("discord-controller-red")
self.status_label.add_css_class("discord-controller-green")

def on_auth_completed(self, success: bool, message: str = None):
self.auth_button.set_sensitive(True)
if success:
self._set_status(self.plugin_base.lm.get(
"actions.base.credentials.authenticated"), False)
else:
if self.plugin_base.lm.get(message):
message = self.plugin_base.lm.get(message)
elif not message:
message = self.plugin_base.lm.get(
"actions.base.credentials.failed")
self._set_status(message, True)
13 changes: 11 additions & 2 deletions backend.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import uuid
import json

from streamcontroller_plugin_tools import BackendBase
Expand All @@ -16,6 +15,7 @@ def __init__(self):
self.access_token: str = None
self.discord_client: AsyncDiscord = None
self.callbacks: dict = {}
self._is_authed: bool = False

def discord_callback(self, code, event):
if code == 0:
Expand All @@ -29,6 +29,8 @@ def discord_callback(self, code, event):
self.discord_client.authenticate(self.access_token)
self.frontend.save_access_token(self.access_token)
case commands.AUTHENTICATE:
self.frontend.on_auth_callback(True)
self._is_authed = True
for k in self.callbacks:
self.discord_client.subscribe(k)
case commands.DISPATCH:
Expand All @@ -45,21 +47,28 @@ def setup_client(self):
else:
self.discord_client.authenticate(self.access_token)
except Exception as ex:
self.frontend.on_auth_callback(False, str(ex))
log.error("failed to setup discord client: {0}", ex)

def update_client_credentials(self, client_id: str, client_secret: str, access_token: str = ""):
if None in (client_id, client_secret) or "" in (client_id, client_secret):
self.frontend.on_auth_callback(
False, "actions.base.credentials.missing_client_info")
return
self.client_id = client_id
self.client_secret = client_secret
self.access_token = access_token
self.setup_client()

def is_authed(self) -> bool:
return self._is_authed

def register_callback(self, key: str, callback: callable):
callbacks = self.callbacks.get(key, [])
callbacks.append(callback)
self.callbacks[key] = callbacks
self.discord_client.subscribe(key)
if self._is_authed:
self.discord_client.subscribe(key)

def set_mute(self, muted: bool) -> bool:
if self.discord_client is None or not self.discord_client.is_connected():
Expand Down
7 changes: 5 additions & 2 deletions locales/en_US.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"plugin.name": "Discord",
"actions.base.no-credentials": "Missing Credentials",
"actions.base.credentials.no-credentials": "Missing Credentials",
"actions.base.client_id": "Client ID",
"actions.base.client_secret": "Client Secret",
"actions.base.validate": "Validate",
"actions.base.credentials.title": "Discord Credentials",
"actions.info.link.label": "Checkout how to configure this plugin on",
"actions.info.link.text": "GitHub",
"actions.mute.choice.title": "Button Press Action",
"actions.mute.label_choice.title": "Display Location"
"actions.mute.label_choice.title": "Display Location",
"actions.base.credentials.authenticated": "Authenticated successfully",
"actions.base.credentials.failed": "Failed to authenticate",
"actions.base.credentials.missing_client_info": "Missing Client ID or Client Secret"
}
8 changes: 7 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def __init__(self):

self.callbacks = {}

self.auth_callback_fn: callable = None

self.lm = self.locale_manager
self.lm.set_to_os_default()

Expand All @@ -30,7 +32,7 @@ def __init__(self):
backend_path = os.path.join(self.PATH, 'backend.py')
self.launch_backend(backend_path=backend_path,
open_in_terminal=False, venv_path=os.path.join(self.PATH, '.venv'))
self.wait_for_backend(10)

self.backend.update_client_credentials(
client_id, client_secret, access_token)

Expand Down Expand Up @@ -88,3 +90,7 @@ def add_callback(self, key: str, callback: callable):
def handle_callback(self, key: str, data: any):
for callback in self.callbacks.get(key):
callback(data)

def on_auth_callback(self, success: bool, message: str = None):
if self.auth_callback_fn:
self.auth_callback_fn(success, message)
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "1.0.1",
"thumbnail": "store/thumbnail.jpg",
"version": "1.0.3",
"thumbnail": "store/thumbnail.png",
"id": "com_imdevinc_StreamControllerDiscordPlugin",
"name": "Discord",
"author": "ImDevinC",
Expand Down
Binary file removed store/thumbnail.jpg
Binary file not shown.
Binary file added store/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.discord-controller-green {
color: green;
}

.discord-controller-red {
color: red;
}
Loading