如何使用python删除不和谐服务器中的某些表情符号

时间:2020-03-16 17:55:05

标签: python discord

我和几个朋友在冠状病毒学校关闭之前建立了一个虚拟教室。 唯一的问题是人们可以使用中指和区域指示符等表情符号来拼写脏话。

我该怎么做,以便我的机器人删除某些表情符号?

import discord

token = ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
client = discord.Client()  # starts the discord client.

@client.event
async def on_ready():  # method expected by client. This runs once when connected
    print(f'We have logged in as {client.user}')  # notification of login.

@client.event
async def on_message(message):  # event that happens per any message.    
     # i dont know what to put here :(

client.run(token)  # recall my token was saved!

1 个答案:

答案 0 :(得分:0)

下面是一个简单的示例Cog,该示例显示了如何收集表情符号以禁止和热点查看它们是否在消息中。这对于自定义和动画表情符号均适用。

from discord.ext.commands import Cog, command

class EmojiRemover(Cog):
    def __init__(self, bot):
        self.bot = bot
        self.banned_emoji = set()
    def react_check(self, message):
        def check(reaction, user):
            return reaction.message.id == message.id
        return check
    @command()
    async def add_emoji(self, ctx):
        msg = await ctx.send("React with forbidden emojis")
        while True:
            reaction, user = await self.bot.wait_for('reaction_add', check=self.react_check(msg))
            self.banned_emoji.add(str(reaction.emoji))
    @Cog.listener()
    async def on_message(self, message):
        if message.author.bot:
            return
        print(message.content)
        if any(e in message.content for e in self.banned_emoji):
            await message.delete()
    @command()
    async def get_emoji(self, ctx):
        await ctx.send(' '.join(map(str, self.banned_emoji)))