Defxult / discordLevelingSystem

A library to implement a leveling system into a discord bot. Contains features such as XP, level, ranks, and role awards.
MIT License
91 stars 17 forks source link

User isn't gaining experience or going into the database #21

Closed InternalCosmos closed 1 year ago

InternalCosmos commented 1 year ago

Describe the bug For some reason, award_xp isn't putting the user into the database or giving experience even if I use add_record to put them in.

To Reproduce Minimal code to reproduce the bug/error I have written the bare minimum to get a bot running with this code, as this is how I have my bot written.

import discord
import asyncio
from discordLevelingSystem import DiscordLevelingSystem
from discordLevelingSystem.errors import DatabaseFileNotFound, LeaderboardNotFound, ImproperLeaderboard, NotConnected,\
    ConnectionFailure, DiscordLevelingSystemError
from discord.ext import commands
from discord.ext.commands import AutoShardedBot
from pathlib import Path
from config import donator_role, prefixes, token

intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.presences = True

if not Path(r'database/DiscordLevelingSystem.db').is_file():
    try:
        DiscordLevelingSystem.create_database_file(r'./database/')
    except (ConnectionFailure, DatabaseFileNotFound) as error:
        raise error

class DiscordBot(AutoShardedBot):

    def __init__(self):
        super().__init__(
            command_prefix=commands.when_mentioned_or(*prefixes),
            intents=intents
        )

    lvl = DiscordLevelingSystem(rate=1, per=60.0, awards=None, announce_level_up=True, stack_awards=True)

    async def on_message(self, message: discord.Message) -> None:
        if message.author == self.user or message.author.bot:
            return
        await self.process_commands(message)
        try:
            await self.lvl.add_record(
                guild_id=message.guild.id,
                member_id=message.author.id,
                member_name=message.author.name,
                level=0
            )
        except DiscordLevelingSystemError:
            raise DiscordLevelingSystemError
        try:
            await self.lvl.award_xp(
                amount=[15, 25],
                message=message,
                refresh_name=True,
                bonus=DiscordLevelingSystem(
                    donator_role,
                    20,
                    multiply=False
                )
            )
        except (DatabaseFileNotFound, LeaderboardNotFound, ImproperLeaderboard, NotConnected) as e:
            raise e

if __name__ == "__main__":
    try:
        DiscordBot.lvl.connect_to_database_file(r'database/DiscordLevelingSystem.db')
    except (ConnectionFailure, DatabaseFileNotFound) as error:
        raise error
    asyncio.run(DiscordBot().start(token=token))

Traceback if any

N/A

Required Checklist

Version Info What's the exact version of discordLevelingSystem/discord.py are you using?

Defxult commented 1 year ago

Not sure why you're catching an error just to raise it...but the bonus parameter in award_xp is incorrect, please refer to the documentation. When you raise the DiscordLevelingSystemError, it is missing the message parameter.

Other than that, I tested your code and it works fine for me (I made the mentioned corrections above before I tested the code).

InternalCosmos commented 1 year ago

Oh wow, I'm blind. Didn't even notice I was missing the .Bonus. Thanks for the help!