Przeglądaj źródła

Add support for attachments both ways

Alex Taber 6 lat temu
rodzic
commit
a4548fa432
1 zmienionych plików z 12 dodań i 4 usunięć
  1. 12
    4
      app.py

+ 12
- 4
app.py Wyświetl plik

@@ -10,11 +10,14 @@ discord_client = discord.Client()
10 10
 
11 11
 matrix_room = matrix_client.join_room(matrix_room_id)
12 12
 
13
+matrix_file_types = ('m.file', 'm.image', 'm.video', 'm.audio')
14
+
13 15
 @discord_client.event
14 16
 async def on_message(message):
15 17
 	if message.author.discriminator == "0000": return
16 18
 	username = message.author.display_name[:1] + "\u200B" + message.author.display_name[1:]
17
-	matrix_room.send_text("<{}> {}".format(username, message.clean_content))
19
+	attachments = "\n".join([x.url for x in message.attachments])
20
+	matrix_room.send_text("<{}> {}".format(username, message.clean_content + ("\n" + attachments if attachments != "" else "")))
18 21
 
19 22
 def send_webhook(username, avatar_url, content):
20 23
 	data = {'username': username, 'content': content}
@@ -34,13 +37,18 @@ def prepare_discord_content(content):
34 37
 	return content
35 38
 
36 39
 def on_matrix_message(room, event):
37
-	if event['type'] == "m.room.message":
38
-		user = matrix_client.get_user(event['sender'])
39
-		if event['content']['msgtype'] == "m.text" and not user.user_id == matrix_user_id:
40
+	user = matrix_client.get_user(event['sender'])
41
+	if event['type'] == "m.room.message" and not user.user_id == matrix_user_id:
42
+		if event['content']['msgtype'] == "m.text":
40 43
 			username = "[Matrix] {}".format(user.get_display_name())
41 44
 			avatar = user.get_avatar_url()
42 45
 			content = prepare_discord_content(event['content']['body'])
43 46
 			send_webhook(username, avatar, content)
47
+		if event['content']['msgtype'] in matrix_file_types:
48
+			username = "[Matrix] {}".format(user.get_display_name())
49
+			avatar = user.get_avatar_url()
50
+			content = matrix_homeserver + "/_matrix/media/v1/download/" + event['content']['url'][6:]
51
+			send_webhook(username, avatar, content)
44 52
 
45 53
 matrix_room.add_listener(on_matrix_message)
46 54
 matrix_client.start_listener_thread()