From e5b2ba55d4abe46b6d5b1b3e82ba0f86d2cb80bb Mon Sep 17 00:00:00 2001 From: git-bruh Date: Sat, 2 Jan 2021 18:28:24 +0530 Subject: [PATCH] update --- README.md | 2 ++ cogs/.keep | 0 main.py | 23 +++++++++++++++++------ 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 cogs/.keep diff --git a/README.md b/README.md index 245cc4b..b059baf 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ A simple non-puppeting bridge between Matrix and Discord written in Python. * Edit `config.json` +* Normal Discord bot functionality like commands can be added to the bot via [cogs](https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html), example [here](https://gist.github.com/EvieePy/d78c061a4798ae81be9825468fe146be). + NOTE: [Privileged Intents](https://discordpy.readthedocs.io/en/latest/intents.html#privileged-intents) must be enabled for your Discord bot. ## Known Issues diff --git a/cogs/.keep b/cogs/.keep new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py index 8414a41..de52fe2 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import discord.ext.commands import discord import json import logging @@ -12,6 +13,7 @@ def config_gen(config_file): "username": "@name:matrix.org", "password": "my-secret-password", "token": "my-secret-token", + "discord_prefix": "my-command-prefix", "bridge": {"channel_id": "room_id", } } @@ -143,7 +145,7 @@ class MatrixClient(nio.AsyncClient): self.logger.warning(f"Failed to send message {event_id}: {e}") -class DiscordClient(discord.Client): +class DiscordClient(discord.ext.commands.Bot): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -153,13 +155,21 @@ class DiscordClient(discord.Client): self.bg_task = self.loop.create_task(self.matrix_client.create(self)) - async def on_ready(self): - print(f"Logged in as {self.user}") + self.add_cogs() + def add_cogs(self): + for cog in os.listdir("./cogs"): + if cog.endswith(".py"): + cog = f"cogs.{cog[:-3]}" + self.load_extension(cog) + + async def on_ready(self): for channel in config["bridge"].keys(): channel_store[channel] = self.get_channel(int(channel)) async def on_message(self, message): + await self.process_commands(message) + if message.author.bot or str(message.channel.id) not in \ config["bridge"].keys(): return @@ -372,13 +382,14 @@ class Callbacks(object): def main(): logging.basicConfig(level=logging.INFO) + allowed_mentions = discord.AllowedMentions(everyone=False, roles=False) + command_prefix = config["discord_prefix"] intents = discord.Intents.default() intents.members = True - allowed_mentions = discord.AllowedMentions(everyone=False, roles=False) - DiscordClient( - intents=intents, allowed_mentions=allowed_mentions + allowed_mentions=allowed_mentions, + command_prefix=command_prefix, intents=intents ).run(config["token"])