Discord 音乐机器人不排队歌曲 (nextcord)

发布于 2025-01-19 20:20:04 字数 3194 浏览 0 评论 0原文

当我使用play命令时,我的机器人不会排队歌曲,它只是播放它们。我试图在使用Spotify& bot中的soundcloud。我尝试排队歌曲,我无法排队。那么,有人可以帮我吗?我已经检查了wavelink文档,但找不到任何东西。(我是这个库的初学者)

代码:

import nextcord
from nextcord.ext import commands
import wavelink 
import random 
import datetime 
from datetime import datetime
import os

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






@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)

  next_song = vc.queue.get()
  await vc.play(next_song)
  emb = nextcord.Embed(description=f"Now playing {next_song.title}",color=nextcord.Color.magenta())
  emb.set_image(url=next_song.thumbnail)
  await ctx.send(embed=emb)   
  


@bot.command(aliases=["p"])
async def play(ctx,*,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):
    embed=nextcord.Embed(description=f"{ctx.author.mention}: No song(s) are playing.",color=nextcord.Color.blue())
    return await ctx.send(embed=embed)

  
  
  else:
    vc:wavelink.Player = ctx.voice_client
  if vc.queue.is_empty and vc.is_playing:
   await vc.play(search)
   embe = nextcord.Embed(description=f"Now playing [{search.title}]({search.uri}) ",color=nextcord.Color.magenta())
  
   embe.set_image(url=search.thumbnail)
   await ctx.send(embed=embe)
  else:
    await vc.queue.put_wait(search)
    emb = nextcord.Embed(description=f"Added [{search.title}]({search.uri}) to the queue.",color=nextcord.Color.magenta())
  
    emb.set_image(url=search.thumbnail)
    await ctx.send(embed=emb)
  vc.ctx = ctx
  setattr(vc,"loop",False)
  


    
@bot.command()
async def queue(ctx):
  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):
    embed=nextcord.Embed(description=f"{ctx.author.mention}: No song(s) are playing.",color=nextcord.Color.blue())
    return await ctx.send(embed=embed)
  
  else:  
     vc:wavelink.Player = ctx.voice_client

  if vc.queue.is_empty:
    emb = nextcord.Embed(description=f"{ctx.author.mention}: The queue is empty. Try adding songs.",color=nextcord.Color.red())
    return await ctx.send(embed=emb)
  lp = nextcord.Embed(title="Queue",color=nextcord.Color.blue())
  queue = vc.queue.copy()
  song_count = 0
  for song in queue:
    song_count += 1
    lp.add_field(name=f"[{song_count}] Song",value=f"{song.title}")
    return await ctx.send(embed=lp)




  

bot.run("TOKEN IS HERE JUST NOT LEAKING IT")

错误:

Traceback (most recent call last):
  File "/home/runner/Solved-Music/venv/lib/python3.8/site-packages/nextcord/client.py", line 417, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 181, in on_wavelink_track_end
    song_count += 1
  File "/home/runner/Solved-Music/venv/lib/python3.8/site-packages/wavelink/queue.py", line 212, in get
    raise QueueEmpty("No items in the queue.")
wavelink.errors.QueueEmpty: No items in the queue.

My bots doesn't queue the songs when i use the play command, it just plays them. Im trying to get all my commands to work before using spotify & soundcloud in the bot.So, when i use the play command & I try to queue the songs, I cannot queue them. So, can anyone help me ? I have checked the wavelink doc but I couldn't find anything.. (im a beginner with this library)

Code:

import nextcord
from nextcord.ext import commands
import wavelink 
import random 
import datetime 
from datetime import datetime
import os

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






@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)

  next_song = vc.queue.get()
  await vc.play(next_song)
  emb = nextcord.Embed(description=f"Now playing {next_song.title}",color=nextcord.Color.magenta())
  emb.set_image(url=next_song.thumbnail)
  await ctx.send(embed=emb)   
  


