如何在discord.py中捕获错误并发送响应?

2024-05-14 19:35:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望我的机器人在有人键入不存在的命令时向聊天室发送消息。我试过了,但什么也没发生。代码如下:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix=BOT_PREFIX)

@bot.event
async def on_ready():
    print("Logged in as: " + bot.user.name + "\n")

@bot.command()
async def asdasd(ctx):
    try:
        if asdasd:
            await ctx.send('asd')
    except discord.ext.commands.errors.CommandNotFound:
        await ctx.send('error')

bot.run('My Token Here')

Tags: import命令sendasync键入defbot机器人
1条回答
网友
1楼 · 发布于 2024-05-14 19:35:51

有一个异常处理程序事件专门用于此

例如:

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, discord.ext.commands.errors.CommandNotFound):
        await ctx.send("That command wasn't found! Sorry :(")

阅读更多关于它的信息。所有不同的错误都可以在here中找到

祝你好运

相关问题 更多 >

    热门问题