首页 > 解决方案 > Problems with my Discord Bot's Leveling system and role giver

问题描述

So, I'm working on a Discord bot for my Discord server by following some tutorials, and I can't seem to figure out why both the reaction-role-giver and the leveling system isn't working.

For the reaction-role-giver, the point is to give the emoji the role to the person who reacts to that emoji as the descriptor implies. The error I'm getting for that is "Member not found." And the leveling system is supposed to store the user ID and the experience level in the users.json file (which it does). Still, it doesn't increase the level numbers like it's told to, and it duplicates the User ID in the users.json instead of adding the EXP points to the already-there User ID.

Here's the code:

import discord
from discord.ext import commands
import os
import json

client = commands.Bot(command_prefix="~")

@client.event
async def on_ready():
print("Bot is ready")

# Leveling System starts here! 
# Needs to be fixed.

    @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):
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.channel)

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

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

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

async def level_up(users, user, channel):
experience = users[user.id]['experience']
lvl_start = users[user.id]['level']
lvl_end = int(experience ** (1/4))

if lvl_start < lvl_end:
    await client.send_message(channel, '{} has leveled up to level {}'.format(user.mention, lvl_end))
    users[user.id]['level'] = lvl_end

# Reaction Code starts here
# Needs to be fixed...

@client.event
async def on_raw_reaction_add(payload):
message_id = payload.message_id
if message_id == ID_redacted: # ID Redacted but in the actual file it's there
    guild_id = payload.guild_id
    guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)

    if payload.emoji.name == 'Toons':
        role = discord.utils.get(guild.roles, name='Toons')
    elif payload.emoji.name == 'Heroes':
        role = discord.utils.get(guild.roles, name='Heroes')
    elif payload.emoji.name == 'NotificationsSquad':
        role = discord.utils.get(guild.roles, name='Notifications Squad')
    else:
        role = discord.utils.get(guild.roles, name=payload.emoji.name)

    if role is not None:
        member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
        if member is not None:
            await member.add_roles(role)
            print("done")
        else:
            print("Member not found.")
    else:
            print("Role not found.")

token = os.environ.get("Secrets")
client.run(token)

I've tried to get help from some friends but they're too busy. So, I have come here for help. I hope that this was enough information to go on! If not, ask me in the comments if I need more information.

标签: pythondiscordbots

解决方案


推荐阅读