Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config #287

Merged
merged 16 commits into from
Dec 10, 2023
1 change: 0 additions & 1 deletion bot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class ErrorHandling(BaseModel):


class CustomRoles(BaseModel):
log_channel_id: int
divider_role_id: int
sarzz2 marked this conversation as resolved.
Show resolved Hide resolved


Expand Down
12 changes: 11 additions & 1 deletion bot/extensions/custom_roles/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from bot import core
from bot.config import settings
from bot.models import CustomRole
from bot.models import Config, CustomRole


class CustomRoles(commands.Cog):
Expand Down Expand Up @@ -105,6 +105,16 @@ async def myrole(
ephemeral=True,
)

@app_commands.command(name="set_log_channel")
sarzz2 marked this conversation as resolved.
Show resolved Hide resolved
@app_commands.describe(channel="New channel")
async def log_channel(self, interaction: discord.Interaction, channel: discord.TextChannel):
sarzz2 marked this conversation as resolved.
Show resolved Hide resolved
"""Set custom role log channel"""
query = """UPDATE configs
SET custom_role_log_channel_id = $1
WHERE guild_id = $2"""
await Config.execute(query, channel.id, interaction.guild.id)
return await interaction.response.send_message(f"Log Channel set to {channel.mention}")


async def setup(bot: commands.Bot):
await bot.add_cog(CustomRoles(bot=bot))
17 changes: 10 additions & 7 deletions bot/extensions/custom_roles/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from discord.ext import commands

from bot import core
from bot.config import settings
from bot.models.custom_roles import CustomRole
from bot.models import Config, CustomRole


class CustomRoleEvents(commands.Cog):
Expand All @@ -16,9 +15,11 @@ def __init__(self, bot: core.DiscordBot):
self.bot = bot
self.updated_at: dict[int, datetime] = {}

@property
def custom_roles_logs_channel(self) -> discord.TextChannel | None:
return self.bot.guild.get_channel(settings.custom_roles.log_channel_id)
async def get_custom_roles_logs_channel(self):
query = """SELECT * FROM configs
WHERE guild_id = $1"""
data = await Config.fetchrow(query, self.bot.guild.id)
return self.bot.guild.get_channel(data.custom_role_log_channel_id)
sarzz2 marked this conversation as resolved.
Show resolved Hide resolved

@commands.Cog.listener()
async def on_guild_role_update(self, before: discord.Role, after: discord.Role):
Expand Down Expand Up @@ -54,7 +55,8 @@ async def on_custom_role_create(self, custom_role: CustomRole):
embed.set_thumbnail(url=user.avatar)
embed.set_footer(text=f"user_id: {custom_role.user_id}")

return await self.custom_roles_logs_channel.send(embed=embed)
channel = await self.get_custom_roles_logs_channel()
return await channel.send(embed=embed)

@commands.Cog.listener()
async def on_custom_role_update(self, before: CustomRole, after: CustomRole):
Expand Down Expand Up @@ -85,4 +87,5 @@ async def on_custom_role_update(self, before: CustomRole, after: CustomRole):
embed.set_author(name=user.name, icon_url=user.avatar)
embed.set_footer(text=f"user_id: {after.user_id}")

return await self.custom_roles_logs_channel.send(embed=embed)
channel = await self.get_custom_roles_logs_channel()
return await channel.send(embed=embed)
81 changes: 77 additions & 4 deletions bot/extensions/levelling/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from bot import core
from bot.extensions.levelling import utils
from bot.models import IgnoredChannel, LevellingRole, LevellingUser
from bot.models import Config, IgnoredChannel, LevellingRole, LevellingUser
from bot.models.custom_roles import CustomRole
from cli import ROOT_DIR

Expand Down Expand Up @@ -45,12 +45,21 @@ class Levelling(commands.Cog):
default_permissions=discord.Permissions(administrator=True),
)

config = app_commands.Group(
parent=admin_commands,
name="config",
description="Manage the levelling configuration(XP Boost, min/max xp awarded)",
default_permissions=discord.Permissions(administrator=True),
)

def __init__(self, bot: core.DiscordBot):
self.bot = bot

self.ignored_channels: dict[int, list[int]] = {}
self.required_xp = [0]
self.xp_boost = 1
self.xp_boost: int = 1
self.min_xp: int = 15
self.max_xp: int = 25

# Initializing fonts
font = f"{ROOT_DIR.as_posix()}/assets/ABeeZee-Regular.otf"
Expand Down Expand Up @@ -78,6 +87,10 @@ async def cog_load(self):
for lvl in range(101):
xp = 5 * (lvl**2) + (50 * lvl) + 100
self.required_xp.append(xp + self.required_xp[-1])
config = await Config.ensure_exists(guild_id=self.bot.guild.id)
self.xp_boost = config.xp_boost
self.min_xp = config.min_xp
self.max_xp = config.max_xp
sarzz2 marked this conversation as resolved.
Show resolved Hide resolved

