【问题标题】:how to make your bot send what message the user send when an error happens如何让您的机器人在发生错误时发送用户发送的消息
【发布时间】:2022-09-28 00:59:11
【问题描述】:

我试图编写错误发生时该怎么做的代码,但是当错误实际发生时我总是会得到这个

error found by: gHoSt FaCe#xxxx, in: admins-chat,  the message was: <Message id=1023531531035230208 channel=<TextChannel id=1019911370512289802 name=\'admins-chat\' position=22 nsfw=False news=False category_id=1019911233077526578> type=<MessageType.default: 0> author=<Member id=593874348524175405 name=\'gHoSt FaCe\' discriminator=\'2856\' bot=False nick=None guild=<Guild id=744535554036858901 name=\'MJSH21\' shard_id=0 chunked=True member_count=53>> flags=<MessageFlags value=0>>, the error: Member \"test\" not found.

代码:

@client.event
async def on_command_error(ctx, error):
    channel = client.get_channel(xxxxxxxx)
    await channel.send(f\'error found by: {str(ctx.author)}, in: {str(ctx.channel)},  the message was: {str(ctx.message)}, the error: {str(error)}\')
    await ctx.send(f\"An error occured: {str(error)} \")

每次收到消息时,它都会发送很多奇怪的东西 它仍然有效,但我怎样才能删除奇怪的东西,只发送消息而不是所有那些奇怪的东西

进口:

from discord.ext import commands
import discord.member
from dotenv import load_dotenv
import discord
import os
import time
from discord.utils import get
from discord.ext.commands import has_permissions, guild_only, Bot

    标签: python discord.py bots message custom-error-handling


    【解决方案1】:

    你把ctx.message单独放了,这就是返回所有奇怪的东西的原因
    为了删除奇怪的东西,你只需添加.content 使其成为ctx.message.content

    此外,您不需要将任何内容转换为字符串,没有它也可以工作

    代码将如下所示

    @bot.event
    async def on_command_error(ctx:commands.Context, error):
        channel = bot.get_channel(xxxxxxxx)
        await channel.send(f'error found by: {ctx.author}, in: {ctx.channel},  the message was: {ctx.message.content}, the error: {error}')
        await ctx.send(f"An error occured: {str(error)} ")
    

    最好将:commands.Context 添加到ctx 以了解您还可以做什么

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 2022-01-03
      • 1970-01-01
      • 2020-09-13
      • 2021-01-08
      • 1970-01-01
      • 2019-06-27
      • 2022-01-23
      • 2021-01-19
      相关资源
      最近更新 更多