Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/role_reacts' into featur…
Browse files Browse the repository at this point in the history
…e/role_reacts
  • Loading branch information
BuildTools committed Nov 16, 2023
2 parents 2f6129a + 3c12fba commit fa92efd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
15 changes: 9 additions & 6 deletions src/heckbot/adapter/sqlite_adaptor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from __future__ import annotations

import sqlite3
import threading
from typing import Any, Optional
from typing import Any
from typing import Optional


class SqliteAdaptor:
def __init__(
self
self,
) -> None:
"""
Constructor method
Expand All @@ -29,7 +32,7 @@ def commit_and_close(self):

def run_query(
self, query: str,
params: Optional[tuple] = None
params: tuple | None = None,
) -> list[dict[str, Any]]:
try:
if params is None:
Expand All @@ -39,12 +42,12 @@ def run_query(
except sqlite3.Error as ex:
print(
f'Got exception {ex} when running query '
f'{query} with params {params}'
f'{query} with params {params}',
)

def run_query_many(
self, query: str,
params_list: Optional[list[tuple]] = None
params_list: list[tuple] | None = None,
) -> list[dict[str, Any]]:
try:
if params_list is None:
Expand All @@ -54,5 +57,5 @@ def run_query_many(
except sqlite3.Error as ex:
print(
f'Got exception {ex} when running query many '
f'{query} with params list {params_list}'
f'{query} with params list {params_list}',
)
61 changes: 32 additions & 29 deletions src/heckbot/cogs/roles.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import annotations

import discord
from discord import RawReactionActionEvent, Role
from discord import RawReactionActionEvent
from discord import Role
from discord.ext import commands
from discord.ext.commands import Context
from heckbot.adapter.sqlite_adaptor import SqliteAdaptor

from bot import HeckBot
from heckbot.adapter.sqlite_adaptor import SqliteAdaptor


class Roles(commands.Cog):
Expand Down Expand Up @@ -57,14 +58,14 @@ def __init__(
@commands.bot_has_permissions(manage_roles=True)
async def create_role(
self, ctx: Context, name: str, description: str,
category: str, emoji: str
category: str, emoji: str,
):
if not any(r.name == name for r in ctx.guild.roles):
await ctx.guild.create_role(name=name)
guild_id = str(ctx.guild.id)
results = self._db.run_query(
'''SELECT channel_id, message_id, role_category FROM role_messages
WHERE guild_id=?;''', (guild_id,)
'''SELECT channel_id, message_id, role_category FROM role_messages
WHERE guild_id=?;''', (guild_id,),
)
if len(results) < 1:
return
Expand All @@ -89,32 +90,32 @@ async def create_role(
message = await channel.send(
f'**{category}**\n'
'--------------------------\n'
f'{emoji} for {description}'
f'{emoji} for {description}',
)
await message.add_reaction(emoji)
message_params_list.append((guild_id, str(channel.id), str(message.id)))
self._db.run_query_many(
'''INSERT INTO role_messages (guild_id, channel_id, message_id)
VALUES (?, ?, ?);''', message_params_list
'''INSERT INTO role_messages (guild_id, channel_id, message_id)
VALUES (?, ?, ?);''', message_params_list,
)
self._db.run_query_many(
'''INSERT INTO roles
(guild_id, role_name, role_description,
'''INSERT INTO roles
(guild_id, role_name, role_description,
role_category, role_react)
VALUES (?, ?, ?, ?, ?);''', roles_params_list
VALUES (?, ?, ?, ?, ?);''', roles_params_list,
)
self._db.commit_and_close()

@commands.command(aliases=['deleterole', 'delrole', 'removerole', 'rmrole'])
@commands.has_permissions(manage_roles=True)
@commands.bot_has_permissions(manage_roles=True)
async def delete_role(
self, ctx: Context, name: str
self, ctx: Context, name: str,
):
guild_id = str(ctx.guild.id)
role_rows = self._db.run_query(
'''SELECT role_react, role_description, role_category, role_react FROM roles
WHERE guild_id=? AND role_name=?;''', (guild_id, name)
WHERE guild_id=? AND role_name=?;''', (guild_id, name),
)
if len(role_rows) < 1:
return
Expand All @@ -123,8 +124,8 @@ async def delete_role(
desc = role_row['role_description']
category = role_row['role_category']
message_rows = self._db.run_query(
'''SELECT channel_id, message_id FROM role_messages
WHERE guild_id=? AND role_category=?;''', (guild_id, category)
'''SELECT channel_id, message_id FROM role_messages
WHERE guild_id=? AND role_category=?;''', (guild_id, category),
)
role_string = f'{react} for {desc}'
# Doesn't delete the role from the guild, just removes it from role
Expand All @@ -135,13 +136,13 @@ async def delete_role(
channel = await ctx.guild.fetch_channel(channel_id)
message = await channel.fetch_message(message_id)
content = message.content.replace(
role_string, ''
role_string, '',
).replace('\n\n', '\n')
await message.edit(content=content)
await message.clear_reaction(react)
self._db.run_query(
'''DELETE FROM roles WHERE guild_id=? AND role_name=?;''',
(guild_id, name)
(guild_id, name),
)
self._db.commit_and_close()

Expand All @@ -154,7 +155,7 @@ async def create_roles_message(self, ctx: Context):
:param ctx: Context of the command
"""
result = self._db.run_query(
'''SELECT role_name, role_description, role_category, role_react
'''SELECT role_name, role_description, role_category, role_react
FROM roles WHERE guild_id=?
AND role_opt_in=TRUE;
''',
Expand Down Expand Up @@ -187,12 +188,12 @@ async def create_roles_message(self, ctx: Context):
params.append((
str(ctx.guild.id),
str(ctx.channel.id),
str(message.id)
str(message.id),
))
self._db.run_query_many(
'''INSERT INTO role_messages (guild_id, channel_id, message_id)
'''INSERT INTO role_messages (guild_id, channel_id, message_id)
VALUES (?, ?, ?);''',
params
params,
)
self._db.commit_and_close()
await ctx.message.delete()
Expand All @@ -206,18 +207,20 @@ async def _fetch_roles_for_reaction_change(
guild = await self._bot.fetch_guild(payload.guild_id)
role_react_message = self._db.run_query(
'''SELECT COUNT(*) AS num_messages FROM role_messages
WHERE guild_id=?
AND channel_id=?
WHERE guild_id=?
AND channel_id=?
AND message_id=?;''',
(guild_id, channel_id, message_id)
(guild_id, channel_id, message_id),
)[0]['num_messages'] > 0
if role_react_message:
role_names = [r['role_name'] for r in self._db.run_query(
'''SELECT role_name FROM roles
WHERE guild_id=?
role_names = [
r['role_name'] for r in self._db.run_query(
'''SELECT role_name FROM roles
WHERE guild_id=?
AND role_react=?;''',
(guild_id, str(payload.emoji))
)]
(guild_id, str(payload.emoji)),
)
]
roles = [
discord.utils.get(guild.roles, name=role_name)
for role_name in role_names
Expand Down

0 comments on commit fa92efd

Please sign in to comment.