如果用户阻止了机器人程序,机器人程序将不会记录automod事件



如果有人说了一个触发automod的脏话,机器人程序将删除消息,DM用户,并将其记录在日志通道中。

问题是,如果有人屏蔽了机器人并说了一句脏话,机器人就无法DM用户,这不允许机器人在日志通道中记录事件。

我尝试了多种方法来解决这个问题,添加了ifelseexcept,但都无济于事。下面是我已经拥有的当前代码,那么如果罪犯阻止了机器人,我如何让机器人仍然记录事件?

@commands.Cog.listener()
async def on_message(self, message):

curseWord = ['bad words here']
msg_content = message.content.lower()  
if any(word in msg_content for word in curseWord):
if message.channel.type == discord.ChannelType.private:
return
if message.author.id == 330988962162147329: #johnny
return
if message.author.id == 467715040087244800: #me
return
if message.author.id == 261578097978114050: #devvy
return
if message.author.id == 835307493558321172: #examplebot
return
await message.delete()
embed=discord.Embed(title="No No Word", description=f"{message.author.mention}, Hey! Those words arent allowed here!", color=0x00FFFF)
embed.timestamp = datetime.datetime.utcnow()
author = message.author
pfp = author.avatar_url
embed.set_author(name=f"{author.name}", icon_url=pfp)
await message.channel.send(embed=embed)
dmembed=discord.Embed(title="AutoMod", description="You were caught saying a bad word!", color=0x00FFFF)
dmembed.add_field(name="**Message:**", value=f"{msg_content}", inline=False)
pfp = author.avatar_url
dmembed.add_field(name="**Server:**", value=f"{message.guild.name}", inline=False)
dmembed.set_author(name=f"{author.name}", icon_url=pfp)
dmembed.timestamp = datetime.datetime.utcnow()
with open('logchannel.json', 'r', encoding='utf-8') as fp:
log_channel = json.load(fp)
try:
await message.author.send(embed=dmembed)
if log_channel:
log_channel = message.guild.get_channel(log_channel[str(message.guild.id)])
logembed=discord.Embed(title="Bot Log", description="Bad Word Said", color=0x00FFFF)
logembed.add_field(name="**Message:**", value=f"{msg_content}", inline=False)
logembed.add_field(name="**Member:**", value=f"{message.author.name}", inline=False)
author = message.author
pfp = author.avatar_url
logembed.set_author(name=f"{author}", icon_url=pfp)
logembed.timestamp = datetime.datetime.utcnow()
await log_channel.send(embed=logembed)
else:
await log_channel.send(embed=logembed)
except (AttributeError, KeyError):
await log_channel.send(embed=logembed)

尝试/排除

当代码中有try/except时,Python将尝试执行代码,如果出现错误,他将执行except部分中的代码。因此,try部分中的代码可能不被执行。

您的代码中有什么需要try/except

在您的代码中,可能引发错误的内容有:

await message.author.send(embed=dmembed) #DM closed
log_channel = ...(log_channel[str(message.guild.id)]) #key error
await log_channel.send(embed=logembed) #NoneType has no attribute send (if the channel doesn't exist)

这意味着,如果第一种可能性出现错误,try中的所有以下代码将不会执行。

解决方案

我建议您有两个try/except,第一个用于DM,第二个用于日志。你用我的解决方案编码现在是:

try:
await message.author.send(embed=dmembed)
except:
pass #ignore error if DM are closed
try:
if log_channel:
log_channel = message.guild.get_channel(log_channel[str(message.guild.id)])
logembed=discord.Embed(title="Bot Log", description="Bad Word Said", color=0x00FFFF)
logembed.add_field(name="**Message:**", value=f"{msg_content}", inline=False)
logembed.add_field(name="**Member:**", value=f"{message.author.name}", inline=False)
author = message.author
pfp = author.avatar_url
logembed.set_author(name=f"{author}", icon_url=pfp)
logembed.timestamp = datetime.datetime.utcnow()
await log_channel.send(embed=logembed)
else: #You can remove that, because if `log_channel` is not, you can't send an embed
await log_channel.send(embed=logembed) #So you can also remove that
except (AttributeError, KeyError):
await log_channel.send(embed=logembed) #Same here, if the above code raises an error, this code can be executed without raising another error.
pass

最新更新