Add support for mentioning discord user with partial name
This commit is contained in:
parent
7fdfaadafe
commit
da2ed9bf52
2 changed files with 31 additions and 2 deletions
|
@ -12,6 +12,8 @@ A simple Matrix-Discord bridge written in Python.
|
|||
|
||||
* Edit `config.json`
|
||||
|
||||
NOTE: [Privileged Intents](https://discordpy.readthedocs.io/en/latest/intents.html) must be enabled for your Discord bot.
|
||||
|
||||
## What Works
|
||||
|
||||
- [x] Sending messages
|
||||
|
|
31
main.py
31
main.py
|
@ -29,8 +29,9 @@ def config_gen(config_file):
|
|||
|
||||
config = config_gen("config.json")
|
||||
|
||||
discord_client = discord.Client()
|
||||
|
||||
intents = discord.Intents.default()
|
||||
intents.members = True
|
||||
discord_client = discord.Client(intents=intents)
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
||||
|
@ -118,6 +119,24 @@ async def webhook_send(author, avatar, message):
|
|||
await hook.send(username=author, avatar_url=avatar, content=message)
|
||||
|
||||
|
||||
async def partial_mention(user):
|
||||
# Get Discord channel from channel ID
|
||||
channel = int(config["channel_id"])
|
||||
channel = discord_client.get_channel(channel)
|
||||
|
||||
# Get guild to parse member list
|
||||
guild = channel.guild
|
||||
|
||||
# Remove "@"
|
||||
user = user[1:]
|
||||
|
||||
for member in await guild.query_members(query=user):
|
||||
user_mention = f"<@!{member.id}>"
|
||||
return user_mention
|
||||
|
||||
return None
|
||||
|
||||
|
||||
async def create_matrix_client():
|
||||
homeserver = config["homeserver"]
|
||||
username = config["username"]
|
||||
|
@ -170,6 +189,14 @@ async def message_callback(room, event):
|
|||
homeserver = author.split(":")[-1]
|
||||
url = "https://matrix.org/_matrix/media/r0/download"
|
||||
|
||||
# Replace partial mention of Discord user with ID
|
||||
if message.startswith("@"):
|
||||
user = message.split()[0]
|
||||
user_mention = await partial_mention(user)
|
||||
|
||||
if user_mention is not None:
|
||||
message = message.replace(user, user_mention)
|
||||
|
||||
# Get attachments
|
||||
try:
|
||||
attachment = event.url.split("/")[-1]
|
||||
|
|
Loading…
Reference in a new issue