@bot.command(aliases=["p"])
async def play(ctx,*,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):
    embed=nextcord.Embed(description=f"{ctx.author.mention}: No song(s) are playing.",color=nextcord.Color.blue())
    return await ctx.send(embed=embed)

  
  
  else:
    vc:wavelink.Player = ctx.voice_client
  if vc.queue.is_empty and vc.is_playing:
   await vc.play(search)
   embe = nextcord.Embed(description=f"Now playing [{search.title}]({search.uri}) ",color=nextcord.Color.magenta())
  
   embe.set_image(url=search.thumbnail)
   await ctx.send(embed=embe)
  else:
    await vc.queue.put_wait(search)
    emb = nextcord.Embed(description=f"Added [{search.title}]({search.uri}) to the queue.",color=nextcord.Color.magenta())
  
    emb.set_image(url=search.thumbnail)
    await ctx.send(embed=emb)
  vc.ctx = ctx
  setattr(vc,"loop",False)
  


    
@bot.command()
async def queue(ctx):
  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):
    embed=nextcord.Embed(description=f"{ctx.author.mention}: No song(s) are playing.",color=nextcord.Color.blue())
    return await ctx.send(embed=embed)
  
  else:  
     vc:wavelink.Player = ctx.voice_client

  if vc.queue.is_empty:
    emb = nextcord.Embed(description=f"{ctx.author.mention}: The queue is empty. Try adding songs.",color=nextcord.Color.red())
    return await ctx.send(embed=emb)
  lp = nextcord.Embed(title="Queue",color=nextcord.Color.blue())
  queue = vc.queue.copy()
  song_count = 0
  for song in queue:
    song_count += 1
    lp.add_field(name=f"[{song_count}] Song",value=f"{song.title}")
    return await ctx.send(embed=lp)




  

bot.run("TOKEN IS HERE JUST NOT LEAKING IT")

Error:

Traceback (most recent call last):
  File "/home/runner/Solved-Music/venv/lib/python3.8/site-packages/nextcord/client.py", line 417, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 181, in on_wavelink_track_end
    song_count += 1
  File "/home/runner/Solved-Music/venv/lib/python3.8/site-packages/wavelink/queue.py", line 212, in get
    raise QueueEmpty("No items in the queue.")
wavelink.errors.QueueEmpty: No items in the queue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海的爱人是光 2025-01-26 20:20:04

您需要将代码: vc:wavelink.Player = ctx.voice_client if vc.queue.is_empty and vc.is_playing: 更改为代码 vc:wavelink.Player = ctx.voice_client if vc.queue.is_empty 和**不是** vc.is_playing:

you need to change your code: vc:wavelink.Player = ctx.voice_client if vc.queue.is_empty and vc.is_playing: to the code vc:wavelink.Player = ctx.voice_client if vc.queue.is_empty and **not** vc.is_playing:

难理解 2025-01-26 20:20:04

如果您遵循其他建议并将其添加到声明中。

if vc.queue.is_empty and not vc.is_playing:
   await vc.play(search)

在我的系统上,机器人会加载一首歌以添加到que并跳过

await vc.play(search)

它,因为我的系统和函数的原因:is_playing不要一起播放。

is_playing 

await vc.play(search)

始终返回正确,因此如果您有同样的问题,请跳过

if vc.queue.is_empty and vc.is_connected and vc._source is None:

    await vc.play(search)

:现在,您的机器人在使用Play命令时会播放,并且会排队未来的歌曲。

if you follow the other advice and add a not to the statement.

if vc.queue.is_empty and not vc.is_playing:
   await vc.play(search)

on my system the bot would load add a song to que and skip

await vc.play(search)

it turns out forwhat ever reason my system and the function: is_playing dont play nice together.

is_playing 

always returned true thus skipping the line

await vc.play(search)

if you are having the same issue you can try:

if vc.queue.is_empty and vc.is_connected and vc._source is None:

    await vc.play(search)

now your bot will play when using the play command and it will queue future songs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文