|
@@ -15,8 +15,6 @@
|
15
|
15
|
"""
|
16
|
16
|
|
17
|
17
|
from matrix_client.client import MatrixClient
|
18
|
|
-import requests
|
19
|
|
-import json
|
20
|
18
|
import discord
|
21
|
19
|
from discord import Webhook, RequestsWebhookAdapter
|
22
|
20
|
import re
|
|
@@ -31,6 +29,9 @@ matrix_room = matrix_client.join_room(matrix_room_id)
|
31
|
29
|
matrix_file_types = ('m.file', 'm.image', 'm.video', 'm.audio')
|
32
|
30
|
|
33
|
31
|
message_id_cache = {}
|
|
32
|
+unmatched_messages_cache = {}
|
|
33
|
+
|
|
34
|
+message_delete_queue = []
|
34
|
35
|
|
35
|
36
|
def prepare_matrix_content(message):
|
36
|
37
|
attachments = "\n".join([x.url for x in message.attachments])
|
|
@@ -38,6 +39,7 @@ def prepare_matrix_content(message):
|
38
|
39
|
return content
|
39
|
40
|
|
40
|
41
|
guild = None
|
|
42
|
+channel = None
|
41
|
43
|
emojis = {}
|
42
|
44
|
webhook = None
|
43
|
45
|
|
|
@@ -46,12 +48,21 @@ async def on_ready():
|
46
|
48
|
global guild
|
47
|
49
|
global emojis
|
48
|
50
|
global webhook
|
49
|
|
- guild = discord_client.get_channel(discord_channel).guild
|
|
51
|
+ global channel
|
|
52
|
+ channel = discord_client.get_channel(discord_channel)
|
|
53
|
+ guild = channel.guild
|
50
|
54
|
emojis = {":{}:".format(emoji.name): "<:{}:{}>".format(emoji.name, emoji.id) for emoji in guild.emojis}
|
51
|
55
|
webhook = Webhook.from_url(webhook_url, adapter=RequestsWebhookAdapter())
|
52
|
56
|
|
53
|
57
|
@discord_client.event
|
54
|
58
|
async def on_message(message):
|
|
59
|
+ for message_id in message_delete_queue:
|
|
60
|
+ message_delete_queue.remove(message_id)
|
|
61
|
+ message = await channel.get_message(message_id)
|
|
62
|
+ await message.delete()
|
|
63
|
+ if message.author.name in unmatched_messages_cache.keys():
|
|
64
|
+ message_id_cache[unmatched_messages_cache[message.author.name]] = message.id
|
|
65
|
+ del unmatched_messages_cache[message.author.name]
|
55
|
66
|
if message.author.discriminator == "0000" or message.channel.id != discord_channel: return
|
56
|
67
|
username = message.author.name[:1] + "\u200B" + message.author.name[1:] + "#" + message.author.discriminator
|
57
|
68
|
content = prepare_matrix_content(message)
|
|
@@ -70,9 +81,11 @@ async def on_message_edit(before, after):
|
70
|
81
|
|
71
|
82
|
@discord_client.event
|
72
|
83
|
async def on_raw_message_delete(paylod):
|
73
|
|
- matrix_room.redact_message(message_id_cache[paylod.message_id], reason="Message Deleted")
|
|
84
|
+ if payload.message_id in message_id_cache.keys():
|
|
85
|
+ matrix_room.redact_message(message_id_cache[paylod.message_id], reason="Message Deleted")
|
74
|
86
|
|
75
|
|
-def send_webhook(username, avatar_url, content):
|
|
87
|
+def send_webhook(username, avatar_url, content, matrix_id):
|
|
88
|
+ unmatched_messages_cache[username] = matrix_id;
|
76
|
89
|
webhook.send(content=content, username=username, avatar_url=avatar_url)
|
77
|
90
|
|
78
|
91
|
def prepare_discord_content(content):
|
|
@@ -96,12 +109,14 @@ def on_matrix_message(room, event):
|
96
|
109
|
username = "{}{}".format(discord_prefix, user.get_display_name())
|
97
|
110
|
avatar = user.get_avatar_url()
|
98
|
111
|
content = prepare_discord_content(event['content']['body'])
|
99
|
|
- message_id_cache[event['event_id']] = send_webhook(username, avatar, content)
|
|
112
|
+ send_webhook(username, avatar, content, event['event_id'])
|
100
|
113
|
if event['content']['msgtype'] in matrix_file_types:
|
101
|
114
|
username = "{}{}".format(discord_prefix, user.get_display_name())
|
102
|
115
|
avatar = user.get_avatar_url()
|
103
|
116
|
content = matrix_homeserver + "/_matrix/media/v1/download/" + event['content']['url'][6:]
|
104
|
|
- message_id_cache[event['event_id']] = send_webhook(username, avatar, content)
|
|
117
|
+ send_webhook(username, avatar, content, event['event_id'])
|
|
118
|
+ if event['type'] == "m.room.redaction" and not user.user_id == matrix_user_id:
|
|
119
|
+ message_delete_queue.append(message_id_cache[event['redacts']])
|
105
|
120
|
|
106
|
121
|
matrix_room.add_listener(on_matrix_message)
|
107
|
122
|
matrix_client.start_listener_thread()
|