Python Discord.py musicbot中的键错误
每次尝试此操作时,我都会收到一个Python Discord.py musicbot中的键错误,python,discord,discord.py,Python,Discord,Discord.py,每次尝试此操作时,我都会收到一个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.',
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'
我修复了它并通过这样做更改了代码。这是新的代码
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()
您从未在代码中向该词典添加任何播放机,或者代码片段不完整(在这种情况下,请更新它),或者这是您的实际错误,已经修复了它,但无论如何,请使用正确的代码片段更新问题,并添加解释如何解决问题的答案。这对遇到类似问题的其他人很有帮助,并允许他们不用事先询问就解决问题。