discord.py 事件未运行 on_command_error()

问题描述 投票:0回答:1

我希望一旦斜杠命令出现错误,它就会向房间发送一条消息并描述错误(MissingPermission、未知交互等),但不要在每个斜杠命令中放置多个检查器以节省时间,所以我没有收到任何 print() 我使用“tree”作为斜杠命令。 我尝试了很多类似的事情,但除了 on_interaction 之外,该事件不会启动,但我不知道我们是否可以获得错误值

import discord
from discord import app_commands, Intents, Webhook, File, Embed, AllowedMentions, Interaction, Color, Spotify
from discord.ext import commands
from discord.ui import Button, View, Select, Modal, TextInput
from youtube_dl import YoutubeDL
import os
import io
import json
import time
import random
import re
import sys
import asyncio
import difflib
import openai
import pyaudio
import traceback
# from dotenv import load_dotenv
from typing import Optional, Union
import datetime
import speech_recognition as sr
from pathlib import Path
from io import BytesIO
from PIL import Image, ImageDraw, ImageOps
from fuzzywuzzy import process
import pygame
from gtts import gTTS

from GroupesTESTER.TAmusement import setup_amusement
from GroupesTESTER.TApplication import setup_application
from GroupesTESTER.TCommunautaire import setup_communautaire
from GroupesTESTER.TInvitation import setup_invitation
from GroupesTESTER.TModeration import setup_moderation
# from GroupesTESTER.TTicket import setup_ticket
from GroupesTESTER.TUtilitaire import setup_utilitaire
from GroupesTESTER.TÉvénements import setup_événements

# Fichers
from variables import *
from keytoken import *

intents = discord.Intents.default()
intents.voice_states = True
intents = Intents.default()
intents.members = True
intents.guild_messages = True
intents.message_content = True

bot = commands.Bot(command_prefix = "!", intents = discord.Intents.all())

def check_dnd():
    async def predicate(ctx):
        if ctx.bot.status == discord.Status.dnd and not ctx.user.id != 937008450515374161:
            await ctx.send('Bot is in DND mode. Commands cannot be used.')
            return False
        return True
    return commands.check(predicate)

parts = Version_Presente.split(".", 1)
vP = parts[1] if len(parts) > 1 else ""

parts = Version_Futur.split(".", 1)
vF = parts[1] if len(parts) > 1 else ""
maj = discord.Game(f"v{vP}>{vF}\Fix de bugs")

@bot.event
async def on_connect():
  await setup_amusement(bot)
  await setup_application(bot)
  await setup_communautaire(bot)
  await setup_invitation(bot)
  await setup_moderation(bot)
  # await setup_ticket(bot)
  await setup_utilitaire(bot)
  await setup_événements(bot)
  await bot.change_presence(status=discord.Status.dnd, activity=maj)
  print(f'Bot connecté')

@bot.event
async def on_ready():
  try:
    synced = await bot.tree.sync()
    print(f"Synced {len(synced)} commande(s)")
  except Exception as e:
    print(e)

  print(f'Bot prêt en tant que {bot.user}')

  await asyncio.sleep(1)
  activity = discord.Activity(type=discord.ActivityType.watching, name=f'Version {Version_Presente}')
  await bot.change_presence(status=discord.Status.online, activity=activity)

@bot.event
async def on_disconnect():
  print(f'Bot deconnecté')

PREVIOUS
The Event 
V V V V V

@bot.event
async def on_command_error(ctx, error):
  if isinstance(error, commands.CommandNotFound):
      await ctx.send("Cette commande n'existe pas.")
  elif isinstance(error, commands.MissingRequiredArgument):
      await ctx.send("Il manque un argument requis pour exécuter cette commande.")
  elif isinstance(error, commands.CommandOnCooldown):
      await ctx.send(f"Cette commande est en cooldown. Réessayez dans {round(error.retry_after, 1)} secondes.")
  elif isinstance(error, commands.NotOwner):
      await ctx.send("Vous n'avez pas la permission d'exécuter cette commande.")
  else:
      await ctx.send(f"Une erreur est survenue lors de l'exécution de la commande : {error}")```
    ...
python discord discord.py
1个回答
0
投票

遗憾的是,对此没有一个完美的答案,因为不和谐只会给你英文的错误消息,而没有太多内容,但我想出了两种方法:

  • 只需让它以英文打印错误即可
  • 将错误翻译成所需的语言

我建议你看看什么NLTK(自然语言工具包) 必须提供。

© www.soinside.com 2019 - 2024. All rights reserved.