python - 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.
解决方案
推荐阅读
- php - 如何在php中限制用户请求一年一次
- powershell - powershell 执行策略不受限制不接受
- clojure - 如何在 Clojure 中的一系列向量中找到然后更新映射中的值?
- php - 使用 jQuery Ajax 计算每个表行
- ios - Vlc 移动套件 iOS 重复,随机播放视频
- java - 从 firebase 获取用户时完全跳过 addOnCompleteListener
- javascript - 如何在我的视频播放器(如 youtube 播放器)中创建流畅的搜索器
- javascript - 垂直输出和水平输出到console.log
- angular - 我的数据在 Angular HTML 模板中显示未定义
- api - 用非官方的无状态 API 封装有状态 API