Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何让discord机器人在用户连续三次说出nsfw单词后静音_Python_Discord_Bots_Discord.py_Discord.py Rewrite - Fatal编程技术网

Python 如何让discord机器人在用户连续三次说出nsfw单词后静音

Python 如何让discord机器人在用户连续三次说出nsfw单词后静音,python,discord,bots,discord.py,discord.py-rewrite,Python,Discord,Bots,Discord.py,Discord.py Rewrite,我试图制造一个不和谐机器人,当一个人连续三次说一个NSFW单词时,让他静音。我希望包含单词的消息每次都被删除,在第三次尝试时,它们会被静音。我似乎不能这样做 这是我的齿轮: import discord from discord.ext import commands import asyncio with open('nsfw.txt') as file: file = file.read().split(',') class BadWords(commands.Cog):

我试图制造一个不和谐机器人,当一个人连续三次说一个NSFW单词时,让他静音。我希望包含单词的消息每次都被删除,在第三次尝试时,它们会被静音。我似乎不能这样做

这是我的齿轮:

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))

每次有人说一个词时,您都将
尝试设置回
0
。删除该选项并将
self.client.attempts={}
放在
\uuuu init\uuuu
下,并将
attempts+=1
更改为:

try:self.client.truments[msg.author.id]+=1
除KeyError外:
self.client.attempts[msg.author.id]=1#这只会发生在第一次
那么

如果self.client.attempts[msg.author.id]==3:
通过做你的其他事情

我认为
尝试=0
应该在循环之外。