【问题标题】:Discord.py ticket systemDiscord.py 票务系统
【发布时间】:2022-02-12 00:46:23
【问题描述】:

我已经开始使用 discord.py 制作 Ticket bot,但我的代码中有一个错误,我不知道如何解决它。嵌入并对消息工作做出反应,但是当我尝试做出反应时,一切都崩溃了。这是我的错误:

Ignoring exception in command ticket:
Traceback (most recent call last):
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\Users\HP\Desktop\222\dscbot-dscbot.py", line 150, in ticket
    if reaction == '????':
NameError: name 'reaction' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'reaction' is not defined

我希望有人知道如何解决这个问题,这是我当前的代码:

import discord
from discord.ext import commands
from discord.utils import get
from discord import Embed, Color
import DiscordUtils
import os
from discord.ext.commands import has_permissions, MissingPermissions
import json




intents = discord.Intents.default()

intents.members = True
client = commands.Bot(command_prefix = "-", intents = intents)
client.remove_command("help")




@client.command(pass_context=True)
async def ticket(ctx):
    guild = ctx.guild
    embed = discord.Embed(
        title = 'Ticket system',
        description = 'React ???? to make a ticket.',
        color = 0
    )

    embed.set_footer(text="ticket system")

    msg = await ctx.send(embed=embed)
    await msg.add_reaction("????")
    reaction = await msg.fetch_message(msg.id)


    await client.wait_for("reaction_add")



    await client.wait_for("reaction_add")

    if reaction == '????':
        await guild.create_text_channel(name=f'ticket - {reaction.author.name}')

ps:我是 discord.py 的新手

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    reaction = await msg.fetch_message(msg.id) 只是给你msg。你需要从await client.wait_for("reaction_add")得到反应

    看看wait_for。您实施检查(反应在哪里)以查看反应是否是邮箱。

    如果反应是邮箱,您需要创建一个创建频道的新函数,然后将其传递给client.wait_for

    def check(reaction, user):
        return str(reaction) == '?' and ctx.author == user
    
    await client.wait_for("reaction_add", check=check)
    await guild.create_text_channel(name=f'ticket - {ctx.author}')
    

    【讨论】:

    • 您能解释一下如何做到这一点吗?正如我所说,我是 dsc.py 的新手,我只知道基础知识。
    • @MatanKa。我将成品添加到我的答案中
    • File "c:\Users\HP\Desktop\222\dscbot-dscbot.py", line 151 await guild.create_text_channel(name=f'ticket - {reaction.author.name}') ^ SyntaxError: 'await' outside async function PS C:\Users\HP\Desktop\222> 你知道怎么解决这个问题吗?
    • @MatanKa。对不起,我搞砸了。我编辑了答案,现在一切都应该很好了!
    【解决方案2】:

    您没有定义reaction,这就是引发此错误的原因。 查看this post,了解如何从消息中获取反应。

    【讨论】:

    • 我不知道我做得好不好,但这次我得到了:Ignoring exception in command ticket: Traceback (most recent call last): File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped ret = await coro(*args, **kwargs) File "c:\Users\HP\Desktop\222\dscbot-dscbot.py", line 145, in ticket reaction = await msg.fetch_message(msg.id) AttributeError: 'Message' object has no attribute 'fetch_message'
    • 您能否更新您的问题以反映您所做的更改?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多