Lavalink 音乐机器人不想播放音乐

Lavalink music bot doesn't want to play music

提问人:Domi 提问时间:11/15/2023 更新时间:11/15/2023 访问量:19

问:

音乐机器人拒绝播放音乐。它在代码后给出以下错误,我将很乐意提供任何帮助。

(https://i.stack.imgur.com/dVZjW.png)

import discord
from discord.ext import commands
from lavalink import Lavalink

intents = discord.Intents.default()
intents.music = True

bot = commands.Bot(command_prefix="!", intents=intents)
bot.remove_command("help")

bot.lavalink = Lavalink(bot)
bot.lavalink.add_node('localhost', 2333, 'youshallnotpass', 'na', 'music-node')

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name} ({bot.user.id})')
    print('------')

@bot.command()
async def join(ctx):
    # Подключаем бота к голосовому каналу
    channel = ctx.author.voice.channel
    await ctx.guild.change_voice_state(channel=channel, self_mute=False, self_deaf=False)

@bot.command()
async def play(ctx, *, query):
    # Воспроизведение музыки
    player = bot.lavalink.player_manager.get(ctx.guild.id)

    if not player.is_connected:
        channel = ctx.author.voice.channel
        await ctx.guild.change_voice_state(channel=channel, self_mute=False, self_deaf=False)

    tracks = await bot.lavalink.get_tracks(query)

    if not tracks:
        return await ctx.send('Треки не найдены')

    embed = discord.Embed(title="Выберите трек для воспроизведения", color=0x00ff00)

    for i, track in enumerate(tracks, start=1):
        embed.add_field(name=f'{i}. {track["info"]["title"]}', value=f'Duration: {track["info"]["length"] // 1000}s')

    await ctx.send(embed=embed)

    def check(m):
        return m.author == ctx.author and m.channel == ctx.channel and m.content.isdigit()

    response = await bot.wait_for('message', check=check)

    track = tracks[int(response.content) - 1]

    if not player.is_playing:
        player.store('channel', ctx.channel.id)
        await bot.lavalink.connect(ctx.guild.id, ctx.channel.id)
        await ctx.send('Подключено к голосовому каналу')

    player.add(requester=ctx.author.id, track=track)
    await ctx.send(f'Добавлено в очередь: {track["info"]["title"]}')

@bot.command()
async def skip(ctx):
    # Пропустить трек
    player = bot.lavalink.player_manager.get(ctx.guild.id)

    if not player.is_playing:
        return await ctx.send('Нет активного трека для пропуска')

    await ctx.send('Пропущено')
    player.skip()

@bot.event
async def on_voice_state_update(member, before, after):
    # Отключение бота от голосового канала, если все пользователи покинули канал
    if not member.bot and after.channel is None:
        guild_id = before.channel.guild.id
        player = bot.lavalink.player_manager.get(guild_id)

        if player.is_connected and not any(member.voice for member in after.channel.members):
            await bot.lavalink.disconnect(guild_id)
            await bot.lavalink._nodes['music-node'].destroy(guild_id)
            await player.destroy()

bot.run("token")

 

ImportError:无法从“lavalink”导入名称“Lavalink”(C:\Users\comp\AppData\Local\Programs\Python\Python\Python310\lib\site-packages\lavalink_init_.py)

我已经安装了所有东西:lavalink.jar 等等 Java 17

python-3.x discord.py

评论

0赞 ewokx 11/15/2023
github.com/Devoxin/Lavalink.py/blob/master/examples/music.py,您似乎不需要导入 Lavalink。

答: 暂无答案