-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated commands and events for levelling.
- Loading branch information
Showing
10 changed files
with
439 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
from bot.core import DiscordBot | ||
|
||
from .commands import Levelling | ||
from .events import LevelEvents | ||
from .events import LevellingEvents | ||
|
||
|
||
async def setup(bot: DiscordBot) -> None: | ||
await bot.add_cog(Levelling(bot=bot)) | ||
await bot.add_cog(LevelEvents(bot=bot)) | ||
await bot.add_cog(LevellingEvents(bot=bot)) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,60 @@ | ||
from bisect import bisect | ||
|
||
import discord | ||
from discord.ext import commands | ||
|
||
from bot import core | ||
from bot.models import LevellingRole, PersistentRole | ||
from bot.models import LevellingRole, LevellingUser | ||
|
||
|
||
class LevelEvents(commands.Cog): | ||
class LevellingEvents(commands.Cog): | ||
"""Events for Levelling in discord.""" | ||
|
||
def __init__(self, bot: core.DiscordBot): | ||
self.bot = bot | ||
|
||
@commands.Cog.listener() | ||
async def on_level_up(self, new_level, member: discord.Member): | ||
"""Add roles when user levels up""" | ||
data = await LevellingRole.list_by_guild(guild_id=member.guild.id) | ||
for i in range(len(data)): | ||
# Adding roles when user levels up | ||
if new_level >= data[i].level and member.guild.get_role(data[i].role_id) not in member.roles: | ||
await member.add_roles(member.guild.get_role(data[i].role_id)) | ||
await PersistentRole.insert_by_guild( | ||
guild_id=member.guild.id, user_id=member.id, role_id=data[i].role_id | ||
) | ||
# Removing roles when users level down using remove_xp command | ||
elif new_level <= data[i].level and member.guild.get_role(data[i].role_id) in member.roles: | ||
await member.remove_roles(member.guild.get_role(data[i].role_id)) | ||
await PersistentRole.delete_by_guild(guild_id=member.guild.id, user_id=member.id) | ||
|
||
@commands.Cog.listener() | ||
async def on_xp_updated(self, data, member: discord.Member, required_xp): | ||
"""Function to check if user's level has changed and trigger the event to assign the roles""" | ||
# Calculating old and new level | ||
try: | ||
old_level = bisect(required_xp, data.old_total_xp) - 1 | ||
new_level = bisect(required_xp, data.total_xp) - 1 | ||
|
||
if old_level != new_level: | ||
self.bot.dispatch("level_up", new_level=new_level, member=member) | ||
except AttributeError: | ||
pass | ||
async def on_xp_update(self, before: LevellingUser, after: LevellingUser): | ||
if after.total_xp == before.total_xp: | ||
return | ||
|
||
elif after.total_xp > before.total_xp: | ||
query = """ | ||
SELECT COALESCE(array_agg(role_id), '{}') | ||
FROM levelling_roles lr | ||
WHERE lr.guild_id = $1 | ||
AND lr.required_xp <= $2 | ||
AND lr.role_id NOT IN ( | ||
SELECT pr.role_id | ||
FROM persisted_roles pr | ||
WHERE pr.guild_id = lr.guild_id | ||
AND pr.user_id = $3 | ||
) | ||
""" | ||
# Fetch role ids that the user qualifies for, but have not been persisted. | ||
role_ids = await LevellingRole.fetchval(query, after.guild_id, after.total_xp, after.user_id) | ||
|
||
if not role_ids: | ||
return | ||
|
||
self.bot.dispatch("persist_roles", guild_id=after.guild_id, user_id=after.user_id, role_ids=role_ids) | ||
|
||
else: | ||
query = """ | ||
SELECT COALESCE(array_agg(role_id), '{}') | ||
FROM levelling_roles lr | ||
WHERE lr.guild_id = $1 | ||
AND lr.required_xp > $2 | ||
AND lr.role_id IN ( | ||
SELECT pr.role_id | ||
FROM persisted_roles pr | ||
WHERE pr.guild_id = lr.guild_id | ||
AND pr.user_id = $3 | ||
) | ||
""" | ||
|
||
role_ids = await LevellingRole.fetchval(query, after.guild_id, after.total_xp, after.user_id) | ||
|
||
if not role_ids: | ||
return | ||
|
||
self.bot.dispatch( | ||
"remove_persisted_roles", guild_id=after.guild_id, user_id=after.user_id, role_ids=role_ids | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from bisect import bisect | ||
|
||
required_xp = [0] | ||
|
||
for lvl in range(1001): | ||
xp = 5 * (lvl**2) + (50 * lvl) + 100 | ||
required_xp.append(xp + required_xp[-1]) | ||
|
||
|
||
def get_level_for_xp(user_xp: int) -> int: | ||
return bisect(required_xp, user_xp) - 1 | ||
|
||
|
||
def get_xp_for_level(level: int) -> int: | ||
return required_xp[level] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,24 @@ | ||
from .model import Model | ||
|
||
|
||
class CustomRoles(Model): | ||
class CustomRole(Model): | ||
id: int | ||
guild_id: int | ||
role_id: int | ||
name: str | ||
color: str | ||
|
||
@classmethod | ||
async def ensure_exists(cls, guild_id: int, role_id: int, name: str, color: str): | ||
"""Inserts or updates the custom role.""" | ||
query = """ | ||
INSERT INTO custom_roles (guild_id, role_id, name, color) | ||
VALUES ($1, $2, $3, $4) | ||
ON CONFLICT (guild_id, role_id) | ||
DO UPDATE SET | ||
name = $3, | ||
color = $4 | ||
RETURNING * | ||
""" | ||
|
||
return await cls.fetchrow(query, guild_id, role_id, name, color) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters