首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ctx = player.ctx AttributeError:“Player”对象没有属性“ctx”

ctx = player.ctx AttributeError:“Player”对象没有属性“ctx”
EN

Stack Overflow用户
提问于 2022-08-14 06:34:31
回答 1查看 172关注 0票数 0

我在做音乐机器人,但我得到了

代码语言:javascript
复制
AttributeError: 'Player' object has no attribute 'ctx'

当我做>>play音乐时,另一种音乐正在播放(应该是为了排队)

全误差

代码语言:javascript
复制
Bot Online
Node <8d0e8f15e4723f72> is ready
Ignoring exception in on_wavelink_track_end
Traceback (most recent call last):
  File "/home/catiganvien3/.local/lib/python3.9/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "/home/catiganvien3/scripts/WaveLinkBot/main.py", line 28, in on_wavelink_track_end
    ctx = player.ctx
AttributeError: 'Player' object has no attribute 'ctx'

完整代码是

代码语言:javascript
复制
import wavelink
import discord
from discord.ext import commands
import asyncio
import os

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


@bot.event
async def on_ready():
    print('Bot Online')
    bot.loop.create_task(node_connect())


@bot.event
async def on_wavelink_node_ready(node: wavelink.Node):
    print(f'Node <{node.identifier}> is ready')


async def node_connect():
    await bot.wait_until_ready()
    await wavelink.NodePool.create_node(bot=bot, host='ash-01.thermalhosting.com', port=2008, password='ASH-01')


@bot.event
async def on_wavelink_track_end(player: wavelink.Player, track: wavelink.Track, reason):
    ctx = player.ctx
    vc: player = ctx.voice_client
    if vc.loop:
        return await vc.play(track)
    try:
        next_song = vc.queue.get()
        await vc.play(next_song)
        embed = discord.Embed(
            title="Now playing", description=f'Song name: **{next_song.title}**\nSong author: **{next_song.author}**')
        await ctx.send(embed=embed)
    except:
        # An exception when after the track end, the queue is now empty. If you dont do this, it will get error.
        await vc.stop()
@bot.command()
async def play(ctx: commands.Context, *, search: wavelink.YouTubeTrack):
    if not ctx.voice_client:
        vc: wavelink.Player = await ctx.author.voice.channel.connect(cls=wavelink.Player)
    elif not getattr(ctx.author.voice, 'channel', None):
        await ctx.send(f'Connect to a voice channel to use this command!')
    else:
        vc: wavelink.Player = ctx.voice_client
    song_name = search.title
    song_author = search.author
    if song_author == None:
        song_author = 'Unknown'
    # embed = discord.Embed(title="Added to queue", description=f'Song name: **{song_name}**\nSong author: **{song_author}**')
    # await vc.play(search)
    # await ctx.send(embed=embed)
    if vc.queue.is_empty and vc.is_playing:
        await vc.play(search)
        embed = discord.Embed(title="Playing", description=f'Song name: **{song_name}**\nSong author: **{song_author}**')
        return await ctx.send(embed=embed)
    else:
        await vc.queue.put_wait(search)
        embed = discord.Embed(title="Added to queue", description=f'Song name: **{song_name}**\nSong author: **{song_author}**')
        return await ctx.send(embed=embed)
    vc.ctx = ctx
    setattr(vc, "loop", False)
@bot.command()
async def pause(ctx: commands.Context):
    if not ctx.voice_client:
        return await ctx.send('Im not playing any music!')
    elif not getattr(ctx.author.voice, 'channel', None):
        await ctx.send(f'Connect to a voice channel to use this command!')
    else:
        vc: wavelink.Player = ctx.voice_client
    await vc.pause()
    await ctx.send(f'Paused!')

@bot.command()
async def resume(ctx: commands.Context):
    if not ctx.voice_client:
        return await ctx.send('Im not playing any music!')
    elif not getattr(ctx.author.voice, 'channel', None):
        await ctx.send(f'Connect to a voice channel to use this command!')
    else:
        vc: wavelink.Player = ctx.voice_client
    await vc.resume()
    await ctx.send(f'Resumed!')

@bot.command()
async def stop(ctx: commands.Context):
    if not ctx.voice_client:
        return await ctx.send('Im not playing any music!')
    elif not getattr(ctx.author.voice, 'channel', None):
        await ctx.send(f'Connect to a voice channel to use this command!')
    else:
        vc: wavelink.Player = ctx.voice_client
    await vc.stop()
    await ctx.send(f'Stopped!')

@bot.command()
async def disconnect(ctx: commands.Context):
    if not ctx.voice_client:
        return await ctx.send('Im not playing any music!')
    elif not getattr(ctx.author.voice, 'channel', None):
        await ctx.send(f'Connect to a voice channel to use this command!')
    else:
        vc: wavelink.Player = ctx.voice_client
    await vc.disconnect()
    await ctx.send('Bye!')

@bot.command()
async def loop(ctx: commands.Context):
    if not ctx.voice_client:
        return await ctx.send('Im not playing any music!')
    elif not getattr(ctx.author.voice, 'channel', None):
        await ctx.send(f'Connect to a voice channel to use this command!')
    else:
        vc: wavelink.Player = ctx.voice_client
    try:
        vc.loop = True
    except Exception:
        setattr(vc, "loop", False)
    if vc.loop:
        return await ctx.send('Loop is now enabled!')
    else:
        vc.loop = False
        return await ctx.send('Loop is now disabled')

@bot.command()
async def queue(ctx: commands.Context):
    if not ctx.voice_client:
        return await ctx.send('Im not playing any music!')
    elif not getattr(ctx.author.voice, 'channel', None):
        await ctx.send(f'Connect to a voice channel to use this command!')
    else:
        vc: wavelink.Player = ctx.voice_client

    if vc.queue.is_empty:
        return await ctx.send('Queue is empty!')
    embed = discord.Embed(title="Queue")
    queue = vc.queue.copy()
    song_count = 0
    for song in queue:
        song_count += 1
        embed.add_field(name=f"Song number {song_count}", value=f"{song.title}")

    return await ctx.send(embed=embed)
bot.run('secret')
EN

回答 1

Stack Overflow用户

发布于 2022-10-02 23:40:42

如果我是对的,应该是一个版本问题。您的代码没有什么问题。,直到我重新安装了小波,我才有这个值,所以我更希望这是一个版本问题,我不知道哪个版本有这个ctx值,但是如果我找到正确的版本,我会回答您!

编辑

player.ctx的版本是1.2.5,我不确定1.3.0是否还有player.ctx,但我做的是1.2.5,因为它是一个更稳定的版本(对我来说)。

安装

python3 -m pip install wavelink==1.2.5python -m pip install wavelink==1.2.5py -m pip install wavelink==1.2.5,取决于您的机器。您也可以尝试1.2.3版本。

重要

也可能是,bot在调用中停留时间太长,和小波无法从它中提取ctx。

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

https://stackoverflow.com/questions/73349559

复制
相关文章

相似问题

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