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?
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 anException
occurs... – Reedinationerif 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. – xupaiiuser
object from discord and your goal is to send them a message that lets them know they've leveled up? Or isuser
an ID? What exactly is the input of your function? – Reedinationeruser
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 forctx.send
– xupaii