Skip to content

Commit

Permalink
renaming the model variable to be more meaningful and refactoring the…
Browse files Browse the repository at this point in the history
… cog to work be in extensions format
  • Loading branch information
sarzz2 committed Oct 13, 2023
1 parent 978b608 commit 75283dc
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bot/cogs/persistent_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self, bot):
@commands.Cog.listener()
async def on_member_join(self, member):
# Get the data
data = await PersistentRole.get(member.guild.id, member.id)
data = await PersistentRole.list_by_guild(member.guild.id, member.id)
# Return if data for specified user and guild does not exist
if data is None:
return
Expand Down
15 changes: 0 additions & 15 deletions bot/extensions/challenges/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from bot import core
from bot.config import settings
from bot.models import LevellingRole, PersistentRole

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -49,17 +48,3 @@ async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent):
await self.bot.http.add_role(
guild_id=self.bot.guild.id, user_id=payload.user_id, role_id=self.participant_role.id
)

@commands.Cog.listener()
async def on_level_up(self, new_level, member: discord.Member):
"""Add roles when user levels up"""
data = await LevellingRole.get(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(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.remove(guild_id=member.guild.id, user_id=member.id)
9 changes: 9 additions & 0 deletions bot/extensions/levelling/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from bot.core import DiscordBot

from .commands import Levelling
from .events import LevelEvents


async def setup(bot: DiscordBot) -> None:
await bot.add_cog(Levelling(bot=bot))
await bot.add_cog(LevelEvents(bot=bot))
6 changes: 3 additions & 3 deletions bot/extensions/levelling/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def __init__(self, bot):

async def cog_load(self):
for guild in self.bot.guilds:
data = await IgnoredChannel.get(guild_id=guild.id)
data = await IgnoredChannel.list_by_guild(guild_id=guild.id)
for i in data:
if guild.id not in self.ignored_channel:
self.ignored_channel[guild.id] = [i.channel_id]
Expand Down Expand Up @@ -175,15 +175,15 @@ async def rank(self, interaction: core.InteractionType, member: discord.Member =
@app_commands.checks.has_permissions(administrator=True)
async def ignore_channel(self, interaction: core.InteractionType, channel: discord.TextChannel):
"""Add the channel to the ignored channel list to not gain XP"""
await IgnoredChannel.insert(channel.guild.id, channel.id)
await IgnoredChannel.insert_by_guild(channel.guild.id, channel.id)
await interaction.response.send_message(f"{channel} has been ignored from gaining XP.")

@app_commands.command()
@app_commands.checks.has_permissions(administrator=True)
@has_permissions(administrator=True)
async def unignore_channel(self, interaction: core.InteractionType, channel: discord.TextChannel):
"""Remove channel from ignored channel list"""
await IgnoredChannel.delete(channel.guild.id, channel.id)
await IgnoredChannel.delete_by_guild(channel.guild.id, channel.id)
await interaction.response.send_message(f"{channel} has been removed from ignored channel list")

@app_commands.command()
Expand Down
28 changes: 28 additions & 0 deletions bot/extensions/levelling/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import discord
from discord.ext import commands

from bot import core
from bot.models import LevellingRole, PersistentRole


class LevelEvents(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)
12 changes: 6 additions & 6 deletions bot/models/ignored_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ class IgnoredChannel(Model):
channel_id: int

@classmethod
async def get(cls, guild_id: int):
query = """ SELECT * FROM ignored_channels WHERE guild_id = $1"""
async def list_by_guild(cls, guild_id: int):
query = """SELECT * FROM ignored_channels WHERE guild_id = $1"""
return await cls.fetch(query, guild_id)

@classmethod
async def insert(cls, guild_id: int, channel_id: int):
async def insert_by_guild(cls, guild_id: int, channel_id: int):
query = """INSERT INTO ignored_channels (guild_id,channel_id) VALUES($1, $2)
ON CONFLICT (guild_id,channel_id) DO NOTHING
RETURNING guild_id, channel_id"""
ON CONFLICT (guild_id,channel_id) DO NOTHING
RETURNING guild_id, channel_id"""
return await cls.fetch(query, guild_id, channel_id)

@classmethod
async def delete(cls, guild_id: int, channel_id: int):
async def delete_by_guild(cls, guild_id: int, channel_id: int):
query = """DELETE FROM ignored_channel WHERE guild_id= $1 and channel_id = $2"""
return await cls.fetch(query, guild_id, channel_id)
2 changes: 1 addition & 1 deletion bot/models/levelling_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class LevellingRole(Model):
level: int

@classmethod
async def get(cls, guild_id: int):
async def list_by_guild(cls, guild_id: int):
query = """SELECT guild_id, role_id, level FROM levelling_roles WHERE guild_id=$1"""
return await cls.fetch(query, guild_id)
12 changes: 6 additions & 6 deletions bot/models/persistent_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ class PersistentRole(Model):
role_id: Optional[int]

@classmethod
async def get(cls, guild_id: int, user_id: int):
async def list_by_guild(cls, guild_id: int, user_id: int):
query = """SELECT * FROM persistent_roles WHERE guild_id = $1 and user_id = $2"""
return await cls.fetch(query, guild_id, user_id)

@classmethod
async def insert(cls, guild_id: int, user_id: int, role_id: int):
async def insert_by_guild(cls, guild_id: int, user_id: int, role_id: int):
query = """INSERT INTO persistent_roles (guild_id, user_id, role_id)
VALUES ($1, $2, $3)
ON CONFLICT (guild_id, user_id, role_id)
DO NOTHING"""
VALUES ($1, $2, $3)
ON CONFLICT (guild_id, user_id, role_id)
DO NOTHING"""

await cls.execute(query, guild_id, user_id, role_id)

@classmethod
async def remove(cls, guild_id: int, user_id: int):
async def delete_by_guild(cls, guild_id: int, user_id: int):
query = """DELETE FROM persistent_roles WHERE guild_id = $1 and user_id = $2"""
return await cls.fetch(query, guild_id, user_id)
2 changes: 1 addition & 1 deletion cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ async def main(ctx):
"bot.cogs.clashofcode",
"bot.cogs.roles",
"bot.cogs.poll",
"bot.cogs.levelling",
"bot.extensions.levelling",
"bot.cogs.persistent_roles",
)

Expand Down

0 comments on commit 75283dc

Please sign in to comment.