首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Discord.py按钮和下拉菜单,没有得到正确的库

Discord.py按钮和下拉菜单,没有得到正确的库
EN

Stack Overflow用户
提问于 2021-07-28 12:53:36
回答 2查看 11.6K关注 0票数 2

修正:添加DiscordComponents(bot)

我试图要么按钮,要么下拉菜单允许用户作出选择。但是,我一直无法使它工作,因为这两种方法最后仍然给我一个组件问题。

错误消息是:第134行,在rps m=等待ctx.send( TypeError: send()获得一个意外的关键字参数'components‘)中

我已经安装了discord_components并导入了它。我对错误信息从何而来感到困惑。

代码语言:javascript
运行
复制
@bot.command(name = "rps")
async def rps(ctx):
    ch1 = ["Rock","Scissors","Paper"]
    comp = random.choice(ch1)

    yet = discord.Embed(title = f"{ctx.author.display_name}'s RPS game", description = "You have yet to select anything")

    win = discord.Embed(title = f"{ctx.author.display_name} Won!", descrption = "Congrats! You won.")

    out = discord.Embed(title = f"{ctx.author.display_name} was too slow.", descrption = "Okay then you were just too slow for me. ")

    lost = discord.Embed(title = f"{ctx.author.display_name} lost", descrption = "Aw darn. Unfortunatly you lost this one.")

    tie = discord.Embed(title = f"{ctx.author.display_name} tied with the bot!", descrption = "Well thats interesting. It was a tie.")


    m = await ctx.send(
        embed=yet,
        components=[Select(placeholder="Select 1", options=[SelectOption(label="Rock", value="Rock"), SelectOption(label="Paper", value="Paper"), SelectOption(label="Scissors", value="Scissors")])]
        ,
    )

    interaction = await bot.wait_for("select_option", check = lambda i: i.component[0].value == "A")
    await interaction.respond(content = f"{interaction.component[0].label} selected!")

我导入了什么以及我的机器人是如何定义的:

代码语言:javascript
运行
复制
import os, csv, random, discord, asyncio, pandas, requests, json, traceback
from discord.ext import commands
from dotenv import load_dotenv
from discord_components import DiscordComponents, Button, Select, SelectOption

load_dotenv()
botToken = os.getenv("DiscBotToken")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-26 08:35:25

我帮不了你,但我可以给你一个工作代码。

代码语言:javascript
运行
复制
import discord
from discord.ext import commands
from discord_components import DiscordComponents, Select, SelectOption, Button,ButtonStyle
from discord.utils import get

bot = commands.Bot(command_prefix='.')

@bot.event
async def on_ready():
DiscordComponents(bot)
print('Connected')

@bot.command()
async def button(ctx):
    embed = discord.Embed(title='Test',description='Test text')

await ctx.send(embed=embed, components=[Button(label='Test', custom_id="test-id", style=ButtonStyle.red)])
interaction = await bot.wait_for(
"button_click", check=lambda inter: inter.custom_id == "test-id")

@bot.event
async def on_button_click(interaction):
    await interaction.respond(type=6)
        await interaction.author.send("Click")

和SelectMenu

代码语言:javascript
运行
复制
import discord
from discord.ext import commands
from discord_components import DiscordComponents, Select, SelectOption, Button, ButtonStyle
from discord.utils import get

bot = commands.Bot(command_prefix='.')

@bot.event
async def on_ready():
    DiscordComponents(bot)
    print('Connected')

@bot.command()
async def sm(ctx):
    if ctx.author.id == 670325098598629377 or 426376741028888576:
        await ctx.send(
    '>>> 'Text',
    components = [
    Select(
        placeholder = 'SelectMenu',
        options = [
            SelectOption(label="SelectMenu1", value="value1"),
            SelectOption(label="SelectMenu2", value="value2"),
            SelectOption(label="SelectMenu3", value="value3"),
            SelectOption(label = "SelectMenu4", value = "value4"),
            SelectOption(label="SelectMenu5", value="value5"),
            SelectOption(label="SelectMenu6", value="value6"),
            SelectOption(label = "SelectMenu7", value = "value7"),
            SelectOption(label = "SelectMenu8", value = "value8")
            ])])

@bot.event
async def on_select_option(interaction):
    if interaction.message.id == 891587821368905728: #Message id(not obligatory)
        await interaction.respond(type=6)
        if interaction.values[0] == "value1":
            await interaction.author.send("Menu 1")
        elif interaction.values[0] == "value2":
            await interaction.author.send("Menu 2")
票数 2
EN

Stack Overflow用户

发布于 2021-10-24 17:47:32

我建议您使用discord.py,而不是其他库/依赖项,如discord_componentsnextcord等。

  • 如果您想检查discord.py v2的视图,请到这里来

https://github.com/Rapptz/discord.py/tree/45d498c1b76deaf3b394d17ccf56112fa691d160/examples/views

  • If您有关于代码的问题或问题,请转到discord.py不和谐服务器

https://discord.gg/dpy和say ?tester#testing通道中访问v2问题通道。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68560781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档