最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python 3.x - How to correction this command? i need to create a command for delete all messages, users and bots - Stack Overflow

programmeradmin0浏览0评论

This is the code

@botmand()
@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()

i try start the command .delMessages for delete all messages, so that a spam bot isn't sending messages like crazy, but dont working

This is the code

@botmand()
@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()

i try start the command .delMessages for delete all messages, so that a spam bot isn't sending messages like crazy, but dont working

Share asked Mar 6 at 21:09 KristKrist 1
Add a comment  | 

1 Answer 1

Reset to default 0

Most of your code is not valid in the discord.py library. I recommend you read the quickstart guide to get a better base of understanding for typical command structures, and more closely reference example designs. It feels to me like you just made up commands hoping they would work. This is not a good way to do it.

  1. You cannot get a channel by instantiating discord.TextChannel and then giving it an ID. Use channel = ctx.bot.get_channel(channel_id).

  2. I'm not sure of the point of doing id = channel.id, if you already have the ID from the command arguments you don't need to redo that.

  3. await guild.delete_messages(id) is not a valid command. There is no command to delete messages from a user on a guild-wide scale. Please check the commands available to the discord.Guild object and the discord.TextChannel object. The basic ways to delete a message are:

    # 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
    
  4. await ctx.delete_channel() is not a valid command, but if it were, it's closer to deleting the channel itself than deleting messages from a channel, which is certainly not what you want. I think. I assume what you intended was something like "delete messages by author ID in this channel". If so, see above point.

  5. Your last line, ctx.delete() will delete the message of the command invocation of the user, which could be fine if that's what you want. If you want to delete the confirmation response from the bot as well, save the message returned from the send() command:

    msg = await ctx.send("...")
    await asyncio.sleep(10)
    await msg.delete()  # delete user message (your message)
    await ctx.message.delete()  # delete bot's reply
    
    # or:
    await ctx.message.delete()  # delete user's message immediately
    msg = await ctx.send("...", delete_after=10)  # delete bot reply after 10s
    

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论