#global variable and keyerror

1 messages · Page 1 of 1 (latest)

little plank
#

I have this code where I modify the global variable afk_list which is a dictionary:

afk_list = {}

@client.application_command(name="afk-for", guilds=[731083709658169344])
async def afk_for(ctx: ApplicationContext, time: Option(int, name="time", min_value=1, max_value=60), unit: Option(str, choices=["seconds", "minuts", "hours"])):
    if ctx.author.voice is None:
        await ctx.respond("You must be in a voice channel!", ephemeral=True)
        return
    
    units = {
        "secondes": "sec",
        "minutes": "min",
        "heures": "h"
    }
    
    last_nick = ctx.author.nick or ctx.author.name
    new_nick = f"[AFK {time}{units[unit]}] {last_nick}"
    
    global afk_list                      # Issue is here
    afk_list[ctx.author.id] = last_nick  # And here

    await ctx.author.edit(nick=new_nick)
    await ctx.respond("You are now AFK", ephemeral=True)

However when I access the variable afk_list in a voice_state_event, the key has not been created and the dictionary is still empty.

Is it due to the fact that it is an asynchronous function?

little plank
#

also here is the event where I read the variable

@client.event
async def on_voice_state_update(member: Member, before: VoiceState, after: VoiceState):
    global afk_list
    if member.id in afk_list:
        if before and not after:
            await after.edit(nick=afk_list[member.id])
            del afk_list[member.id]

        elif before and after:
            if before.self_mute and not after.self_mute:
                await after.edit(nick=afk_list[member.id])
                del afk_list[member.id]
little plank
#

global variable and keyerror

#

it seems that python hates me today and decided to not work at all

#

When the part 1 (indicated bellow) changes the nickname, part 2 raises a KeyError even thought part 1 has been able to read it
However when part 1 is useless as the nickname because it's the same as before the change, there are no errors and part to is able to delete the key properly... HOW

@client.event
async def on_voice_state_update(member: Member, before: VoiceState, after: VoiceState):
    global afk_list
    if member.id in afk_list:
        if before and not after:
            await after.edit(nick=afk_list[member.id])
            del afk_list[member.id]

        elif before and after:
            if before.self_mute and not after.self_mute:
                await after.edit(nick=afk_list[member.id])  # part 1
                del afk_list[member.id]                     # part 2
#

For example if the current nickname is "George" and part 1 changes it to "Steven", part 2 raises a KeyError
But if the current nickname is already "Steven" and part 1 changes it to "Steven" (which in this case is useless), part 2 won't raise any error

#

.

#

PLEASE HELP

little plank
#

I figured out how to fix the last issue mentioned, I used afk_list.pop(member.id) instead

little plank
#

So

#

I might have been a dumbass and wrote an event that was in fact basically clearing the variable each time it was modified