logout the matrix bot, comments

This commit is contained in:
git-bruh 2021-01-21 16:08:04 +05:30
parent 032e996aa3
commit 83ce183d18
No known key found for this signature in database
GPG key ID: E1475C50075ADCE6

62
main.py
View file

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