我试图找到一种方法来对我的机器人进行编程,以清除通道中特定数量的消息。但是,我不知道如何让我的机器人根据用户的输入数据来运行它的代码。例如,假设我是一个想要清除特定数量消息的用户,例如说15条消息。我希望我的漫游器准确地清除15条消息,没有更多,也没有更少。我该怎么办?
if message.content == "{clear":
await message.channel.send("Okay")
await message.channel.send("How many messages should I clear my dear sir?")
这是我得到的所有合法礼物。很抱歉让我对这个社区感到失望;(
答案 0 :(得分:0)
使用on_message
事件,您必须使用startswith
方法,并创建一个amount
变量,该变量将没有{clear
的消息内容作为值:< / p>
if message.content.startswith("{clear"):
amount = int(message.content[7:])
await message.channel.purge(limit=amount+1)
但是,我不建议使用on_message
事件来创建命令。您可以使用discord.py的commands
框架。创建命令将更加容易。
一个简单的例子:
from discord.ext import commands
bot = commands.Bot(command_prefix='{')
@bot.event
async def on_ready():
print("Bot's ready to go")
@bot.command(name="clear")
async def clear(ctx, amount: int):
await ctx.channel.purge(limit=amount+1)
bot.run("Your token")
ctx
将允许您访问消息author
,channel
,guild
,...,并允许您调用诸如send
之类的方法,...