【问题标题】:HOW TO USE PURGE COMMAND INSIDE ON_MESSAGE如何在 ON_MESSAGE 中使用 PURGE 命令
【发布时间】:2021-06-17 18:45:48
【问题描述】:

我找到了一个清除命令,但它对我不起作用,因为我使用 client = discord.Client(command_prefix = '$', description = ' ') 而不是 client = commands.Bot(command_prefix = '$') 而且我在中使用了很多命令

@client.event 
async def on_message(message):

所以我也想在on_message 中使用清除命令。谢谢!

清除命令:

@client.command()
async def clear(ctx, amount = 5):
    await ctx.channel.purge(limit = amount)

【问题讨论】:

  • on_message 事件并不值得。你可以这样做:if message.content.startswith("clear"):
  • 我可以用什么来代替@client.command()
  • 你为什么要使用别的东西?
  • 因为我有一个没有属性命令的不同客户端

标签: python discord discord.py bots


【解决方案1】:

我删除了我的第一个答案,因为我看到我可以改进,我最终发现你可以做到

ctx = await client.get_context(message)
split = message.content.split()
if split[0] == "$clear": #Checking if the message is the clear command, you can also use message.content.tolower().startswith("$clear"):
    if len(split) == 2:
        num = 0
        try:
            num = int(split[1]) #checking if the second param <amount> is an int
        except ValueError:
            await message.channel.send("<amount> in $clear <amount> must be a number")
            return
        await ctx.channel.purge(limit = num)
    else:
        await message.channel.send("Please enter the command as $clear <amount>")

它对我来说很好用。客户也必须是

client = commands.Bot(params)

【讨论】:

  • 太棒了!谢谢!感谢您的帮助!
  • 不客气!在我看到你的问题之前,我从来没有想过我可以做到这一点!所以在某种程度上你也帮助了我的机器人!
【解决方案2】:

您必须执行与 on_message 事件中的其他命令相同的 if 语句。由于您在 on_message 事件中使用它,因此您无法真正设置使用该命令的数量,因此您必须预先定义它。

定义数量并定义要清除的通道。 您可以通过discord.Client.get_channel或选择消息发送到message.channel的频道来获取频道。

【讨论】:

  • 我如何获得金额
  • 您必须自己定义金额,例如使用amount = 15
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 2021-08-03
  • 1970-01-01
  • 2021-09-22
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
相关资源
最近更新 更多