test message passing
This commit is contained in:
parent
d1691f7a73
commit
f2e412d5de
|
|
@ -1,6 +1,7 @@
|
||||||
import requests
|
import requests
|
||||||
import discord
|
import discord
|
||||||
import json
|
import json
|
||||||
|
import queue
|
||||||
from discord.ext import commands, tasks
|
from discord.ext import commands, tasks
|
||||||
from ovos_plugin_manager.phal import PHALPlugin
|
from ovos_plugin_manager.phal import PHALPlugin
|
||||||
from ovos_config.config import LocalConf
|
from ovos_config.config import LocalConf
|
||||||
|
|
@ -19,6 +20,9 @@ class DiscordBotPlugin(PHALPlugin):
|
||||||
super().__init__(bus, "ovos-PHAL-plugin-discord-bot", config)
|
super().__init__(bus, "ovos-PHAL-plugin-discord-bot", config)
|
||||||
#self.web_config = LocalConf(get_webcache_location())
|
#self.web_config = LocalConf(get_webcache_location())
|
||||||
|
|
||||||
|
# Initialize queue for Mycroft and user messages
|
||||||
|
self.message_queue = queue.Queue()
|
||||||
|
|
||||||
# Initialize Discord bot and intents
|
# Initialize Discord bot and intents
|
||||||
bot_intents = discord.Intents.all()
|
bot_intents = discord.Intents.all()
|
||||||
self.bot = commands.Bot(command_prefix='!', intents=bot_intents)
|
self.bot = commands.Bot(command_prefix='!', intents=bot_intents)
|
||||||
|
|
@ -36,7 +40,7 @@ class DiscordBotPlugin(PHALPlugin):
|
||||||
# Discord event: bot is ready
|
# Discord event: bot is ready
|
||||||
@self.bot.event
|
@self.bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
#myLoop.start()
|
myLoop.start()
|
||||||
LOG.info(f"{self.bot.user} has connected to Discord!")
|
LOG.info(f"{self.bot.user} has connected to Discord!")
|
||||||
|
|
||||||
admin = await get_discord_user_by_id(self.config.get("admin_id"))
|
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}")
|
LOG.info(f"Admin is {admin.name}")
|
||||||
await admin.send("I'm now online")
|
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()
|
self.setup_configuration()
|
||||||
|
|
||||||
@classproperty
|
@classproperty
|
||||||
|
|
@ -60,9 +77,13 @@ class DiscordBotPlugin(PHALPlugin):
|
||||||
|
|
||||||
def on_utterance(self, message=None):
|
def on_utterance(self, message=None):
|
||||||
LOG.info(f"On utterance triggered: %s", message.serialize())
|
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):
|
def on_speak(self, message=None):
|
||||||
LOG.info(f"On speak triggered: %s", message.serialize())
|
LOG.info(f"On speak triggered: %s", message.serialize())
|
||||||
|
message_queue.put(("mycroft", message.data['utterance']))
|
||||||
|
|
||||||
|
|
||||||
def setup_configuration(self):
|
def setup_configuration(self):
|
||||||
first_start = self.config.get("first_start", True)
|
first_start = self.config.get("first_start", True)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue