diff --git a/ovos_PHAL_plugin_discord_bot/__init__.py b/ovos_PHAL_plugin_discord_bot/__init__.py index e5668f2..55a3b6f 100644 --- a/ovos_PHAL_plugin_discord_bot/__init__.py +++ b/ovos_PHAL_plugin_discord_bot/__init__.py @@ -1,6 +1,7 @@ import requests import discord import json +import queue from discord.ext import commands, tasks from ovos_plugin_manager.phal import PHALPlugin from ovos_config.config import LocalConf @@ -19,6 +20,9 @@ class DiscordBotPlugin(PHALPlugin): super().__init__(bus, "ovos-PHAL-plugin-discord-bot", config) #self.web_config = LocalConf(get_webcache_location()) + # Initialize queue for Mycroft and user messages + self.message_queue = queue.Queue() + # Initialize Discord bot and intents bot_intents = discord.Intents.all() self.bot = commands.Bot(command_prefix='!', intents=bot_intents) @@ -36,7 +40,7 @@ class DiscordBotPlugin(PHALPlugin): # Discord event: bot is ready @self.bot.event async def on_ready(): - #myLoop.start() + myLoop.start() LOG.info(f"{self.bot.user} has connected to Discord!") admin = await get_discord_user_by_id(self.config.get("admin_id")) @@ -44,6 +48,19 @@ class DiscordBotPlugin(PHALPlugin): LOG.info(f"Admin is {admin.name}") await admin.send("I'm now online") + QUEUE_POLL_INTERVAL = 0.2 + @tasks.loop(seconds=QUEUE_POLL_INTERVAL) + async def myLoop(): + admin = await get_discord_user_by_id(self.config.get("admin_id")) + while not self.message_queue.empty(): + entity, message_content = self.message_queue.get() + print(f"{entity} said: {message_content}") + if entity == "user": + await admin.send(f"User said: {message_content}") + elif entity == "mycroft": + await admin.send(f"Mycroft said: {message_content}") + + self.setup_configuration() @classproperty @@ -60,9 +77,13 @@ class DiscordBotPlugin(PHALPlugin): def on_utterance(self, message=None): LOG.info(f"On utterance triggered: %s", message.serialize()) + for utterance in message.data['utterances']: + message_queue.put(("user", utterance)) def on_speak(self, message=None): LOG.info(f"On speak triggered: %s", message.serialize()) + message_queue.put(("mycroft", message.data['utterance'])) + def setup_configuration(self): first_start = self.config.get("first_start", True)