From 0b53c6b864735dbc0ebf675eaa478f1275ae0fa9 Mon Sep 17 00:00:00 2001 From: Skipper Date: Mon, 3 Feb 2025 11:19:02 +0000 Subject: Added discord client Begun implementation of the discord client. Will exclude the config.py after this - so any changes made during testing (including API keys) will not be copied through - un-ignore if adding new config values. --- lib/DiscordClient.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/DiscordClient.py (limited to 'lib') diff --git a/lib/DiscordClient.py b/lib/DiscordClient.py new file mode 100644 index 0000000..431a1d1 --- /dev/null +++ b/lib/DiscordClient.py @@ -0,0 +1,42 @@ +import discord +import asyncio +from lib.PacketManager.packets.FSNETCMD_TEXTMESSAGE import FSNETCMD_TEXTMESSAGE +from config import DISCORD_TOKEN, CHANNEL_ID + +class DiscordClient(discord.Client): + + def __init__(self, intents, parent=None): + super().__init__(intents=intents) + self.parent = parent + + async def on_ready(self): + await self.check_messages() + + async def on_message(self, message): + if message.channel.id == CHANNEL_ID: + if message.author.id == self.user.id: + return + author = message.author.name + content = message.content + messageToSendToClients = FSNETCMD_TEXTMESSAGE.encode(author, content) + if self.parent: + self.parent.send_to_all(messageToSendToClients) + + async def send_message(self, message): + channel = self.get_channel(CHANNEL_ID) + if channel: + message = await channel.send(message) + + async def check_messages(self): + while True: + if self.parent: + for message in self.parent.chatLog: + await self.send_message(message) + self.parent.chatLog.remove(message) + await asyncio.sleep(1) + + def start_bot(self): + loop = asyncio.get_event_loop() + loop.create_task(self.check_messages()) + self.run(DISCORD_TOKEN) + -- cgit v1.2.3