Search
 
SCRIPT & CODE EXAMPLE
 

PHP

discord.py Levels

@client.event
async def on_member_join(member):
    with open('users.json', 'r') as f:
        users = json.load(f)

    await update_data(users, member)

    with open('users.json', 'w') as f:
        json.dump(users, f)


@client.event
async def on_message(message):
    if message.author.bot == False:
        with open('users.json', 'r') as f:
            users = json.load(f)

        await update_data(users, message.author)
        await add_experience(users, message.author, 5)
        await level_up(users, message.author, message)

        with open('users.json', 'w') as f:
            json.dump(users, f)

    await client.process_commands(message)


async def update_data(users, user):
    if not f'{user.id}' in users:
        users[f'{user.id}'] = {}
        users[f'{user.id}']['experience'] = 0
        users[f'{user.id}']['level'] = 1


async def add_experience(users, user, exp):
    users[f'{user.id}']['experience'] += exp


async def level_up(users, user, message):
    with open('levels.json', 'r') as g:
        levels = json.load(g)
    experience = users[f'{user.id}']['experience']
    lvl_start = users[f'{user.id}']['level']
    lvl_end = int(experience ** (1 / 4))
    if lvl_start < lvl_end:
        await message.channel.send(f'{user.mention} has leveled up to level {lvl_end}')
        users[f'{user.id}']['level'] = lvl_end

@client.command()
async def level(ctx, member: discord.Member = None):
    if not member:
        id = ctx.message.author.id
        with open('users.json', 'r') as f:
            users = json.load(f)
        lvl = users[str(id)]['level']
        await ctx.send(f'You are at level {lvl}!')
    else:
        id = member.id
        with open('users.json', 'r') as f:
            users = json.load(f)
        lvl = users[str(id)]['level']
        await ctx.send(f'{member} is at level {lvl}!')
Comment

PREVIOUS NEXT
Code Example
Php :: remove rank math next prev canonical 
Php :: custom end-point request php-salesforce-rest-api 
Php :: expiry date alert in php 
Php :: ob_start store variable in php 
Php :: Laravel appends child when referencing it in attribute function 
Php :: php Write a program to reverse an array or string 
Php :: spatie/laravel-health 
Php :: laravel 7 link to another page with language prefix 
Php :: text short in laravel 
Php :: auto complete order paid3 
Php :: do php 
Php :: $s = [1,2,3] for loop use in php 
Php :: laravel has many deep 
Php :: an einem string etwas anfügen php 
Php :: laravel add model to polymorphic relationships 
Php :: phpcs unabl 
Php :: woocommerce show percentage in sales badge 
Php :: wcfm filter vendor dashboard widgets 
Php :: wordpress redirect attachment page to file 
Php :: phpmailer 5 string attachment 
Php :: app/View/Errors/missing_controller.ctp 
Php :: Convert Array Value Session Value To String PHP 
Php :: PHP force refresh image 
Php :: install tinymce php 
Php :: prosedur dan fungsi dengan php 
Php :: woocommerce php customer reset password length 
Php :: php find odd even number in loop 
Php :: php years 
Php :: How to create an Invoice with watermark FPDF 
Php :: add attachment to the invoice laravel 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =