From 91ce6584aadbde11d24a29f5c109a1a47d15803b Mon Sep 17 00:00:00 2001 From: git-bruh Date: Wed, 25 Nov 2020 11:06:00 +0530 Subject: [PATCH] Steal regex --- main.py | 49 +++++++++++++------------------------------------ 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/main.py b/main.py index 9b2c66c..89452fb 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ import discord import json import logging import nio +import re import os @@ -92,24 +93,10 @@ async def get_channel(): async def process_discord(message): - content = message.content + content = message.clean_content - emote_list = await process_split(content, "<:", ">") - mention_list = await process_split(content, "<@", ">") - - for emote in emote_list: - emote_name = emote.split(":")[1] - content = content.replace(emote, f":{emote_name}:") - - for mention in mention_list: - # Discord mentions can start with either "<@" or "<@!" - try: - mention_ = int(mention[2:-1]) - except ValueError: - mention_ = int(mention[3:-1]) - - user = discord_client.get_user(mention_) - content = content.replace(mention, f"@{user.name}") + # Replace emote IDs with names + content = re.sub(r"", r"\g<1>", content) # Append attachments to message for attachment in message.attachments: @@ -125,35 +112,25 @@ async def process_matrix(message): message = message.replace("@everyone", "@\u200Beveryone") message = message.replace("@here", "@\u200Bhere") - emote_list = await process_split(message, ":", ":") - mention_list = await process_split(message, "@", "") + mention_list = re.findall(r"(^|\s)(@(\w*))", message) - for emote in emote_list: - emote_ = discord.utils.get(discord_client.emojis, name=emote[1:-1]) - - if emote_: - message = message.replace(emote, str(emote_)) + for emote in message.split(): + if emote[0] == emote[-1] == ":": + emote_ = discord.utils.get(discord_client.emojis, name=emote[1:-1]) + if emote_: + message = message.replace(emote, str(emote_)) channel = await get_channel() guild = channel.guild for mention in mention_list: - for member in await guild.query_members(query=mention[1:]): - message = message.replace(mention, member.mention) + member = guild.get_member_named(mention[2]) + if member: + message = message.replace(mention[1], member.mention) return message -async def process_split(message, start, end): - return_list = [] - - for item in message.split(): - if item.startswith(start) and item.endswith(end): - return_list.append(item) - - return return_list - - async def webhook_send(author, avatar, message, event_id): channel = await get_channel()