使机器人删除频道中除命令和响应之外的所有内容[discord.py]

2024-05-17 13:28:43 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:我终于让代码工作了,但只针对一个频道。有没有办法让它成为两个频道?尝试过“或”,但不起作用。它只接受一个频道,有时甚至忽略所有频道

一般情况下删除所有用户消息并测试,但不包括!问吧!总的来说是一个问题!测试中的测试

@commands.Cog.listener()
async def on_message(self, message):
    if not message.guild:
        return
    if not message.channel.name == 'general':
        return
    if message.channel.name == 'general' and message.content != '!ask' or '!question':
        if message.author.bot:
            pass
        else:
            await message.delete()

Tags: 代码用户name消息编辑messagereturnif
1条回答
网友
1楼 · 发布于 2024-05-17 13:28:43

您可以通过浏览every commands来检查message author is a bot(对命令的响应)或消息是否包含command name

@commands.Cog.listener()
async def on_message(self, ctx):
    if not message.guild:
       return
    for command in self.client.commands:
        if command.name in message or message.author.bot:
            return
    await message.delete()

要使其在特定频道上工作,您有多种选择:

  • 使用词汇表(如果需要处理多个频道/服务器,则效率不高):
    bot_channels = {}
    
    @commands.command()
    async def bot_channel(self, ctx):
        try:
            temp = bot_channels[ctx.guild.id]
        except:
            bot_channels[ctx.guild.id] = [ctx.channel.id]
            print(f"Set {ctx.channel} as a bot channel")
        else:
            if ctx.channel.id in bot_channels[ctx.guild.id]:
                bot_channels[ctx.guild.id].pop(ctx.channel.id)
                print(f"{ctx.channel} is no longer a bot channel")
            else:
                bot_channels[ctx.guild.id].append(ctx.channel.id)
                print(f"Set {ctx.channel} as a bot channel")
    
    @commands.Cog.listener()
    async def on_message(self, ctx):
        try:
            temp = bot_channels[ctx.guild.id]
        except:
            return
        else:
            if not message.guild:
                return
            for command in self.client.commands:
                if (command.name in message or message.author.bot) and message.channel.id in bot_channels[ctx.guild.id]:
                    return
            await message.delete()
    
  • 使用^{} library和json文件(数据量适中时效率更高):
    json文件
    {}
    
    您的python代码
    from json import loads, dumps
    
    @commands.command()
    async def bot_channel(self, ctx):
        with open('file directory', 'r') as file:
            data = file.read()
            bot_channels = loads(data)
        try:
            temp = bot_channels[str(ctx.guild.id)]
        except:
            bot_channels[str(ctx.guild.id)] = [ctx.channel.id]
            print(f"Set {ctx.channel} as a bot channel")
        else:
            if ctx.channel.id in bot_channels[str(ctx.guild.id)]:
                bot_channels[ctx.guild.id].pop(ctx.channel.id)
                print(f"{ctx.channel} is no longer a bot channel"
            else:
                bot_channels[str(ctx.guild.id)].append(ctx.channel.id)
                print(f"Set {ctx.channel} as a bot channel")
        with open("filename.json", "w") as file:
            bot_channels = file.write(dumps(bot_channels))
    
      @commands.Cog.listener()
      async def on_message(self, ctx):
          with open('file directory', 'r') as file:
              data = file.read()
              bot_channels = loads(data)
          try:
              temp = bot_channels[ctx.guild.id]
          except:
              return
          else:
              if not message.guild:
                  return
              for command in self.client.commands:
                  if (command.name in message or message.author.bot) and message.channel.id in bot_channels[ctx.guild.id]:
                      return
              await message.delete()
    
  • 如果你有很多数据,为了提高效率,你必须使用数据库(在这种情况下,我帮不了你)

相关问题 更多 >