从“不和谐”中导入名称“选择”时出现问题

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

启动时会写入错误:

Traceback (most recent call last):
  File "c:\Users\name\Desktop\Discord\main.py", line 4, in <module>
    from discord import Select, SelectOption, ActionRow
ImportError: cannot import name 'Select' from 'discord'

由于学习原因,我已经很长时间没有编程了,现在我不知道如何修复这个错误,请帮忙。我将不胜感激

这是代码本身:

import discord
from discord.ext import commands
from discord import Select, SelectOption, ActionRow

intents = discord.Intents.default()
intents.members = True
intents.message_content = True

bot = commands.Bot(command_prefix='!', intents=intents)

intents.typing = False
intents.presences = False

users = {}

@bot.event
async def on_ready():
    print(f'Вошел в систему как {bot.user}')

@bot.command()
async def start(ctx):
    user_id = ctx.author.id
    if user_id not in users:
        users[user_id] = {
            'games': 0,
            'currency': 0
        }
        embed = discord.Embed(
            title="Успешная регистрация",
            description="Вы успешно зарегистрировались!",
            color=discord.Color.green()
        )
        await ctx.send(embed=embed)
    else:
        embed = discord.Embed(
            title="Регистрация не удалась",
            description="Вы уже зарегистрированы!",
            color=discord.Color.red()
        )
        await ctx.send(embed=embed)

@bot.command()
async def играть(ctx):
    options = [
        SelectOption(label="Угадай число", value="option1"),
        SelectOption(label="Option 2", value="option2"),
        SelectOption(label="Option 3", value="option3"),
    ]

    select = Select(custom_id="select_menu", placeholder="Выберите опцию", options=options)

    action_row = ActionRow(select)

    await ctx.send("Пожалуйста, выберите опцию:", components=[action_row])

@bot.event
async def on_select_option(interaction: discord.Interaction):
    if interaction.custom_id == "select_menu":
        selected_value = interaction.data["values"][0]
        await interaction.response.send_message(f"Вы выбрали: {selected_value}", ephemeral=True)

@bot.command()
@commands.has_role('Chapter [10]')
async def money(ctx, action, user: discord.Member, amount: int):
    user_id = user.id
    if user_id in users:
        if action == 'выдать':
            users[user_id]['currency'] += amount
            await ctx.send(f'Вы выдали {amount} валюты пользователю {user.name}')
        elif action == 'забрать':
            if users[user_id]['currency'] >= amount:
                users[user_id]['currency'] -= amount
                await ctx.send(f'Вы забрали {amount} валюты у пользователя {user.name}')
            else:
                await ctx.send('У пользователя недостаточно валюты!')
        else:
            await ctx.send('Недопустимое действие!')
    else:
        await ctx.send('Пользователь не зарегистрирован!')

@bot.command()
async def info(ctx):
    user_id = ctx.author.id
    if user_id in users:
        games = users[user_id]['games']
        currency = users[user_id]['currency']
        registration_date = users[user_id]['registration_date']
        embed = discord.Embed(title='Статистика', color=discord.Color.blue())
        embed.add_field(name='Количество игр', value=games, inline=False)
        embed.add_field(name='Количество валюты', value=currency, inline=False)
        embed.add_field(name='Дата регистрации', value=registration_date, inline=False)
        await ctx.send(embed=embed)
    else:
        await ctx.send('Вы не зарегистрированы!')

bot.run('TOKEN')

我在网上尝试了很多方法,但都没有帮助,我已经坐了两个多小时搜索信息,但不知道如何解决它,尽管我在半年前写了 + -类似的代码并且有效

discord discord.py
1个回答
0
投票

只需从 discord.ui 导入 Select 即可:

from discord.ui import Select
© www.soinside.com 2019 - 2024. All rights reserved.