【问题标题】:Discord bot that deletes messages by "x" amount in python在python中按“x”数量删除消息的不和谐机器人
【发布时间】:2021-05-18 17:15:49
【问题描述】:

所以我创建的机器人(在 python 中)当前可以使用命令“!清除所有”删除所有消息。但是,我还想为其添加一个功能,以便人们可以输入“x”数量,然后将其删除而不是全部删除。这可能吗?还是我需要以不同的方式重做这个功能? 我应该补充一下,我是 python 新手,还在学习。

这是我的代码:

if '!clear all' in message.content and message.author.permissions_in(message.channel).manage_messages:
    await message.channel.send('__**This will delete all messages (even pinned)**__')
    await message.channel.send('**Type:**')
    await message.channel.send('```"!yes" to continue```')
    await message.channel.send('```"!no" to stop```')
if '!no' in message.content and message.author.permissions_in(message.channel).manage_messages:
    await message.channel.send('**This proccess has stopped**')
    exit()
else:
    if '!yes' in message.content and message.author.permissions_in(message.channel).manage_messages:
        await message.channel.send('**This proccess will continue in:**')
        await message.channel.send('**3**')
        await asyncio.sleep(0.5)
        await message.channel.send('**2**')
        await asyncio.sleep(0.5)
        await message.channel.send('**1**')
        deleted = await message.channel.purge(limit=3000)
        await message.channel.send('**All messages deleted. Have a nice day**'.format(deleted))
        await asyncio.sleep(3)
        deleted = await message.channel.purge(limit=3000)

非常感谢!

【问题讨论】:

    标签: python discord bots


    【解决方案1】:

    Discord.py实际上提供了实现这样的功能,你可以通过function arguments给你的命令添加参数,你可以通过@要求discord.py自动将它们转换为特定类型(例如integer/numbers) 987654322@.

    如果您还没有使用discord.pycommands,我建议您这样做,这很有帮助!

    要解决没有这些功能的问题,您可以获取用户输入(这里是 message.contentsplit 在每个空格之后(message_arguments = message.content.split()),这将为您提供一个单词列表(其中message_arguments[0] 是命令,此处为!clear,message_arguments1 是第二个参数,例如all 或可能是一个数字),从那里您需要检查第二个参数是all 还是使用@987654337 的数字@,更多关于 here 的例子。一旦你有了数字,你就可以简单地将它用作变量并使用它来删除消息:await message.channel.purge(limit=variable_that_holds_a_number)

    【讨论】:

      猜你喜欢
      • 2018-05-02
      • 2021-05-29
      • 2021-07-25
      • 1970-01-01
      • 2021-09-26
      • 2018-11-19
      • 2020-05-11
      • 2020-12-23
      • 2020-04-21
      相关资源
      最近更新 更多