【问题标题】:Why isn't this discord slash command working?为什么这个不和谐的斜线命令不起作用?
【发布时间】:2021-10-17 15:25:58
【问题描述】:

我正在尝试使用discord_slash 包(我相信现在称为discord_interactions)使用斜杠命令升级我的所有不和谐机器人。但是,当我运行以下代码时,每次我使用斜杠命令时,它都会在 discord 上显示“此交互失败”。调试器和一些调试文本也证明与命令关联的函数根本没有运行。这是我的代码(我认为我对按钮所做的事情是错误的,但在这种情况下这无关紧要): 为什么这种交互会失败?


import discord
import discord_slash
from discord.types.components import ButtonComponent
from discord_slash import SlashCommand
from discord.ui import Button

bot = discord.Client(intents=discord.Intents.all())
slash = SlashCommand(bot, sync_commands=True)


@bot.event
async def on_ready():
    print("we a'goin!")


guild_ids = ['guild id']
normalPollOptions = [
    {
        "name": "poll_title",
        "description": "Title of the Poll",
        "type": 3,
        "required": True
    },
    {
        "name": "option_1",
        "description": "First choice of the poll",
        "type": 3,
        "required": True
    },
    {
        "name": "option_2",
        "description": "Second choice of the poll",
        "type": 3,
        "required": True
    },
    {
        "name": "option_3",
        "description": "Third choice of the poll",
        "type": 3,
        "required": False
    },
    {
        "name": "option_4",
        "description": "Fourth choice of the poll",
        "type": 3,
        "required": False
    },
    {
        "name": "option_5",
        "description": "Fifth choice of the poll",
        "type": 3,
        "required": False
    },
    {
        "name": "option_6",
        "description": "Sixth choice of the poll",
        "type": 3,
        "required": False
    },
    {
        "name": "option_7",
        "description": "Seventh choice of the poll",
        "type": 3,
        "required": False
    },
    {
        "name": "option_8",
        "description": "Eighth choice of the poll",
        "type": 3,
        "required": False
    },
    {
        "name": "option_9",
        "description": "Ninth choice of the poll",
        "type": 3,
        "required": False
    },
    {
        "name": "option_10",
        "description": "Tenth choice of the poll",
        "type": 3,
        "required": False
    }
]


@slash.slash(name="normalpoll", description="Make a normal poll", guild_ids=guild_ids, options=normalPollOptions)
async def _normalpoll(ctx, poll_title, option_1, option_2, option_3=None, option_4=None, option_5=None, option_6=None, option_7=None, option_8=None, option_9=None, option_10=None):
    await ctx.send(content="hi!")
    optionParam = [option_1, option_2, option_3, option_4, option_5, option_6, option_7, option_8, option_9, option_10]
    options = [i for i in optionParam if not i is None]
    buttonList = [Button(label=i) for i in options]
    await ctx.send(components=buttonList)


bot.run('censored bot token')

谢谢!

【问题讨论】:

  • 是所有 10 个用户输入类型选项,就像用户手动输入他们的选择一样,还是像 where do u live? a.usa b.uk c.asis d.australia 这样的相同选项的一些选择是你的 10 个选项,比如美国,英国在这里?

标签: python-3.x discord.py


【解决方案1】:

这些选项实际上应该采用不同的格式才能使它们起作用

您必须使用 discord_slash.utils.manage_commands 中的 create_option 函数来为您的命令创建一个选项

import discord
import discord_slash
from discord.types.components import ButtonComponent
from discord_slash.utils.manage_commands import create_option
from discord_slash import SlashCommand
from discord.ui import Button

bot = discord.Client(intents=discord.Intents.all())
slash = SlashCommand(bot, sync_commands=True)


@bot.event
async def on_ready():
    print("we a'goin!")


guild_ids = ['guild id']
normalPollOptions = [
    create_option(
        name="poll_title",
        description="Title of the Poll",
        type=3,
        required=True
    ),
    create_option(
        name="option_1",
        description="First choice of the poll",
        type=3,
        required=True
    ),
    create_option(
        name="option_2",
        description="Second choice of the poll",
        type=3,
        required=True
    ),
    create_option(
        name="option_3",
        description="Third choice of the poll",
        type=3,
        required=False
    ),
    create_option(
        name="option_4",
        description="Fourth choice of the poll",
        type=3,
        required=False
    ),
    create_option(
        name="option_5",
        description="Fifth choice of the poll",
        type=3,
        required=False
    ),
    create_option(
        name="option_6",
        description="Sixth choice of the poll",
        type=3,
        required=False
    ),
    create_option(
        name="option_7",
        description="Seventh choice of the poll",
        type=3,
        required=False
    ),
    create_option(
        name="option_8",
        description="Eighth choice of the poll",
        type=3,
        required=False
    ),
    create_option(
        name="option_9",
        description="Ninth choice of the poll",
        type=3,
        required=False
    ),
    create_option(
        name="option_10",
        description="Tenth choice of the poll",
        type=3,
        required=False
    )
]

@slash.slash(name="normalpoll", description="Make a normal poll", guild_ids=guild_ids, options=normalPollOptions)
async def _normalpoll(ctx, poll_title, option_1, option_2, option_3=None, option_4=None, option_5=None, option_6=None, option_7=None, option_8=None, option_9=None, option_10=None):
    await ctx.send(content="hi!")
    optionParam = [option_1, option_2, option_3, option_4, option_5, option_6, option_7, option_8, option_9, option_10]
    options = [i for i in optionParam if not i is None]
    buttonList = [Button(label=i) for i in options]
    await ctx.send(components=buttonList)


bot.run('censored bot token')

告诉我是否适合你...

【讨论】:

  • 它没有用
猜你喜欢
  • 2021-09-27
  • 2022-10-20
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 2022-11-13
  • 2021-07-26
相关资源
最近更新 更多