Add support for redacting matrix messages

This commit is contained in:
git-bruh 2020-11-15 19:38:25 +05:30
parent f03b5167b4
commit 8f5f6b7c8e
No known key found for this signature in database
GPG key ID: E1475C50075ADCE6

41
main.py
View file

@ -35,6 +35,10 @@ discord_client = discord.Client(intents=intents)
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
deletion_queue = []
message_cache = {}
@discord_client.event @discord_client.event
async def on_ready(): async def on_ready():
print(f"Logged in as {discord_client.user}") print(f"Logged in as {discord_client.user}")
@ -49,6 +53,12 @@ async def on_message(message):
if message.author.bot: if message.author.bot:
return return
# Delete redacted messages
for webhook_message in deletion_queue:
message_ = message_cache[webhook_message]
await message_.delete()
deletion_queue.remove(webhook_message)
# Replace mention/emote IDs with names # Replace mention/emote IDs with names
content = await process(message.content, "emote_") content = await process(message.content, "emote_")
content = await process(content, "mention_") content = await process(content, "mention_")
@ -63,6 +73,11 @@ async def on_message(message):
await message_send(content) await message_send(content)
@discord_client.event
async def on_message_delete(message):
pass
@discord_client.event @discord_client.event
async def on_typing(channel, user, when): async def on_typing(channel, user, when):
channel_ = await get_channel() channel_ = await get_channel()
@ -136,7 +151,7 @@ async def process(message, category):
return message return message
async def webhook_send(author, avatar, message): async def webhook_send(author, avatar, message, event_id):
channel = await get_channel() channel = await get_channel()
# Create webhook if it doesn't exist # Create webhook if it doesn't exist
@ -149,7 +164,11 @@ async def webhook_send(author, avatar, message):
# Replace emote names # Replace emote names
message = await process(message, "emote") message = await process(message, "emote")
await hook.send(username=author, avatar_url=avatar, content=message) # 'wait=True' allows us to store the sent message
hook = await hook.send(username=author, avatar_url=avatar, content=message,
wait=True)
message_cache[event_id] = hook
async def create_matrix_client(): async def create_matrix_client():
@ -170,6 +189,8 @@ async def create_matrix_client():
matrix_client.add_event_callback(message_callback, (nio.RoomMessageText, matrix_client.add_event_callback(message_callback, (nio.RoomMessageText,
nio.RoomMessageMedia)) nio.RoomMessageMedia))
matrix_client.add_event_callback(redaction_callback, nio.RedactionEvent)
matrix_client.add_ephemeral_callback(typing_callback, nio.EphemeralEvent) matrix_client.add_ephemeral_callback(typing_callback, nio.EphemeralEvent)
# Sync forever # Sync forever
@ -200,7 +221,7 @@ async def message_callback(room, event):
if not message: if not message:
return return
# Don't reply to ourselves # Don't act on ourselves
if event.sender == matrix_client.user: if event.sender == matrix_client.user:
return return
@ -236,7 +257,19 @@ async def message_callback(room, event):
avatar = f"{url}/{homeserver}/{avatar}" avatar = f"{url}/{homeserver}/{avatar}"
break break
await webhook_send(author, avatar, message) await webhook_send(author, avatar, message, event.event_id)
async def redaction_callback(room, event):
# Don't act on activities in other rooms
if room.room_id != config["room_id"]:
return
# Don't act on ourselves
if event.sender == matrix_client.user:
return
deletion_queue.append(event.redacts)
async def typing_callback(room, event): async def typing_callback(room, event):