Nessuna descrizione

app.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """
  2. Copyright 2018 Alex Taber ("astronautlevel")
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. """
  13. from matrix_client.client import MatrixClient
  14. import requests
  15. import json
  16. import discord
  17. import re
  18. from config import *
  19. matrix_client = MatrixClient(matrix_homeserver)
  20. token = matrix_client.login(matrix_username, matrix_password)
  21. discord_client = discord.Client()
  22. matrix_room = matrix_client.join_room(matrix_room_id)
  23. matrix_file_types = ('m.file', 'm.image', 'm.video', 'm.audio')
  24. def prepare_matrix_content(message):
  25. attachments = "\n".join([x.url for x in message.attachments])
  26. content = message.clean_content + ("\n" + attachments if attachments != "" else "")
  27. return content
  28. guild = None
  29. emojis = {}
  30. @discord_client.event
  31. async def on_ready():
  32. global guild
  33. global emojis
  34. guild = discord_client.get_channel(discord_channel).guild
  35. emojis = {":{}:".format(emoji.name): "<:{}:{}>".format(emoji.name, emoji.id) for emoji in guild.emojis}
  36. @discord_client.event
  37. async def on_message(message):
  38. if message.author.discriminator == "0000" or message.channel.id != discord_channel: return
  39. username = message.author.name[:1] + "\u200B" + message.author.name[1:] + "#" + message.author.discriminator
  40. content = prepare_matrix_content(message)
  41. matrix_room.send_text("<{}> {}".format(username, content))
  42. def send_webhook(username, avatar_url, content):
  43. data = {'username': username, 'content': content}
  44. if avatar_url: data['avatar_url'] = avatar_url
  45. headers = {'Content-type': 'application/json'}
  46. r = requests.post(webhook_url, data = json.dumps(data), headers=headers)
  47. def prepare_discord_content(content):
  48. content = content.replace("@everyone", "@\u200Beveryone")
  49. content = content.replace("@here", "@\u200Bhere")
  50. content = re.sub("</?del>", "~~", content)
  51. mentions = re.findall("(^|\s)(@(\w*))", content)
  52. for mention in mentions:
  53. member = guild.get_member_named(mention[2])
  54. if not member: continue
  55. content = content.replace(mention[1], member.mention)
  56. for emoji_name, emoji_id in emojis.items():
  57. if emoji_name in content:
  58. content = content.replace(emoji_name, emoji_id)
  59. return content
  60. def on_matrix_message(room, event):
  61. user = matrix_client.get_user(event['sender'])
  62. if event['type'] == "m.room.message" and not user.user_id == matrix_user_id:
  63. if event['content']['msgtype'] == "m.text":
  64. username = "{}{}".format(discord_prefix, user.get_display_name())
  65. avatar = user.get_avatar_url()
  66. content = prepare_discord_content(event['content']['body'])
  67. send_webhook(username, avatar, content)
  68. if event['content']['msgtype'] in matrix_file_types:
  69. username = "{}{}".format(discord_prefix, user.get_display_name())
  70. avatar = user.get_avatar_url()
  71. content = matrix_homeserver + "/_matrix/media/v1/download/" + event['content']['url'][6:]
  72. send_webhook(username, avatar, content)
  73. matrix_room.add_listener(on_matrix_message)
  74. matrix_client.start_listener_thread()
  75. discord_client.run(discord_token)