Parcourir la source

Delete previous message when edited, carry deletes from discord

Alex Taber il y a 6 ans
Parent
révision
7d80aa5d8d
1 fichiers modifiés avec 19 ajouts et 9 suppressions
  1. 19
    9
      app.py

+ 19
- 9
app.py Voir le fichier

@@ -18,6 +18,7 @@ from matrix_client.client import MatrixClient
18 18
 import requests
19 19
 import json
20 20
 import discord
21
+from discord import Webhook, RequestsWebhookAdapter
21 22
 import re
22 23
 from config import *
23 24
 
@@ -29,6 +30,8 @@ matrix_room = matrix_client.join_room(matrix_room_id)
29 30
 
30 31
 matrix_file_types = ('m.file', 'm.image', 'm.video', 'm.audio')
31 32
 
33
+message_id_cache = {}
34
+
32 35
 def prepare_matrix_content(message):
33 36
 	attachments = "\n".join([x.url for x in message.attachments])
34 37
 	content = message.clean_content + ("\n" + attachments if attachments != "" else "")
@@ -36,34 +39,41 @@ def prepare_matrix_content(message):
36 39
 
37 40
 guild = None
38 41
 emojis = {}
42
+webhook = None
39 43
 
40 44
 @discord_client.event
41 45
 async def on_ready():
42 46
 	global guild
43 47
 	global emojis
48
+	global webhook
44 49
 	guild = discord_client.get_channel(discord_channel).guild
45 50
 	emojis = {":{}:".format(emoji.name): "<:{}:{}>".format(emoji.name, emoji.id) for emoji in guild.emojis}
51
+	webhook = Webhook.from_url(webhook_url, adapter=RequestsWebhookAdapter())
46 52
 
47 53
 @discord_client.event
48 54
 async def on_message(message):
49 55
 	if message.author.discriminator == "0000" or message.channel.id != discord_channel: return
50 56
 	username = message.author.name[:1] + "\u200B" + message.author.name[1:] + "#" + message.author.discriminator
51 57
 	content = prepare_matrix_content(message)
52
-	matrix_room.send_text("<{}> {}".format(username, content))
58
+	matrix_message_id = matrix_room.send_text("<{}> {}".format(username, content))['event_id']
59
+	message_id_cache[message.id] = matrix_message_id
53 60
 
54 61
 @discord_client.event
55 62
 async def on_message_edit(before, after):
56
-	if after.channel.id != discord_channel: return
63
+	if after.author.discriminator == "0000" or after.channel.id != discord_channel: return
64
+	if after.content == before.content: return
57 65
 	after.attachments = []
58 66
 	username = after.author.name[:1] + "\u200B" + after.author.name[1:] + "#" + after.author.discriminator
59 67
 	content = prepare_matrix_content(after)
60
-	matrix_room.send_text("<{}> {} (edited)".format(username, content))
68
+	matrix_room.redact_message(message_id_cache[before.id], reason="Message Edited")
69
+	message_id_cache[after.id] = matrix_room.send_text("<{}> {} (edited)".format(username, content))['event_id']
70
+
71
+@discord_client.event
72
+async def on_raw_message_delete(paylod):
73
+	matrix_room.redact_message(message_id_cache[paylod.message_id], reason="Message Deleted")
61 74
 
62 75
 def send_webhook(username, avatar_url, content):
63
-	data = {'username': username, 'content': content}
64
-	if avatar_url: data['avatar_url'] = avatar_url
65
-	headers = {'Content-type': 'application/json'}
66
-	r = requests.post(webhook_url, data = json.dumps(data), headers=headers)
76
+	webhook.send(content=content, username=username, avatar_url=avatar_url)
67 77
 
68 78
 def prepare_discord_content(content):
69 79
 	content = content.replace("@everyone", "@\u200Beveryone")
@@ -86,12 +96,12 @@ def on_matrix_message(room, event):
86 96
 			username = "{}{}".format(discord_prefix, user.get_display_name())
87 97
 			avatar = user.get_avatar_url()
88 98
 			content = prepare_discord_content(event['content']['body'])
89
-			send_webhook(username, avatar, content)
99
+			message_id_cache[event['event_id']] = send_webhook(username, avatar, content)
90 100
 		if event['content']['msgtype'] in matrix_file_types:
91 101
 			username = "{}{}".format(discord_prefix, user.get_display_name())
92 102
 			avatar = user.get_avatar_url()
93 103
 			content = matrix_homeserver + "/_matrix/media/v1/download/" + event['content']['url'][6:]
94
-			send_webhook(username, avatar, content)
104
+			message_id_cache[event['event_id']] = send_webhook(username, avatar, content)
95 105
 
96 106
 matrix_room.add_listener(on_matrix_message)
97 107
 matrix_client.start_listener_thread()