diff --git a/main.py b/main.py index a2e84ba..aec5702 100644 --- a/main.py +++ b/main.py @@ -55,7 +55,7 @@ class MatrixClient(nio.AsyncClient): self.logger.info("Doing initial sync.") await self.sync(timeout) - # Set up event callbacks + # Set up event callbacks after syncing once to ignore old messages. callbacks = Callbacks(self) self.add_event_callback( @@ -72,11 +72,14 @@ class MatrixClient(nio.AsyncClient): callbacks.typing_callback, nio.EphemeralEvent ) + # Wait for Discord client... await discord_client.wait_until_ready() self.logger.info("Syncing forever.") await self.sync_forever(timeout=timeout) + # Logout + await self.logout() await self.close() async def upload_emote(self, emote_id): @@ -202,13 +205,13 @@ height=\"32\" src=\"{emote_}\" data-mx-emoticon />""" hooks = await channel.webhooks() - # Create webhook if it doesn't exist + # Create webhook if it doesn't exist. hook = discord.utils.get(hooks, name=hook_name) if not hook: hook = await channel.create_webhook(name=hook_name) - # Username must be between 1 and 80 characters in length - # 'wait=True' allows us to store the sent message + # Username must be between 1 and 80 characters in length, + # 'wait=True' allows us to store the sent message. try: hook = await hook.send( username=author[:80], avatar_url=avatar, @@ -236,7 +239,12 @@ class DiscordClient(discord.ext.commands.Bot): self.add_cogs() def add_cogs(self): - for cog in os.listdir("./cogs"): + cogs_dir = "./cogs" + + if not os.path.isdir(cogs_dir): + return + + for cog in os.listdir(cogs_dir): if cog.endswith(".py"): cog = f"cogs.{cog[:-3]}" self.load_extension(cog) @@ -259,6 +267,7 @@ class DiscordClient(discord.ext.commands.Bot): channel_store[channel] = self.get_channel(int(channel)) async def on_message(self, message): + # Process other stuff like cogs before ignoring the message. await self.process_commands(message) if self.to_return(message.channel.id, message.author): @@ -279,6 +288,7 @@ class DiscordClient(discord.ext.commands.Bot): content = await self.process_message(after) + # Edit message only if it can be looked up in the cache. if before.id in message_store: await self.matrix_client.message_send( content[0], after.channel.id, @@ -286,6 +296,7 @@ class DiscordClient(discord.ext.commands.Bot): ) async def on_message_delete(self, message): + # Delete message only if it can be looked up in the cache. if message.id in message_store: await self.matrix_client.message_redact( message_store[message.id], message.channel.id @@ -306,23 +317,27 @@ class DiscordClient(discord.ext.commands.Bot): regex = r"" emotes = {} + # Store all emotes in a dict to upload and insert into formatted body. + # { "emote_name": "emote_id" } for emote in re.findall(regex, content): emotes[emote[0]] = emote[1] + # Get message reference for replies. replied_event = None if message.reference: replied_message = await message.channel.fetch_message( message.reference.message_id ) + # Try to get the corresponding event from the message cache. try: replied_event = message_store[replied_message.id] except KeyError: pass - # Replace emote IDs with names + # Replace emote IDs with names. content = re.sub(regex, r":\g<1>:", content) - # Append attachments to message + # Append attachments to message. for attachment in message.attachments: content += f"\n{attachment.url}" @@ -351,11 +366,13 @@ class Callbacks(object): async def message_callback(self, room, event): message = event.body + # Ignore messages having an empty body. if self.to_return(room, event) or not message: return content_dict = event.source.get("content") + # Get the corresponding Discord channel. channel_id = self.get_channel(room) author = room.user_name(event.sender) @@ -366,14 +383,18 @@ class Callbacks(object): try: if content_dict["m.relates_to"]["rel_type"] == "m.replace": + # Get the original message's event ID. edited_event = content_dict["m.relates_to"]["event_id"] edited_content = await self.process_message( content_dict["m.new_content"]["body"], channel_id ) + + # Get the corresponding Discord message. webhook_message = message_store[edited_event] try: await webhook_message.edit(content=edited_content) + # Handle exception if edited message was deleted on Discord. except ( discord.errors.NotFound, discord.errors.HTTPException ) as e: @@ -388,10 +409,13 @@ class Callbacks(object): try: if content_dict["m.relates_to"]["m.in_reply_to"]["event_id"] in \ message_store.values(): + # Remove the first occurance of our bot's username if replying. + # > <@discordbridge:something.org> [discord user] message = message.replace(f"<{config['username']}>", "", 1) except KeyError: pass + # _testuser waves_ (Italics) if content_dict["msgtype"] == "m.emote": message = f"_{author} {message}_" @@ -399,19 +423,22 @@ class Callbacks(object): embed = None - # Get attachments + # Get attachments. try: attachment = event.url.split("/")[-1] + # TODO: Fix URL for attachments forwarded from other rooms. attachment = f"{url}/{homeserver}/{attachment}" embed = discord.Embed(colour=discord.Colour.blue(), title=message) embed.set_image(url=attachment) + # Send attachment URL in message along with embed, + # Just in-case the attachment is not an image. message = attachment except AttributeError: pass - # Get avatar + # Get avatar. for user in room.users.values(): if user.user_id == event.sender: if user.avatar_url: @@ -427,10 +454,11 @@ class Callbacks(object): if self.to_return(room, event): return - # Redact webhook message + # Try to fetch the message from cache. try: message = message_store[event.redacts] await message.delete() + # Handle exception if message was already deleted on Discord. except discord.errors.NotFound as e: self.matrix_client.logger.warning( f"Failed to delete message {event.event_id}: {e}" @@ -443,12 +471,15 @@ class Callbacks(object): or room.room_id not in config["bridge"].values(): return + # Return if the event is sent by our bot. if len(room.typing_users) == 1 and \ self.matrix_client.user in room.typing_users: return + # Get the corresponding Discord channel. channel_id = self.get_channel(room) + # Send typing event. async with channel_store[channel_id].typing(): return @@ -456,20 +487,27 @@ class Callbacks(object): mentions = re.findall(r"(^|\s)(@(\w*))", message) emotes = re.findall(r":(\w*):", message) + # Get the guild from channel ID. guild = channel_store[channel_id].guild added_emotes = [] for emote in emotes: + # Don't replace emote names with IDs multiple times. + # :emote: becomes <:emote:emote_id> if emote not in added_emotes: added_emotes.append(emote) emote_ = discord.utils.get(guild.emojis, name=emote) if emote_: message = message.replace(f":{emote}:", str(emote_)) + # mentions = [('', '@name', 'name'), (' ', '@', '')] for mention in mentions: + # Don't fetch member if mention is empty. + # Single "@" without any name. if mention[2]: member = await guild.query_members(query=mention[2]) if member: + # Get first result. message = message.replace(mention[1], member[0].mention) return message @@ -478,11 +516,15 @@ class Callbacks(object): def main(): logging.basicConfig(level=logging.INFO) + # Disable everyone and role mentions. allowed_mentions = discord.AllowedMentions(everyone=False, roles=False) + # Set command prefix for Discord bot. command_prefix = config["discord_prefix"] + # Intents to fetch members from guild. intents = discord.Intents.default() intents.members = True + # Start Discord bot. DiscordClient( allowed_mentions=allowed_mentions, command_prefix=command_prefix, intents=intents