这是我当前的Play命令,我知道检查它是否在正确的语音通道中存在问题,但最终我可能会发现,我担心的是我很确定此代码如果人们试图同时在不同的服务器上播放音乐,将无法正常工作。
@bot.command(name='play', help='This command plays songs')
async def play(ctx, *args):
searchterm = " ".join(args)
print(searchterm)
global queue
if not ctx.message.author.voice:
await ctx.send("You are not connected to a voice channel")
return
else:
channel = ctx.message.author.voice.channel
botchannel = ctx.bot.voice_clients
print(channel)
print(botchannel)
await channel.connect()
if 'youtube.com' in searchterm:
queue.append(searchterm)
server = ctx.message.guild
voice_channel = server.voice_client
async with ctx.typing():
player = await YTDLSource.from_url(queue[0], loop=bot.loop)
voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
await ctx.send('**Now playing:** {}'.format(player.title))
del(queue[0])
elif 'youtube.com' not in searchterm:
results = SearchVideos(searchterm, offset=1, mode = "dict", max_results=1).result()
resultlist = results['search_result']
resultdict = resultlist[0]
url = resultdict['link']
queue.append(url)
server = ctx.message.guild
voice_channel = server.voice_client
async with ctx.typing():
player = await YTDLSource.from_url(queue[0], loop=bot.loop)
voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
await ctx.send('**Now playing:** {}'.format(player.title))
del(queue[0])
答案 0 :(得分:1)
您的代码将在多台服务器上运行,但是您将只有一个唯一的队列(因为queue
是列表)。您可以使用服务器ID作为键并以值作为队列来创建字典:
queue = {}
@bot.command(name='play', help='This command plays songs')
async def play(ctx, *, searchterm):
global queue
(...)
if 'youtube.com' in searchterm:
try:
queue[ctx.guild.id].append(searchterm)
except:
queue[ctx.guild.id] = [searchterm]
elif 'youtube.com' not in searchterm:
results = SearchVideos(searchterm, offset=1, mode = "dict", max_results=1).result()
url = results['search_result'][0]['link']
try:
queue[ctx.guild.id].append(url)
except:
queue[ctx.guild.id] = [url]
server = ctx.message.guild
voice_channel = server.voice_client
async with ctx.typing():
player = await YTDLSource.from_url(queue[ctx.guild.id][0], loop=bot.loop)
voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
await ctx.send(f'**Now playing:** {player.title}')
del(queue[ctx.guild.id][0])