python - 我正在尝试为我的discord.py 机器人设置清除/清除命令。没有错误,但当我运行命令时,没有任何反应

标签 python discord

这是代码的开始。我是否在代码中输入了错误的内容,导致命令无法正常工作?

import discord
import os
from keep_alive import keep_alive
from discord.ext import commands
import random

client = commands.Bot(command_prefix= '>')

@client.event
async def on_ready():
    print('The bot is online')
    await client.change_presence(
        activity=discord.Game('>help ┃ Created by ColeTMK'))

@client.command()
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount=6):
  await ctx.channel.purge(limit=amount)

最佳答案

我已经正确解释了如何做到这一点。使用协程是可能的。

您的代码中似乎没有错误。我只想说创建我在下面提到的命令并重试。


我首先创建了一个purge命令,其中包含以下代码:

@nucleobot.command()
@commands.has_permissions(manage_messages=True)
async def purge(ctx, limit: int):
    await ctx.message.delete()
    await asyncio.sleep(1)
    await ctx.channel.purge(limit=limit)
    purge_embed = discord.Embed(title='Purge [!purge]', description=f'Successfully purged {limit} messages. \n Command executed by {ctx.author}.', color=discord.Colour.random())
    purge_embed.set_footer(text=str(datetime.datetime.now()))
    await ctx.channel.send(embed=purge_embed, delete_after=True)

这段代码是如何工作的?

  1. 命令用法被删除,即!purge 10发送到聊天中时将被删除。

  2. 由于 await asyncio.sleep(1),它将暂停 1 秒。您需要导入 asyncio 才能使用它。 (你可能已经知道了:D)

  3. 使用await ctx.message.delete(limit=limit)从 channel 中清除您输入的消息数量(这是一个discord.py协程)

  4. purge_embed 是嵌入变量,用于在删除后发送嵌入内容。我使用 datetime 模块在嵌入中添加命令完成的时间。 (您还需要导入日期时间,但前提是您想使用它。如果不需要,则删除页脚代码。)

这将构成完整且有效的purge命令。 :D


带有图像的示例。

我创建了一个新 channel ,并添加了 10 条消息,编号从 1 到 10,如下所示:

Messages in Trail

然后我在消息框中输入了命令,就像(我知道不需要它,但没关系):

Command Entered

在我发送此消息并执行命令后,机器人发布了清除成功嵌入的消息:

Successful Purge


我很高兴能提供帮助。如有任何混淆的疑问,我们表示赞赏。随时问我。 :D

谢谢! :)

关于python - 我正在尝试为我的discord.py 机器人设置清除/清除命令。没有错误,但当我运行命令时,没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67157139/

相关文章:

python - 如何测试自定义 python-social-auth 管道?

python - 在 dm 中发送问题,并在嵌入到 channel discord.py 中发布答案

javascript - 如何在 Math.floor 中使用变量

javascript - puppeteer .match,如何让控制台记录字符串的所有内容而不仅仅是我的搜索

python - 谷歌应用引擎 - Python : How to use Namespaces and Ancestors?

python - 如何在Python中安排定时事件

python - 在两个独立的Python程序之间传输数据有哪些好方法?

python - 分配给未映射到 SQLAlchemy 列的属性时如何引发异常?

discord - 我正在尝试使用 discord.py 构建歌词命令,但出现此错误。关于我做错了什么以及如何修复它有什么想法吗?

python - 如何列出discord.py 中的所有角色