Python 如何让discord机器人在用户连续三次说出nsfw单词后静音
我试图制造一个不和谐机器人,当一个人连续三次说一个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):
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
应该在循环之外。