@commands.Cog.listener()
async def on_message(self, message):
Expand All @@ -101,8 +114,7 @@ async def on_message(self, message):
RETURNING *;
"""

# TODO: Allow each guild to set custom xp range and boost.
xp = random.randint(15, 25) * self.xp_boost
xp = random.randint(self.min_xp, self.max_xp) * self.xp_boost
after = await LevellingUser.fetchrow(query, message.guild.id, message.author.id, xp)

if after is None:
Expand Down Expand Up @@ -472,6 +484,67 @@ async def list_roles(self, interaction: core.InteractionType):

return await interaction.response.send_message(f"```\n{response}\n```\n")

@config.command(name="xp_boost")
async def xp_boost(self, interaction: discord.Interaction, multiplier: int):
"""Change the xp multiplier"""
if 0 < multiplier <= 10:
self.xp_boost = multiplier
query = """
UPDATE configs
SET xp_boost = $1
WHERE guild_id = $2
"""
await Config.execute(query, multiplier, interaction.guild.id)
return await interaction.response.send_message(f"XP multiplied by {multiplier}x.")
else:
return await interaction.response.send_message("XP multiply range should be between 1 and 10")
sarzz2 marked this conversation as resolved.
Show resolved Hide resolved

@config.command(name="min_xp")
async def min_xp(self, interaction: discord.Interaction, xp: int):
"""Set Min XP gained per message"""
if xp >= self.max_xp:
return await interaction.response.send_message(
f"Min XP({xp}) can not be greater than or equal to max XP({self.max_xp})"
)
if 0 < xp <= 100:
self.min_xp = xp
query = """
UPDATE configs
SET min_xp = $1
WHERE guild_id = $2
"""
await Config.execute(query, xp, interaction.guild.id)
return await interaction.response.send_message(f"Min XP gained each message updated to {xp}")
else:
return await interaction.response.send_message("Min XP range should be between 1 and 100")

@config.command(name="max_xp")
async def max_xp(self, interaction: discord.Interaction, xp: int):
"""Set Max XP gained per message"""
if xp <= self.min_xp:
return await interaction.response.send_message(
f"Max XP({xp}) can not be less than or equal to min XP({self.min_xp})"
)
if 1 < xp <= 500:
self.min_xp = xp
query = """
UPDATE configs
SET max_xp = $1
WHERE guild_id = $2"""
await Config.execute(query, xp, interaction.guild.id)
return await interaction.response.send_message(f"Max XP gained each message updated to {xp}")
else:
return await interaction.response.send_message("Max XP range should be between 1 and 500")

@config.command(name="info")
async def current_config(self, interaction: discord.Interaction):
"""Return the current configuration settings"""
query = """
SELECT * FROM configs
WHERE guild_id = $1"""
x = await Config.fetchrow(query, interaction.guild.id)
return await interaction.response.send_message(x)
sarzz2 marked this conversation as resolved.
Show resolved Hide resolved


async def setup(bot: core.DiscordBot):
await bot.add_cog(Levelling(bot=bot))
2 changes: 2 additions & 0 deletions bot/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .configs import Config
from .custom_roles import CustomRole
from .gconfig import FilterConfig
from .levelling_ignored_channels import IgnoredChannel
Expand All @@ -22,4 +23,5 @@
IgnoredChannel,
LevellingRole,
CustomRole,
Config,
) # Fixes F401
20 changes: 20 additions & 0 deletions bot/models/configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from .model import Model


class Config(Model):
sarzz2 marked this conversation as resolved.
Show resolved Hide resolved
id: int
guild_id: int
min_xp: int
max_xp: int
xp_boost: int
custom_role_log_channel_id: int | None

@classmethod
async def ensure_exists(cls, guild_id: int):
query = """
INSERT INTO configs (guild_id)
VALUES ($1)
ON CONFLICT (guild_id)
DO UPDATE SET guild_id = configs.guild_id
RETURNING *"""
return await cls.fetchrow(query, guild_id)
sarzz2 marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions bot/models/migrations/006_down__configs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS configs;
9 changes: 9 additions & 0 deletions bot/models/migrations/006_up__configs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS configs
(
id BIGINT PRIMARY KEY DEFAULT create_snowflake(),
sarzz2 marked this conversation as resolved.
Show resolved Hide resolved
guild_id BIGINT UNIQUE NOT NULL,
min_xp INT NOT NULL DEFAULT 15,
max_xp INT NOT NULL DEFAULT 25,
xp_boost INT NOT NULL DEFAULT 1,
custom_role_log_channel_id BIGINT
);
4 changes: 2 additions & 2 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ TAGS__LOG_CHANNEL_ID=
TAGS__REQUIRED_ROLE_ID=

# --- Custom Roles
CUSTOM_ROLES__LOG_CHANNEL_ID=
CUSTOM_ROLES__DIVIDER_ROLE_ID=
CUSTOM_ROLES__DIVIDER_ROLE_ID=0


# --- Youtube
YOUTUBE__CHANNEL_ID=UC4JX40jDee_tINbkjycV4Sg
Expand Down
Loading