【问题标题】:Python discord.py display unknown commandPython discord.py 显示未知命令
【发布时间】:2023-03-12 22:52:01
【问题描述】:

我正在尝试制作一个记录未知命令的日志系统:

@client.event
async def on_command_error(ctx, error):
    channel = client.get_channel(test)
    await channel.send(f"**{ctx.author.mention} **SEND COMMAND: ** `{ctx.prefix}{ctx.command.name}` **IN CHANNEL: ** {ctx.channel.mention}!")

但是发送None,有什么想法吗?

【问题讨论】:

  • "但是发送None" -- 因为命令没有被执行
  • 我知道,我要求修复
  • if isinstance(error, CommandNotFound): 如果找不到命令,则会记录错误。
  • 但是我可以打印吗?我之前试过
  • 查看这篇文章,如果它有帮助stackoverflow.com/questions/52900101/…

标签: python discord.py


【解决方案1】:

这很好用(适合我的情况)

import discord
from discord.ext import commands

class NameOfYourBot(commands.Cog):
    def __init__(self, bot):
        bot.remove_command('help')
        self.bot = bot

    @commands.Cog.listener()
    async def on_ready(self):
        print(f'Logged in as {self.bot.user} ({self.bot.user.id})')


    @commands.Cog.listener()
    async def on_command_error(self, ctx, error):
        await ctx.send(error)

    @commands.command()
    async def ping(self, ctx):
        await ctx.send("pong")

        
bot = commands.Bot(command_prefix='!',description='description', case_insensitive=True)

bot.add_cog(NameOfYourBot(bot))
bot.run('TOKEN')

此代码在我的设置(机器人的登录文件)文件中,并且工作正常。作为未定义命令的输出,我得到this

【讨论】:

    猜你喜欢
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2020-08-22
    • 1970-01-01
    • 2021-09-26
    • 2021-01-21
    相关资源
    最近更新 更多