This commit is contained in:
git-bruh 2021-01-03 13:03:30 +05:30
parent e5b2ba55d4
commit fa83aca814
No known key found for this signature in database
GPG key ID: E1475C50075ADCE6

43
main.py
View file

@ -35,7 +35,7 @@ message_store, channel_store = {}, {}
class MatrixClient(nio.AsyncClient): class MatrixClient(nio.AsyncClient):
async def create(self, discord_client): async def start(self, discord_client):
self.logger = logging.getLogger("matrix_logger") self.logger = logging.getLogger("matrix_logger")
password = config["password"] password = config["password"]
@ -153,7 +153,7 @@ class DiscordClient(discord.ext.commands.Bot):
config["homeserver"], config["username"] config["homeserver"], config["username"]
) )
self.bg_task = self.loop.create_task(self.matrix_client.create(self)) self.bg_task = self.loop.create_task(self.matrix_client.start(self))
self.add_cogs() self.add_cogs()
@ -163,6 +163,11 @@ class DiscordClient(discord.ext.commands.Bot):
cog = f"cogs.{cog[:-3]}" cog = f"cogs.{cog[:-3]}"
self.load_extension(cog) self.load_extension(cog)
def to_return(self, channel_id, user):
if user.discriminator == "0000" \
or str(channel_id) not in config["bridge"].keys():
return True
async def on_ready(self): async def on_ready(self):
for channel in config["bridge"].keys(): for channel in config["bridge"].keys():
channel_store[channel] = self.get_channel(int(channel)) channel_store[channel] = self.get_channel(int(channel))
@ -170,8 +175,7 @@ class DiscordClient(discord.ext.commands.Bot):
async def on_message(self, message): async def on_message(self, message):
await self.process_commands(message) await self.process_commands(message)
if message.author.bot or str(message.channel.id) not in \ if self.to_return(message.channel.id, message.author):
config["bridge"].keys():
return return
content = await self.process_message(message) content = await self.process_message(message)
@ -183,8 +187,7 @@ class DiscordClient(discord.ext.commands.Bot):
message_store[message.id] = matrix_message message_store[message.id] = matrix_message
async def on_message_edit(self, before, after): async def on_message_edit(self, before, after):
if after.author.bot or str(after.channel.id) not in \ if self.to_return(after.channel.id, after.author):
config["bridge"].keys():
return return
content = await self.process_message(after) content = await self.process_message(after)
@ -201,8 +204,7 @@ class DiscordClient(discord.ext.commands.Bot):
) )
async def on_typing(self, channel, user, when): async def on_typing(self, channel, user, when):
if user.bot or str(channel.id) not in \ if self.to_return(channel.id, user) or user == self.user:
config["bridge"].keys():
return return
# Send typing event # Send typing event
@ -247,15 +249,15 @@ class Callbacks(object):
return channel_id return channel_id
async def message_callback(self, room, event): def to_return(self, room, event):
# Ignore messages from ourselves or other rooms
if room.room_id not in config["bridge"].values() or \ if room.room_id not in config["bridge"].values() or \
event.sender == self.matrix_client.user: event.sender == self.matrix_client.user:
return return True
async def message_callback(self, room, event):
message = event.body message = event.body
if 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")
@ -323,9 +325,7 @@ class Callbacks(object):
) )
async def redaction_callback(self, room, event): async def redaction_callback(self, room, event):
# Ignore messages from ourselves or other rooms if self.to_return(room, event):
if room.room_id not in config["bridge"].values() or \
event.sender == self.matrix_client.user:
return return
# Redact webhook message # Redact webhook message
@ -340,21 +340,18 @@ class Callbacks(object):
pass pass
async def typing_callback(self, room, event): async def typing_callback(self, room, event):
# Ignore events from other rooms if not room.typing_users \
if room.room_id not in config["bridge"].values(): or room.room_id not in config["bridge"].values():
return return
if room.typing_users: if len(room.typing_users) == 1 and \
# Ignore events from ourselves self.matrix_client.user in room.typing_users:
if len(room.typing_users) == 1 \
and room.typing_users[0] == self.matrix_client.user:
return return
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():
pass return
async def process_message(self, message, channel_id): async def process_message(self, message, channel_id):
mentions = re.findall(r"(^|\s)(@(\w*))", message) mentions = re.findall(r"(^|\s)(@(\w*))", message)