【问题标题】:Why does Slash Commands in Pycord not give response in discord?为什么 Pycord 中的斜线命令没有给出不和谐的响应?
【发布时间】:2022-11-13 16:16:26
【问题描述】:

我正在尝试制作一个允许用户与其他用户一起玩游戏的不和谐机器人。我正在使用pycord 和python 版本3.10.8

这是我的代码:-

from dotenv import load_dotenv
load_dotenv()
import discord,os

global TOKEN,bot,main,games

TOKEN = os.getenv("DISCORD_TOKEN")
bot = discord.Bot()
main = bot.create_group("nisticks","Nisticks Commands")
games = main.create_subgroup("games","Nisticks Games Commands")

@games.command(description="Tic Tac Toe")
async def ttt(ctx):
    await ctx.channel.respond("gg")


if __name__ == '__main__':
    bot.add_application_command(main)
    bot.run(TOKEN)

在不和谐中运行斜杠命令后:-

它显示了这一点:-

这个错误出现在python中:-

Ignoring exception in on_interaction
Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 124, in wrapped
    ret = await coro(arg)
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 1310, in _invoke
    await command.invoke(ctx)
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 372, in invoke
    await self.prepare(ctx)
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 292, in prepare
    if not await self.can_run(ctx):
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 390, in can_run
    local_check = cog._get_overridden_method(cog.cog_check)
AttributeError: '_MissingSentinel' object has no attribute '_get_overridden_method'

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

Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\discord\bot.py", line 1114, in invoke_application_command
    await ctx.command.invoke(ctx)
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 375, in invoke
    await injected(ctx)
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 132, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: AttributeError: '_MissingSentinel' object has no attribute '_get_overridden_method'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\discord\client.py", line 377, in _run_event
    await coro(*args, **kwargs)
  File "C:\Python310\lib\site-packages\discord\bot.py", line 1167, in on_interaction
    await self.process_application_commands(interaction)
  File "C:\Python310\lib\site-packages\discord\bot.py", line 848, in process_application_commands
    await self.invoke_application_command(ctx)
  File "C:\Python310\lib\site-packages\discord\bot.py", line 1118, in invoke_application_command
    await ctx.command.dispatch_error(ctx, exc)
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 418, in dispatch_error
    local = cog.__class__._get_overridden_method(cog.cog_command_error)
AttributeError: type object '_MissingSentinel' has no attribute '_get_overridden_method'
Ignoring exception in command nisticks games:
Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 124, in wrapped
    ret = await coro(arg)
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 1310, in _invoke
    await command.invoke(ctx)
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 372, in invoke
    await self.prepare(ctx)
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 292, in prepare
    if not await self.can_run(ctx):
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 390, in can_run
    local_check = cog._get_overridden_method(cog.cog_check)
AttributeError: '_MissingSentinel' object has no attribute '_get_overridden_method'

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

Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\discord\bot.py", line 1114, in invoke_application_command
    await ctx.command.invoke(ctx)
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 375, in invoke
    await injected(ctx)
  File "C:\Python310\lib\site-packages\discord\commands\core.py", line 132, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: AttributeError: '_MissingSentinel' object has no attribute '_get_overridden_method'

与此相关的另一个Stackoverflow Question 有答案可以运行:-

pip install git+https://github.com/Pycord-Development/pycord

但这并没有什么不同。任何帮助,将不胜感激!

【问题讨论】:

    标签: python discord.py pycord


    【解决方案1】:

    那是因为你回应。

    @games.command(description="Tic Tac Toe")
    async def ttt(ctx):
        print(await ctx.__dict__)
    

    print 打印到 Python 控制台。它不会向 Discord 发送任何内容。您将在服务器端看到它,但在客户端看不到。你也不能await字典;你必须await一个等待的对象(例如协程)。

    要回复 Discord,请使用 ctx.respond。那个功能产生一个协程,所以我们等待那个函数调用。要将对象的字典打印到 Discord 通道,请使用

    await ctx.respond(str(ctx.__dict__))
    

    【讨论】:

    • 很抱歉,我在代码中有这个,但它没有工作(给出同样的错误)await ctx.channel.respond("gg") 所以我用打印语句替换它
    猜你喜欢
    • 2022-06-24
    • 2022-06-20
    • 1970-01-01
    • 2022-10-17
    • 2022-10-20
    • 2022-10-05
    • 2022-10-13
    • 2021-11-20
    • 2021-09-27
    相关资源
    最近更新 更多