From 9ea40e5433a7641ec0171ff11c50c03e2225d6d7 Mon Sep 17 00:00:00 2001 From: Conatum Date: Sat, 19 Mar 2022 16:57:51 +0200 Subject: [PATCH] feature (sponsors): Add guild whitelist. Add `sponsor_hidden_in` app setting. Add new `GuildID` and `GuildIDList` setting types. --- bot/modules/sponsors/config.py | 32 ++++++++++++- bot/modules/sponsors/data.py | 2 +- bot/modules/sponsors/module.py | 13 ++--- bot/settings/setting_types.py | 71 ++++++++++++++++++++++++++++ data/migration/v10-v11/migration.sql | 8 +++- data/schema.sql | 8 +++- 6 files changed, 122 insertions(+), 12 deletions(-) diff --git a/bot/modules/sponsors/config.py b/bot/modules/sponsors/config.py index b4170a0f..c9d25b56 100644 --- a/bot/modules/sponsors/config.py +++ b/bot/modules/sponsors/config.py @@ -1,11 +1,12 @@ from cmdClient.checks import is_owner from settings import AppSettings, Setting, KeyValueData, ListData -from settings.setting_types import Message, String +from settings.setting_types import Message, String, GuildIDList from meta import client from core.data import app_config +from .data import guild_whitelist @AppSettings.attach_setting class sponsor_prompt(String, KeyValueData, Setting): @@ -37,6 +38,13 @@ class sponsor_prompt(String, KeyValueData, Setting): else: return None + @property + def success_response(self): + if self.value: + return "The sponsor prompt has been update." + else: + return "The sponsor prompt has been cleared." + @AppSettings.attach_setting class sponsor_message(Message, KeyValueData, Setting): @@ -60,3 +68,25 @@ class sponsor_message(Message, KeyValueData, Setting): _key = 'sponsor_message' _cmd_str = "{prefix}sponsors --edit" + + @property + def success_response(self): + return "The `sponsors` command message has been updated." + + +@AppSettings.attach_setting +class sponsor_guild_whitelist(GuildIDList, ListData, Setting): + attr_name = 'sponsor_guild_whitelist' + write_ward = is_owner + + category = 'Sponsors' + display_name = 'sponsor_hidden_in' + desc = "Guilds where the sponsor prompt is not displayed." + long_desc = ( + "A list of guilds where the sponsor prompt hint will be hidden (see the `sponsor_prompt` setting)." + ) + + _table_interface = guild_whitelist + _id_column = 'appid' + _data_column = 'guildid' + _force_unique = True diff --git a/bot/modules/sponsors/data.py b/bot/modules/sponsors/data.py index 39f4fcd3..c3a26d3a 100644 --- a/bot/modules/sponsors/data.py +++ b/bot/modules/sponsors/data.py @@ -1,4 +1,4 @@ from data import Table -sponsor_text = Table("sponsor_text") +guild_whitelist = Table("sponsor_guild_whitelist") diff --git a/bot/modules/sponsors/module.py b/bot/modules/sponsors/module.py index d709a16d..ae9ba299 100644 --- a/bot/modules/sponsors/module.py +++ b/bot/modules/sponsors/module.py @@ -16,11 +16,12 @@ sponsored_commands = {'profile', 'stats', 'weekly', 'monthly'} async def sponsor_reply_wrapper(func, ctx: LionContext, *args, **kwargs): if ctx.cmd and ctx.cmd.name in sponsored_commands: if (prompt := ctx.client.settings.sponsor_prompt.value): - sponsor_hint = discord.Embed( - description=prompt, - colour=discord.Colour.dark_theme() - ) - if 'embed' not in kwargs: - kwargs['embed'] = sponsor_hint + if not ctx.guild or ctx.guild.id not in ctx.client.settings.sponsor_guild_whitelist.value: + sponsor_hint = discord.Embed( + description=prompt, + colour=discord.Colour.dark_theme() + ) + if 'embed' not in kwargs: + kwargs['embed'] = sponsor_hint return await func(ctx, *args, **kwargs) diff --git a/bot/settings/setting_types.py b/bot/settings/setting_types.py index 565ed5a1..8c2863ad 100644 --- a/bot/settings/setting_types.py +++ b/bot/settings/setting_types.py @@ -473,6 +473,64 @@ class Emoji(SettingType): return str(data) +class GuildID(SettingType): + """ + Integer type for storing Guild IDs. Stores any snowflake. + + Types: + data: Optional[int] + The stored integer value. + value: Optional[int] + The stored integer value. + """ + accepts = "Any snowflake id." + + @classmethod + def _data_from_value(cls, id: int, value: Optional[bool], **kwargs): + """ + Both data and value are of type Optional[int]. + Directly return the provided value as data. + """ + return value + + @classmethod + def _data_to_value(cls, id: int, data: Optional[bool], **kwargs): + """ + Both data and value are of type Optional[int]. + Directly return the internal data as the value. + """ + return data + + @classmethod + async def _parse_userstr(cls, ctx: Context, id: int, userstr: str, **kwargs): + """ + Relies on integer casting to convert the user string + """ + if not userstr or userstr.lower() == "none": + return None + + try: + num = int(userstr) + except Exception: + raise UserInputError("Couldn't parse provided guild id.") from None + + return num + + @classmethod + def _format_data(cls, id: int, data: Optional[int], **kwargs): + """ + Return the string version of the data. + """ + if data is None: + return None + elif (guild := client.get_guild(data)): + return f"`{data}` ({guild.name})" + elif (row := client.data.guild_config.fetch(data)): + return f"`{data}` ({row.name})" + else: + return f"`{data}`" + + class Timezone(SettingType): """ Timezone type, storing a valid timezone string. @@ -1046,3 +1104,16 @@ class StringList(SettingList): "Write `--add` or `--remove` to add or remove strings." ) _setting = String + + +class GuildIDList(SettingList): + """ + List of guildids. + """ + accepts = ( + "Comma separated list of guild ids. Use `None` to unset. " + "Write `--add` or `--remove` to add or remove ids. " + "The provided ids are not verified in any way." + ) + + _setting = GuildID diff --git a/data/migration/v10-v11/migration.sql b/data/migration/v10-v11/migration.sql index 0e8740b0..09a75c73 100644 --- a/data/migration/v10-v11/migration.sql +++ b/data/migration/v10-v11/migration.sql @@ -10,13 +10,17 @@ CREATE TABLE AppConfig( -- Sponsor Data {{{ CREATE TABLE sponsor_guild_whitelist( - guildid INTEGER PRIMARY KEY + appid TEXT, + guildid BIGINT, + PRIMARY KEY(appid, guildid) ); -- }}} -- Topgg Data {{{ CREATE TABLE topgg_guild_whitelist( - guildid INTEGER PRIMARY KEY + appid TEXT, + guildid BIGINT, + PRIMARY KEY(appid, guildid) ); -- }}} diff --git a/data/schema.sql b/data/schema.sql index 97f468fd..69592305 100644 --- a/data/schema.sql +++ b/data/schema.sql @@ -807,13 +807,17 @@ create TABLE topgg( CREATE INDEX topgg_userid_timestamp ON topgg (userid, boostedTimestamp); CREATE TABLE topgg_guild_whitelist( - guildid INTEGER PRIMARY KEY + appid TEXT, + guildid BIGINT, + PRIMARY KEY(appid, guildid) ); -- }}} -- Sponsor Data {{{ CREATE TABLE sponsor_guild_whitelist( - guildid INTEGER PRIMARY KEY + appid TEXT, + guildid BIGINT, + PRIMARY KEY(appid, guildid) ); -- }}}