【问题标题】:Input Messages in Discord.py在 Discord.py 中输入消息
【发布时间】:2020-11-01 18:01:37
【问题描述】:

我正在尝试找到一种方法来对我的机器人进行编程以清除频道中特定数量的消息。但是,我不知道如何让我的机器人根据用户的输入数据运行它的代码。例如,假设我是一个想要清除特定数量消息的用户,比如 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。很抱歉,我对这个社区如此失望;(

【问题讨论】:

    标签: python-3.x pycharm discord.py discord.py-rewrite


    【解决方案1】:

    使用on_message 事件,您必须使用startswith mehtod 并创建一个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 将允许您访问消息authorchannelguild,...并允许您调用诸如send、...等方法

    【讨论】:

      猜你喜欢
      • 2019-01-12
      • 2018-06-29
      • 2019-05-01
      • 2020-12-21
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 2020-09-08
      相关资源
      最近更新 更多