1
votes

I've begun setting up a levelling system for my bot, and have been working to try and implement a message on level-up. However, because the function I am creating isn't a command, I don't think I can pass context to it, which is causing issues.

Due to lack of a better solution, I chucked it into my function which checks exp and matches with a level - what happens is a copy of the user's level is made, then later compared to the updated one to see if a level up occurred. This works, however my ONLY issue is not being able to send the "level-up" message.

async def levelcheck(user):
    global userlvl
    global userlvlc

    channel = discord.Object(id="")
    # copying the userlevel BEFORE update
    try:
        userlvlc = userlvl
    except NameError as e:
        print(e)

    with open('userexp.json', 'r') as fp:
        userexp = json.load(fp)

    # finding out level based on exp 
    for x in range(0,len(levelcaps)):
        if userexp[str(user)] <= levelcaps[x]:
            userlvl = x
            try:
                if userlvlc < userlvl:
                    await ctx.send('You levelled up --> {}'.format(userlvl))
            except NameError as e:
                print(e)
            #
            return userlvl

As you can clearly see, it isn't a pretty command but certainly works. I begun by trying to get a channel id, only to realise I don't know how to get the current channel with context. Is there a way to send a message to the current channel without context, or do I need to re-think my method?

1
You realize that return userlvl makes about half of the code you posted unreachable, right? The way I am reading it, the only way it can be reached is actually if an Exception occurs...Reedinationer
huh.. well i mean it gets to the level-up message when i level up, and doesn't other wise. I managed to get if userexp[str(user)] < 100 into my for loop. If you can help me improve my code, I am definitely interested though, I am still pretty beginner.xupaii
Updated my code to what it is now, if there is still any problems feel free to let me know :) [note: still not sending level-up messages]xupaii
So you have the user object from discord and your goal is to send them a message that lets them know they've leveled up? Or is user an ID? What exactly is the input of your function?Reedinationer
user is an ID, sorry for not clarifying. My dicts are all in the format of "id": value. My goal is just to send a message in the chat the user is when they level up, which would work if i had a substitute for ctx.sendxupaii

1 Answers

1
votes

Seeing as you have a user ID I would propose this solution.

user_to_level_up = bot.fetch_user(user) # since your user variable is an ID
dm_channel = user_to_level_up.dm_channel
if dm_channel is None:
    await user_to_level_up.create_dm()
    dm_channel = user_to_level_up.dm_channel
await dm_channel.send("You've leveled up! You are now level {}".format(userlvl))

Note: this is untested, but the links should provide the information to make it work!

This just sends them a personal message, instead of posting in a server. To post in a server it would seem you need to cross reference the servers the user is in that the bot is also in, and that sounds like too much work to me XD