Pycord/discord.py 斜杠命令未显示

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

我以前从未遇到过这个问题。我所有的其他机器人都工作得很好,我确信这不是我的源代码。当我启动我的机器人时,我收到以下错误消息:

Traceback (most recent call last):
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\client.py", line 377, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\bot.py", line 1164, in on_connect
    await self.sync_commands()
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\bot.py", line 719, in sync_commands
    registered_commands = await self.register_commands(
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\bot.py", line 588, in register_commands
    data = [cmd["command"].to_dict() for cmd in filtered_deleted]
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\bot.py", line 588, in <listcomp>
    data = [cmd["command"].to_dict() for cmd in filtered_deleted]
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\commands\core.py", line 844, in to_dict
    "options": [o.to_dict() for o in self.options],
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\commands\core.py", line 844, in <listcomp>
    "options": [o.to_dict() for o in self.options],
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\commands\options.py", line 318, in to_dict
    "type": self.input_type.value,
AttributeError: 'NoneType' object has no attribute 'value'

错误发生后,机器人启动,但在服务器中不显示任何斜杠命令。我正在使用这个邀请链接:

https://discord.com/api/oauth2/authorize?client_id=REDACTED&permissions=8&scope=bot%20applications.commands

这是我的机器人意图:

我的代码:

import discord
from discord.ext import commands
import os

intents=discord.Intents.all()
client=commands.Bot(intents=intents)

@client.event
async def on_ready():
    print(f'\n\nSuccessfully logged into Discord as "{client.user}"\nAwaiting user input...')
    await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name="all by myself..."))

@client.slash_command()
async def lol(interaction: discord.Interaction, ctx: discord.ApplicationContext):
    await interaction.response.send_message("LOL")

client.run(os.environ.get("TOKEN"))

如有任何帮助,我们将不胜感激!对于我的一生,我无法弄清楚发生了什么。

python python-3.x discord.py pycord
2个回答
2
投票

您的交互参数错误。应该是:

import discord
from discord.ext import commands
import os

intents=discord.Intents.all()
client=commands.Bot(intents=intents)

@client.event
async def on_ready():
    print(f'\n\nSuccessfully logged into Discord as "{client.user}"\nAwaiting user input...')
    await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name="all by myself..."))

@client.slash_command()
async def lol(ctx: discord.ApplicationContext):
    await ctx.response.send_message("LOL")

client.run(os.environ.get("TOKEN"))

将您的代码与我的更改一起使用可以使其运行而不会引发异常,并且我能够调用

lol
斜杠命令。

斜线命令文档。斜杠命令具有

ctx
(ApplicationContext) 参数,然后其他参数通常是斜杠命令选项。


0
投票

您应该尝试使用discord.app_commands库。

这是您更新的代码:

import discord
from discord import app_commands
import os

class prepareclient(discord.Client):
    def __init__(self): super().__init__(intents=discord.Intents.all())
    async def on_ready():
        print(f'\n\nSuccessfully logged into Discord as "{client.user}"\nAwaiting user input...')
        await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name="all by myself..."))
        await tree.sync()

client = prepareclient()
tree = app_commands.CommandTree(client)

@tree.command(name="lol", description=f"lol")
async def lol(inc: discord.Interaction):
    await inc.response.send_message("LOL")
    return

client.run(os.environ.get("TOKEN"))

我希望这有帮助。

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