|
| 1 | +"""Module for the XP extension for the discord bot.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import random |
| 6 | +from typing import TYPE_CHECKING, Self |
| 7 | + |
| 8 | +import discord |
| 9 | +import expiringdict |
| 10 | +import munch |
| 11 | +from core import auxiliary, cogs, extensionconfig |
| 12 | +from discord.ext import commands |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + import bot |
| 16 | + |
| 17 | + |
| 18 | +async def setup(bot: bot.TechSupportBot) -> None: |
| 19 | + """Loading the XP plugin into the bot |
| 20 | +
|
| 21 | + Args: |
| 22 | + bot (bot.TechSupportBot): The bot object to register the cogs to |
| 23 | + """ |
| 24 | + config = extensionconfig.ExtensionConfig() |
| 25 | + config.add( |
| 26 | + key="categories_counted", |
| 27 | + datatype="list", |
| 28 | + title="List of category IDs to count for XP", |
| 29 | + description="List of category IDs to count for XP", |
| 30 | + default=[], |
| 31 | + ) |
| 32 | + config.add( |
| 33 | + key="level_roles", |
| 34 | + datatype="dict", |
| 35 | + title="Dict of levels in XP:Role ID.", |
| 36 | + description="Dict of levels in XP:Role ID", |
| 37 | + default={}, |
| 38 | + ) |
| 39 | + |
| 40 | + await bot.add_cog(LevelXP(bot=bot, extension_name="xp")) |
| 41 | + bot.add_extension_config("xp", config) |
| 42 | + |
| 43 | + |
| 44 | +class LevelXP(cogs.MatchCog): |
| 45 | + """Class for the LevelXP to make it to discord.""" |
| 46 | + |
| 47 | + async def preconfig(self: Self) -> None: |
| 48 | + """Sets up the dict""" |
| 49 | + self.ineligible = expiringdict.ExpiringDict( |
| 50 | + max_len=1000, |
| 51 | + max_age_seconds=60, |
| 52 | + ) |
| 53 | + |
| 54 | + async def match( |
| 55 | + self: Self, config: munch.Munch, ctx: commands.Context, _: str |
| 56 | + ) -> bool: |
| 57 | + """Checks a given message to determine if XP should be applied |
| 58 | +
|
| 59 | + Args: |
| 60 | + config (munch.Munch): The guild config for the running bot |
| 61 | + ctx (commands.Context): The context that the original message was sent in |
| 62 | +
|
| 63 | + Returns: |
| 64 | + bool: True if XP should be granted, False if it shouldn't be. |
| 65 | + """ |
| 66 | + # Ignore all bot messages |
| 67 | + if ctx.message.author.bot: |
| 68 | + return False |
| 69 | + |
| 70 | + # Ignore anyone in the ineligible list |
| 71 | + if ctx.author.id in self.ineligible: |
| 72 | + return False |
| 73 | + |
| 74 | + # Ignore messages outside of tracked categories |
| 75 | + if ctx.channel.category_id not in config.extensions.xp.categories_counted.value: |
| 76 | + return False |
| 77 | + |
| 78 | + # Ignore messages that are too short |
| 79 | + if len(ctx.message.clean_content) < 20: |
| 80 | + return False |
| 81 | + |
| 82 | + prefix = await self.bot.get_prefix(ctx.message) |
| 83 | + |
| 84 | + # Ignore messages that are bot commands |
| 85 | + if ctx.message.clean_content.startswith(prefix): |
| 86 | + return False |
| 87 | + |
| 88 | + # Ignore messages that are factoid calls |
| 89 | + if "factoids" in config.enabled_extensions: |
| 90 | + factoid_prefix = prefix = config.extensions.factoids.prefix.value |
| 91 | + if ctx.message.clean_content.startswith(factoid_prefix): |
| 92 | + return False |
| 93 | + |
| 94 | + last_message_in_channel = await auxiliary.search_channel_for_message( |
| 95 | + channel=ctx.channel, |
| 96 | + prefix=prefix, |
| 97 | + allow_bot=False, |
| 98 | + skip_messages=[ctx.message.id], |
| 99 | + ) |
| 100 | + if last_message_in_channel.author == ctx.author: |
| 101 | + return False |
| 102 | + |
| 103 | + return True |
| 104 | + |
| 105 | + async def response( |
| 106 | + self: Self, config: munch.Munch, ctx: commands.Context, content: str, _: bool |
| 107 | + ) -> None: |
| 108 | + """Updates XP for the given user. |
| 109 | + Message has already been validated when you reach this function. |
| 110 | +
|
| 111 | + Args: |
| 112 | + config (munch.Munch): The guild config for the running bot |
| 113 | + ctx (commands.Context): The context in which the message was sent in |
| 114 | + content (str): The string content of the message |
| 115 | + """ |
| 116 | + current_XP = await get_current_XP(self.bot, ctx.author, ctx.guild) |
| 117 | + new_XP = random.randint(10, 20) |
| 118 | + |
| 119 | + await update_current_XP(self.bot, ctx.author, ctx.guild, (current_XP + new_XP)) |
| 120 | + |
| 121 | + await self.apply_level_ups(ctx.author, (current_XP + new_XP)) |
| 122 | + |
| 123 | + self.ineligible[ctx.author.id] = True |
| 124 | + |
| 125 | + async def apply_level_ups(self: Self, user: discord.Member, new_xp: int) -> None: |
| 126 | + """This function will determine if a user leveled up and apply the proper roles |
| 127 | +
|
| 128 | + Args: |
| 129 | + user (discord.Member): The user who just gained XP |
| 130 | + new_xp (int): The new amount of XP the user has |
| 131 | + """ |
| 132 | + config = self.bot.guild_configs[str(user.guild.id)] |
| 133 | + levels = config.extensions.xp.level_roles.value |
| 134 | + |
| 135 | + if len(levels) == 0: |
| 136 | + return |
| 137 | + |
| 138 | + configured_levels = [ |
| 139 | + (int(xp_threshold), int(role_id)) |
| 140 | + for xp_threshold, role_id in levels.items() |
| 141 | + ] |
| 142 | + configured_role_ids = {role_id for _, role_id in configured_levels} |
| 143 | + |
| 144 | + # Determine the role id that corresponds to the new XP (target role) |
| 145 | + target_role_id = max( |
| 146 | + ((xp, role_id) for xp, role_id in configured_levels if new_xp >= xp), |
| 147 | + default=(-1, None), |
| 148 | + key=lambda t: t[0], |
| 149 | + )[1] |
| 150 | + |
| 151 | + # A list of roles IDs related to the level system that the user currently has. |
| 152 | + user_level_roles_ids = [ |
| 153 | + role.id for role in user.roles if role.id in configured_role_ids |
| 154 | + ] |
| 155 | + |
| 156 | + # If the user has only the correct role, do nothing. |
| 157 | + if user_level_roles_ids == [target_role_id]: |
| 158 | + return |
| 159 | + |
| 160 | + # Otherwise, remove all the roles from user_level_roles and then apply target_role_id |
| 161 | + for role_id in user_level_roles_ids: |
| 162 | + role_object = await user.guild.fetch_role(role_id) |
| 163 | + await user.remove_roles(role_object, reason="Level up") |
| 164 | + |
| 165 | + if not target_role_id: |
| 166 | + return |
| 167 | + |
| 168 | + target_role_object = await user.guild.fetch_role(target_role_id) |
| 169 | + await user.add_roles(target_role_object, reason="Level up") |
| 170 | + |
| 171 | + |
| 172 | +async def get_current_XP( |
| 173 | + bot: object, user: discord.Member, guild: discord.Guild |
| 174 | +) -> int: |
| 175 | + """Calls to the database to get the current XP for a user. Returns 0 if no XP |
| 176 | +
|
| 177 | + Args: |
| 178 | + bot (object): The TS bot object to use for the database lookup |
| 179 | + user (discord.Member): The member to look for XP for |
| 180 | + guild (discord.Guild): The guild to fetch the XP from |
| 181 | +
|
| 182 | + Returns: |
| 183 | + int: The current XP for a given user, or 0 if the user has no XP entry |
| 184 | + """ |
| 185 | + current_XP = ( |
| 186 | + await bot.models.XP.query.where(bot.models.XP.user_id == str(user.id)) |
| 187 | + .where(bot.models.XP.guild_id == str(guild.id)) |
| 188 | + .gino.first() |
| 189 | + ) |
| 190 | + if not current_XP: |
| 191 | + return 0 |
| 192 | + |
| 193 | + return current_XP.xp |
| 194 | + |
| 195 | + |
| 196 | +async def update_current_XP( |
| 197 | + bot: object, user: discord.Member, guild: discord.Guild, xp: int |
| 198 | +) -> None: |
| 199 | + """Calls to the database to get the current XP for a user. Returns 0 if no XP |
| 200 | +
|
| 201 | + Args: |
| 202 | + bot (object): The TS bot object to use for the database lookup |
| 203 | + user (discord.Member): The member to look for XP for |
| 204 | + guild (discord.Guild): The guild to fetch the XP from |
| 205 | + xp (int): The new XP to give the user |
| 206 | +
|
| 207 | + """ |
| 208 | + current_XP = ( |
| 209 | + await bot.models.XP.query.where(bot.models.XP.user_id == str(user.id)) |
| 210 | + .where(bot.models.XP.guild_id == str(guild.id)) |
| 211 | + .gino.first() |
| 212 | + ) |
| 213 | + if not current_XP: |
| 214 | + current_XP = bot.models.XP(user_id=str(user.id), guild_id=str(guild.id), xp=xp) |
| 215 | + await current_XP.create() |
| 216 | + else: |
| 217 | + await current_XP.update(xp=xp).apply() |
0 commit comments