每次尝试此操作时,我都会得到一个KeyError
提示我不和谐的IP地址
players = {}
@client.command(pass_context=True)
@is_vale()
async def pause(ctx):
id = ctx.message.server.id
players[id].pause()
embed = discord.Embed(
description='Musik wurde pausiert.',
color=botcolor
)
await client.say(embed=embed)
错误看起来像这样
File "C:\Users\asche\Desktop\discord bot\bot\musikbot (1).py", line 98, in pause
players[id].pause()
KeyError: '510767063510417418'
答案 0 :(得分:0)
我对此进行了修复并更改了代码。这是新代码
def check_queue(id):
if queues[id] != []:
player = queues[id].pop(0)
players[id] = player
player.start()
else:
del players[id]
@client.command(pass_context=True)
async def play(ctx, url):
server = ctx.message.server
voice_client = client.voice_client_in(server)
try:
if players[server.id].is_playing():
player = await voice_client.create_ytdl_player(url,ytdl_options=ytdl_format_options, after=lambda: check_queue(server.id))
if server.id in queues:
queues[server.id].append(player)
else:
queues[server.id] = [player]
embed = discord.Embed(description='{} ist in der Playlist.'.format(url), color=botcolor)
await client.say(embed=embed)
else:
player = await voice_client.create_ytdl_player(url, ytdl_options=ytdl_format_options, after=lambda: check_queue(server.id))
embed = discord.Embed(description='{} wird abgespielt.'.format(url), color=botcolor)
await client.say(embed=embed)
await songs.put(player)
players[server.id] = player
player.start()
except KeyError:
player = await voice_client.create_ytdl_player(url, ytdl_options=ytdl_format_options, after=lambda: check_queue(server.id))
embed = discord.Embed(description='{} wird abgespielt.'.format(url), color=botcolor)
await client.say(embed=embed)
await songs.put(player)
players[server.id] = player
player.start()