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. --- config.py | 9 +++++++++ lib/DiscordClient.py | 42 ++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + testDiscord.py | 7 +++++++ 4 files changed, 59 insertions(+) create mode 100644 lib/DiscordClient.py create mode 100644 requirements.txt create mode 100644 testDiscord.py diff --git a/config.py b/config.py index ab3b14c..c8cce89 100644 --- a/config.py +++ b/config.py @@ -44,3 +44,12 @@ SMOKE_PLANE = True # Planes smoking minimum life SMOKE_LIFE = 5 + + +#Discord chat integration +#Make sure to enable read message privilege for the bot. + +DISCORD_TOKEN = "YourDiscordBotTOKEN" + +# Channel ID for the chat +CHANNEL_ID = 0 # Channel ID, as an integer 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) + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..844f49a --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +discord.py diff --git a/testDiscord.py b/testDiscord.py new file mode 100644 index 0000000..ca084e8 --- /dev/null +++ b/testDiscord.py @@ -0,0 +1,7 @@ +from lib.DiscordClient import DiscordClient +import discord + +intents = discord.Intents.default() +intents.message_content = True +client = DiscordClient(intents) +client.start_bot() -- cgit v1.2.3