简体   繁体   English

Discord Python Bot:等级和经验系统

[英]Discord Python Bot: Levels and EXP System

So I have a Discord bot I'm building in Python and I made a level system for it.所以我有一个用 Python 构建的 Discord 机器人,我为它制作了一个关卡系统。 It's working in the most part but I'm having two issues with it.它在大多数情况下都有效,但我遇到了两个问题。

It's leveling up a tad bit too fast, and also it's reading both bots and webhooks as members.它的升级有点太快了,而且它同时读取机器人和 webhooks 作为成员。

I've tried adjusting the numbers in the if cur_xp >= round((4 * (cur_lvl ** 3)) / 5) statement but it either speeds it up or does nothing at all.我试过调整if cur_xp >= round((4 * (cur_lvl ** 3)) / 5)语句中的数字,但它要么加快速度,要么什么都不做。

Here is my code:这是我的代码:

import discord
from discord.ext import commands

import json
import asyncio


class Levels(commands.Cog):
    def __init__(self, client):
        self.client = client

        with open(r"path\to\json\file\users.json", "r") as f:
            self.users = json.load(f)

        self.client.loop.create_task(self.save_users())

    async def save_users(self):
        await self.client.wait_until_ready()
        while not self.client.is_closed():
            with open(r"path\to\json\file\users.json", "w") as f:
                json.dump(self.users, f, indent=4)

            await asyncio.sleep(5)

    def lvl_up(self, author_id):
        cur_xp = self.users[author_id]["exp"]
        cur_lvl = self.users[author_id]["level"]

        if cur_xp >= round((4 * (cur_lvl ** 3)) / 5):
            self.users[author_id]["level"] += 1
            return True
        else:
            return False

    @commands.Cog.listener()
    async def on_message(self, message):
        if message.author == self.client.user:
            return

        author_id = str(message.author.id)

        if not author_id in self.users:
            self.users[author_id] = {}
            self.users[author_id]["level"] = 0
            self.users[author_id]["exp"] = 0

        self.users[author_id]["exp"] += 1

        if self.lvl_up(author_id):
            await message.channel.send(f"{message.author.mention} is now level {self.users[author_id]['level']}")

    @commands.command(brief="Displays the user's level and experience.")
    async def level(self, ctx, member: discord.Member = None):
        member = ctx.author if not member else member
        member_id = str(member.id)

        if not member_id in self.users:
            await ctx.send(f"{member} doesn't have a level")
        else:
            embed = discord.Embed(color=member.color, timestamp=ctx.message.created_at)

            embed.set_author(name=f"Member = {member}", icon_url=self.client.user.avatar_url)

            embed.add_field(name="Level", value=self.users[member_id]["level"])
            embed.add_field(name="XP", value=self.users[member_id]["exp"])

            await ctx.send(embed=embed)


def setup(client):
    client.add_cog(Levels(client))

What I'm needing is to slow down the EXP gain a little bit more and also make it stop giving EXP to bots and webhooks.我需要的是稍微减慢 EXP 的增益,并使其停止向机器人和 webhooks 提供 EXP。

Any help would be appreciated.任何帮助,将不胜感激。 As the guide I found to build this didn't give a good explanation of how the variables used fr the XP actually measure it so I'm not sure what numbers to change.作为我发现构建它的指南并没有很好地解释 XP 中使用的变量如何实际测量它,所以我不确定要更改哪些数字。

I'd say this is between knowing what the formula's curve looks like and stopping the user from spamming for XP.我会说这是在了解公式的曲线是什么样子和阻止用户为 XP 发送垃圾邮件之间。 Great examples of formulas to use and breakdowns of their uses can be found in this questions answers.这个问题的答案中可以找到使用公式的很好的例子和它们的使用细目。

As for limiting to only users put a check in your on_message function for if not user.bot: before your leveling code to exclude bots/webhooks.至于仅限于用户在您的on_message函数中检查if not user.bot:在您的调平代码以排除 bots/webhooks 之前。

And to time limit xp gain record the time users last message then subtract that time from the current time and ensure it's greater than a minute or whatever time limit you see fit.并限制xp增益记录用户上次发送消息的时间,然后从当前时间中减去该时间并确保它大于一分钟或您认为合适的任何时间限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM