如何更正此命令?我需要为删除所有消息,用户和bots创建一个命令

问题描述 投票:0回答:1

这是代码

@bot.command()
@commands.has_permissions(manage_messages=True)
async def delMessages(ctx, id=None, reason=None):

    guild = ctx.guild

    id = discord.TextChannel.id(id)

    await guild.delete_messages(id)
    await ctx.delete_channel(ctx.delete_messages(), id)

    await ctx.send(f'The user {ctx.author.mention} has been delete all messages for {reason} on the channel {id}')
    await asyncio.sleep(10)
    await ctx.delete()

我尝试启动命令.delmessages以删除所有消息,以便垃圾邮件机器人不会发送诸如疯狂之类的消息,而是不工作

python-3.x security discord discord.py
1个回答
0
投票

大多数代码在discord.py库中无效。我建议您阅读《 QuickStart指南》,以获得更好的典型命令结构的理解基础,并更紧密地参考示例设计。对我来说,感觉就像您刚刚编制了命令,希望他们能起作用。这不是做到这一点的好方法。

您无法通过实例化
    discord.TextChannel
  1. 来获取频道,然后给它一个ID。使用

    channel = ctx.bot.get_channel(channel_id)

    .
    我不确定要执行的目的,如果您已经拥有命令参数的ID,则无需重做。
    

  2. id = channel.id

    不是有效的命令。没有命令可以在公会范围内删除用户的消息。请查看可用于

    discord.guild对象

    discord.textChannel
  3. 对象的命令。删除消息的基本方法是:
  4. await guild.delete_messages(id)

    
    # assuming "channel" is a discord.TextChannel and "message_id is an integer
    
    # Method 1:
    msg = await channel.fetch_message(message_id)
    
    # Method 2 (be careful you don't delete all messages in channel)
    for async msg in channel.history(...):
       if [ ... ]:  # put in condition here for which messages to delete
          await msg.delete()  # deletes last 
    
    # Method 3
    await channel.purge(..., check=...)  # use "check" to choose which messages to delete
    不是有效的命令,但是如果是,它比删除频道的消息更接近删除频道本身,这当然不是您想要的。我认为。我认为您的预期是“此频道中的作者ID删除消息”之类的东西。如果是这样,请参见上面的点。
    

    您的最后一行,
    await ctx.delete_channel()
    将删除用户命令调用的消息,如果您想要的话,这可能很好。如果您也想从机器人中删除确认响应,请保存从
  5. ctx.delete()
  6. 命令返回的消息:

    send()

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.