如何在用户连续说 nsfw 词 3 次后让不和谐机器人静音

时间:2021-01-29 13:33:49

标签: python discord bots discord.py discord.py-rewrite

我正在尝试制作一个不和谐的机器人,当有人连续说三遍 NSFW 单词时,他们可以静音。我希望每次都删除包含该词的消息,并在第 3 次尝试时将其静音。我似乎无法做到这一点。

这是我的齿轮:

import discord
from discord.ext import commands
import asyncio

with open('nsfw.txt') as file:
    file = file.read().split(',')


class BadWords(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_message(self, msg):
        for word in file:
            if word in msg.content.lower():
                await msg.delete()
                attempts = 0
                attempts += 1

                if attempts == 3:
                    muted_role = msg.author.guild.get_role(783622936837226529)
                    await msg.author.add_roles(muted_role)
                    embed = discord.Embed(
                        title='',
                        description='',
                        colour=discord.Colour.red()
                    )
                    embed.add_field(name=f"✅ {msg.author.display_name} has been muted.", value='Reason: Toxicity')
                    await msg.channel.send(embed=embed)
                    await asyncio.sleep(1800)
                    await msg.author.remove_roles(muted_role)
                    break

                else:
                    pass


def setup(client):
    client.add_cog(BadWords(client))

1 个答案:

答案 0 :(得分:1)

每次有人说一个字时,您都将 attempts 重新设置为 0。删除它并将 self.client.attempts = {} 放在 __init__ 下并将 attempts += 1 更改为:

try: self.client.attempts[msg.author.id] += 1
except KeyError: 
    self.client.attempts[msg.author.id] = 1 #this will only happen the first time

那么,

if self.client.attempts[msg.author.id] == 3: 
    pass # do your other stuff