简体   繁体   English

Discord bot 删除 python 中“x”数量的消息

[英]Discord bot that deletes messages by “x” amount in python

So the bot I created (in python) can currently delete all messages with the command ".clear all", However, I would also like to add a function to it.所以我创建的机器人(在python中)目前可以使用命令“.clear all”删除所有消息,但是,我还想向它添加一个 function 。 so that people can type in "x" amount and for that to be deleted instead of all?以便人们可以输入“x”数量并删除而不是全部? Is this possible?这可能吗? or would I need to redo this function a different way.还是我需要以不同的方式重做这个 function。 I should add I'm new to python and still learning the ropes.我应该补充一下,我是 python 的新手,并且仍在学习。

Here is my code:这是我的代码:

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)

Many thanks!非常感谢!

Discord.py actually provides functionality to implement something like this, you can add parameters to your command via function arguments , and you can ask discord.py to automatically convert them to specific types (such as integer/numbers ) with converters . Discord.py actually provides functionality to implement something like this, you can add parameters to your command via function arguments , and you can ask discord.py to automatically convert them to specific types (such as integer/numbers ) with converters .

If you're not using discord.py or commands yet, I recommend you do, it helps a lot!如果您还没有使用discord.pycommands ,我建议您这样做,这很有帮助!

To solve the question without these features though, you can take the user input (here message.content and split it after every space ( message_arguments = message.content.split() ) this will give you a list of words (where message_arguments[0] is the command, here !clear , and message_arguments 1 is the second argument, for example all or potentially a number) and from there you need to check whether that second argument is all or a number by using int(message_arguments[1]) , more on that here for example. Once you have the number you can simply use it as a variable and use it to delete the messages: await message.channel.purge(limit=variable_that_holds_a_number) .但是,要解决没有这些功能的问题,您可以获取用户输入(此处为message.content并在每个空格后split它( message_arguments = message.content.split() ),这将为您提供一个单词列表(其中message_arguments[0]是命令,这里是!clear , message_arguments 1是第二个参数,例如all或可能是一个数字),从那里你需要使用int(message_arguments[1])检查第二个参数是all还是一个数字, 更多关于这里的例子。一旦你有了数字,你可以简单地将它用作变量并使用它来删除消息: await message.channel.purge(limit=variable_that_holds_a_number)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM