问题描述
基本上,如果您说的不是脏话,然后将消息编辑成脏话,机器人将不会检测到它.我该如何解决?这是我的代码:
basically if you say something that's not a swear word but then edit the message into a swear word, the bot will not detect it. how do i fix this? here's my code:
@client.event
async def on_message(message):
if message.author.bot:
return
for badword in file:
if badword in message.content.lower():
await message.delete()
warnmessage = f"hey {message.author.mention}! don't say that!"
await message.channel.send(warnmessage, delete_after=5.0)
print(f"{message.author.name} tried saying: {badword}")
channel = client.get_channel(836232733126426666)
embed = discord.embed(title=f"someone tried to swear!", colour=0x2d2d2d)
embed.add_field(name="person who tried to swear:", value=f"{message.author.name}", inline=false)
embed.add_field(name="what they tried to say:", value=f"{badword}", inline=false)
embed.add_field(name="channel they tried to swear in:", value=f"<#{message.channel.id}>", inline=false)
await channel.send(embed=embed)
return
await client.process_commands(message)
if message.content.startswith('jason derulo'):
await message.channel.send('wiggle wiggle wiggle')
if message.content.startswith('fast'):
await message.channel.send('she a runner she a track star')
await client.process_commands(message)
推荐答案
您可以为此使用 on_message_edit 事件,并使用您在 on_message 事件(只要记住使用 after 参数而不是 message)
you can use the on_message_edit event for that and use the same logic you're using in the on_message event (just remember to use the after arg instead of message)
@client.event
async def on_message_edit(before, after):
for badword in file:
if badword in after.content.lower():
await after.delete()
warnmessage = f"hey {after.author.mention}! don't say that!"
await after.channel.send(warnmessage, delete_after=5.0)
print(f"{message.author.name} tried saying: {badword}")
channel = client.get_channel(836232733126426666)
embed = discord.embed(title=f"someone tried to swear!", colour=0x2d2d2d)
embed.add_field(name="person who tried to swear:", value=f"{message.author.name}", inline=false)
embed.add_field(name="what they tried to say:", value=f"{badword}", inline=false)
embed.add_field(name="channel they tried to swear in:", value=f"<#{message.channel.id}>", inline=false)
return await channel.send(embed=embed)
参考:
- on_message_edit
茵茵酱