Skip to content

Commit

Permalink
finish config setting for now....
Browse files Browse the repository at this point in the history
  • Loading branch information
No767 committed Jul 3, 2024
1 parent b20725c commit 0f21263
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
58 changes: 38 additions & 20 deletions bot/cogs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime
import difflib
import itertools
import re
from typing import (
TYPE_CHECKING,
Annotated,
Expand Down Expand Up @@ -30,12 +31,14 @@

if TYPE_CHECKING:
from cogs.tickets import Tickets

from rodhaj import Rodhaj


UNKNOWN_ERROR_MESSAGE = (
"An unknown error happened. Please contact the dev team for assistance"
)
MENTION_REGEX = r"<@!?([0-9]+)>"


class BlocklistTicket(NamedTuple):
Expand Down Expand Up @@ -277,30 +280,24 @@ async def convert(self, ctx: GuildContext, argument: str) -> str:

class ConfigValueConverter(commands.Converter):
async def convert(self, ctx: GuildContext, argument: str) -> str:
true_options = ("yes", "y", "true", "t", "1", "enable", "on")
false_options = ("no", "n", "false", "f", "0", "disable", "off")
lowered = argument.lower()

# we need to check for whether people are silently passing boolean options or not
if lowered in [
"yes",
"y",
"true",
"t",
"1",
"enable",
"on",
"no",
"n",
"false",
"f",
"0",
"disable",
"off",
]:
if lowered in true_options or lowered in false_options:
raise commands.BadArgument(
f"Please use `{ctx.prefix or 'r>'}config toggle` to enable/disable boolean configuration options instead."
)

# TODO: Parse datetime/mentions here
# Double check mention regexes
# TODO: Somehow parse these mentions and safely store them
mention_re = re.compile(MENTION_REGEX)
if not mention_re.fullmatch(argument):
raise commands.BadArgument(
"Invalid mention used. Please use an valid mention instead"
)
# TODO: Parse datetime timedeltas here
return argument


Expand Down Expand Up @@ -725,7 +722,23 @@ async def config_set(
)
return

await ctx.send(f"{value}")
# I'm not joking but this is the only cleanest way I can think of doing this
# Noelle 2024
if key in "account_age":
clause = "SET account_age = $2"
elif key in "guild_age":
clause = "SET guild_age = $2"
else:
clause = "SET mention = $2"

query = f"""
UPDATE guild_config
{clause}
WHERE id = $1;
"""
await self.bot.pool.execute(query, ctx.guild.id, value)
self.get_guild_settings.cache_invalidate(ctx.guild.id)
await ctx.send(f"Set `{key}` to `{value}`")

@check_permissions(manage_guild=True)
@commands.guild_only()
Expand All @@ -752,13 +765,20 @@ async def config_toggle(
if not current_guild_settings:
raise RuntimeError("Guild settings could not be found")

# There is technically an faster method
# of directly modifying the subscripted path...
# But for the reason of autonomic guarantees, the whole entire dict should be modified
query = """
UPDATE guild_config
SET settings = $2::jsonb
WHERE id = $1;
"""
guild_dict = current_guild_settings.to_dict()
original_value = guild_dict.get(key)
if original_value and original_value is value:
await ctx.send(f"`{key}` is already set to `{value}`!")
return

guild_dict[key] = value
await self.bot.pool.execute(query, ctx.guild.id, guild_dict)
self.get_guild_settings.cache_invalidate(ctx.guild.id)
Expand All @@ -778,8 +798,6 @@ async def on_config_toggle_error(
):
if isinstance(error, commands.BadArgument):
await ctx.send(str(error))
else:
await ctx.send(str(error))

@check_permissions(manage_guild=True)
@commands.guild_only()
Expand Down
1 change: 1 addition & 0 deletions bot/migrations/V6__guild_settings.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
-- So these needs to be separate columns
ALTER TABLE IF EXISTS guild_config ADD COLUMN account_age INTERVAL DEFAULT ('2 hours'::interval) NOT NULL;
ALTER TABLE IF EXISTS guild_config ADD COLUMN guild_age INTERVAL DEFAULT ('2 days':: interval) NOT NULL;
ALTER TABLE IF EXISTS guild_config ADD COLUMN mention TEXT;

-- The guild settings is just an jsonb column that stores extra settings for the guild.
-- Misc settings like enabling an certain feature, etc.
Expand Down

0 comments on commit 0f21263

Please sign in to comment.