在 Discord.py 中输入消息
Input Messages in Discord.py
我正在尝试找到一种方法来对我的机器人进行编程,以清除频道中特定数量的消息。但是,我不知道如何让我的机器人 运行 它是基于用户输入数据的代码。例如,假设我是一个想要清除特定数量消息的用户,比如 15 条消息。我希望我的机器人能够准确地清除 15 条消息,不多也不少。我该怎么做?
if message.content == "{clear":
await message.channel.send("Okay")
await message.channel.send("How many messages should I clear my dear sir?")
我得到的都是合法的 lmao。很抱歉我让这个社区如此失望;(
使用 on_message
事件,您必须使用 startswith
方法并创建一个 amount
变量,该变量将不带 {clear
的消息内容作为值:
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
, ...
我正在尝试找到一种方法来对我的机器人进行编程,以清除频道中特定数量的消息。但是,我不知道如何让我的机器人 运行 它是基于用户输入数据的代码。例如,假设我是一个想要清除特定数量消息的用户,比如 15 条消息。我希望我的机器人能够准确地清除 15 条消息,不多也不少。我该怎么做?
if message.content == "{clear":
await message.channel.send("Okay")
await message.channel.send("How many messages should I clear my dear sir?")
我得到的都是合法的 lmao。很抱歉我让这个社区如此失望;(
使用 on_message
事件,您必须使用 startswith
方法并创建一个 amount
变量,该变量将不带 {clear
的消息内容作为值:
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
, ...