0

誰かが automod をトリガーする悪い言葉を言うと、ボットはメッセージを削除し、ユーザーに DM を送り、ログ チャネルに記録します。

問題は、誰かがボットをブロックして悪い言葉を言った場合、ボットはユーザーに DM を送信できず、ボットがログ チャネルにイベントを記録することを許可しないことです。

ifelseとを追加してこれを修正する方法を複数試しましexceptたが、それらは役に立ちません。以下は私が既に持っている現在のコードです。違反者がボットをブロックした場合でも、ボットにイベントを記録させるにはどうすればよいですか?

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

1 に答える 1

1

試す/除外する

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実行されません。

解決

1 つ目は DM 用、2 つtryexceptはログ用です。私のソリューションを使用してコードを作成すると、次のようになります。

    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
于 2021-05-28T12:14:15.557 に答える