From da9f9b02b139bfa35f7509090c74173b941178dd Mon Sep 17 00:00:00 2001 From: Conatum Date: Sat, 12 Mar 2022 17:27:25 +0200 Subject: [PATCH] feature (sponsors): Add sponsor command. Add new sponsor prompts. Add new sponsor command. Add `sponsor_text` table. Add sponsor global config settings. Update `Setting.command` to use new `widget`. Add custom `cmd_str` support to `Message` `SettingType`. --- bot/modules/__init__.py | 1 + bot/modules/sponsors/__init__.py | 5 ++ bot/modules/sponsors/commands.py | 27 +++++++++++ bot/modules/sponsors/config.py | 70 ++++++++++++++++++++++++++++ bot/modules/sponsors/data.py | 4 ++ bot/modules/sponsors/module.py | 27 +++++++++++ bot/modules/topgg/module.py | 8 +++- bot/settings/base.py | 6 +-- bot/settings/setting_types.py | 12 +++-- data/migration/v10-v11/migration.sql | 9 ++++ data/schema.sql | 11 ++++- 11 files changed, 170 insertions(+), 10 deletions(-) create mode 100644 bot/modules/sponsors/__init__.py create mode 100644 bot/modules/sponsors/commands.py create mode 100644 bot/modules/sponsors/config.py create mode 100644 bot/modules/sponsors/data.py create mode 100644 bot/modules/sponsors/module.py create mode 100644 data/migration/v10-v11/migration.sql diff --git a/bot/modules/__init__.py b/bot/modules/__init__.py index 20630a1b..ee4a7bb4 100644 --- a/bot/modules/__init__.py +++ b/bot/modules/__init__.py @@ -13,3 +13,4 @@ from .renting import * from .moderation import * from .accountability import * from .plugins import * +from .sponsors import * diff --git a/bot/modules/sponsors/__init__.py b/bot/modules/sponsors/__init__.py new file mode 100644 index 00000000..615a9085 --- /dev/null +++ b/bot/modules/sponsors/__init__.py @@ -0,0 +1,5 @@ +from . import module + +from . import data +from . import config +from . import commands diff --git a/bot/modules/sponsors/commands.py b/bot/modules/sponsors/commands.py new file mode 100644 index 00000000..e937739c --- /dev/null +++ b/bot/modules/sponsors/commands.py @@ -0,0 +1,27 @@ +from cmdClient.checks import is_owner + +from .module import module +from .config import settings + + +@module.cmd( + name="sponsors", + group="Meta", + desc="Check out our wonderful partners!", + flags=('edit', 'prompt') +) +async def cmd_sponsors(ctx, flags): + """ + Usage``: + {prefix}sponsors + """ + if await is_owner.run(ctx) and any(flags.values()): + if flags['edit']: + # Run edit setting command + await settings.sponsor_message.command(ctx, 0) + elif flags['prompt']: + # Run prompt setting command + await settings.sponsor_prompt.command(ctx, 0) + else: + # Display message + await ctx.reply(**settings.sponsor_message.args(ctx)) diff --git a/bot/modules/sponsors/config.py b/bot/modules/sponsors/config.py new file mode 100644 index 00000000..0cda5bc6 --- /dev/null +++ b/bot/modules/sponsors/config.py @@ -0,0 +1,70 @@ +from cmdClient.checks import is_owner + +from settings.base import Setting, ColumnData, ObjectSettings +from settings.setting_types import Message, String + +from meta import client +from utils.lib import DotDict + +from .data import sponsor_text + + +class SponsorSettings(ObjectSettings): + settings = DotDict() + pass + + +@SponsorSettings.attach_setting +class sponsor_prompt(String, ColumnData, Setting): + attr_name = 'sponsor_prompt' + _default = "Type {prefix}sponsors to check our wonderful partners!" + + write_ward = is_owner + + display_name = 'sponsor_prompt' + desc = "Text to send after core commands to encourage checking `sponsors`." + long_desc = ( + "Text posted after several commands to encourage users to check the `sponsors` command. " + "Occurences of `{{prefix}}` will be replaced by the bot prefix." + ) + + _quote = False + + _data_column = 'prompt_text' + _table_interface = sponsor_text + _id_column = 'ID' + _upsert = True + _create_row = True + + @classmethod + def _data_to_value(cls, id, data, **kwargs): + if data: + return data.replace("{prefix}", client.prefix) + else: + return None + + +@SponsorSettings.attach_setting +class sponsor_message(Message, ColumnData, Setting): + attr_name = 'sponsor_message' + _default = '{"content": "Coming Soon!"}' + + write_ward = is_owner + + display_name = 'sponsor_message' + desc = "`sponsors` command response." + + long_desc = ( + "Message to reply with when a user runs the `sponsors` command." + ) + + _data_column = 'command_response' + _table_interface = sponsor_text + _id_column = 'ID' + _upsert = True + _create_row = True + + _cmd_str = "{prefix}sponsors --edit" + + +settings = SponsorSettings(0) diff --git a/bot/modules/sponsors/data.py b/bot/modules/sponsors/data.py new file mode 100644 index 00000000..39f4fcd3 --- /dev/null +++ b/bot/modules/sponsors/data.py @@ -0,0 +1,4 @@ +from data import Table + + +sponsor_text = Table("sponsor_text") diff --git a/bot/modules/sponsors/module.py b/bot/modules/sponsors/module.py new file mode 100644 index 00000000..9f08c0c1 --- /dev/null +++ b/bot/modules/sponsors/module.py @@ -0,0 +1,27 @@ +import discord + +from LionModule import LionModule +from LionContext import LionContext + +from meta import client + +from .config import settings + + +module = LionModule("Sponsor") + + +sponsored_commands = {'profile', 'stats', 'weekly', 'monthly'} + + +@LionContext.reply.add_wrapper +async def sponsor_reply_wrapper(func, ctx: LionContext, *args, **kwargs): + if ctx.cmd and ctx.cmd.name in sponsored_commands: + sponsor_hint = discord.Embed( + description=settings.sponsor_prompt.value, + colour=discord.Colour.dark_theme() + ) + if 'embed' not in kwargs: + kwargs['embed'] = sponsor_hint + + return await func(ctx, *args, **kwargs) diff --git a/bot/modules/topgg/module.py b/bot/modules/topgg/module.py index d2ff79f5..c03f4e2a 100644 --- a/bot/modules/topgg/module.py +++ b/bot/modules/topgg/module.py @@ -2,6 +2,8 @@ from LionModule import LionModule from LionContext import LionContext from core.lion import Lion +from modules.sponsors.module import sponsored_commands + from .utils import get_last_voted_timestamp, lion_loveemote, lion_yayemote from .webhook import init_webhook @@ -39,12 +41,14 @@ boostfree_commands = {'config', 'pomodoro'} async def topgg_reply_wrapper(func, ctx: LionContext, *args, suggest_vote=True, **kwargs): if not suggest_vote: pass - elif ctx.cmd and (ctx.cmd.name in boostfree_commands or ctx.cmd.group in boostfree_groups): + elif not ctx.cmd: + pass + elif ctx.cmd.name in boostfree_commands or ctx.cmd.group in boostfree_groups: pass elif not get_last_voted_timestamp(ctx.author.id): upvote_info_formatted = upvote_info.format(lion_yayemote, ctx.best_prefix, lion_loveemote) - if 'embed' in kwargs: + if 'embed' in kwargs and ctx.cmd.name not in sponsored_commands: # Add message as an extra embed field kwargs['embed'].add_field( name="\u200b", diff --git a/bot/settings/base.py b/bot/settings/base.py index 8b6f67a1..44678a97 100644 --- a/bot/settings/base.py +++ b/bot/settings/base.py @@ -201,13 +201,13 @@ class Setting: raise NotImplementedError @classmethod - async def command(cls, ctx, id): + async def command(cls, ctx, id, flags=()): """ Standardised command viewing/setting interface for the setting. """ - if not ctx.args: + if not ctx.args and not ctx.msg.attachments: # View config embed for provided cls - await ctx.reply(embed=cls.get(id).embed) + await cls.get(id).widget(ctx, flags=flags) else: # Check the write ward if cls.write_ward and not await cls.write_ward.run(ctx): diff --git a/bot/settings/setting_types.py b/bot/settings/setting_types.py index 4b5e1dd3..4502c106 100644 --- a/bot/settings/setting_types.py +++ b/bot/settings/setting_types.py @@ -713,6 +713,8 @@ class Message(SettingType): _substitution_desc = { } + _cmd_str = '{prefix} config {setting}' + @classmethod def _data_from_value(cls, id, value, **kwargs): if value is None: @@ -844,14 +846,16 @@ class Message(SettingType): embed.add_field( name="Setting Guide", value=( - "• For plain text without an embed, use `{prefix}config {setting} `.\n" + "• For plain text without an embed, use `{cmd_str} `.\n" "• To include an embed, build the message [here]({builder}) " - "and upload the json code as a file with the `{prefix}config {setting}` command.\n" - "• To reset the message to the default, use `{prefix}config {setting} None`." + "and upload the json code as a file with the `{cmd_str}` command.\n" + "• To reset the message to the default, use `{cmd_str} None`." + ).format( + cmd_str=self._cmd_str, + builder="https://glitchii.github.io/embedbuilder/?editor=gui" ).format( prefix=ctx.best_prefix, setting=self.display_name, - builder="https://glitchii.github.io/embedbuilder/?editor=gui" ), inline=False ) diff --git a/data/migration/v10-v11/migration.sql b/data/migration/v10-v11/migration.sql new file mode 100644 index 00000000..6c3b1f69 --- /dev/null +++ b/data/migration/v10-v11/migration.sql @@ -0,0 +1,9 @@ +-- Sponsor Data {{{ +CREATE TABLE sponsor_text( + ID INTEGER PRIMARY KEY DEFAULT 0, + prompt_text TEXT, + command_response TEXT +); +-- }}} + +INSERT INTO VersionHistory (version, author) VALUES (11, 'v10-v11 migration'); diff --git a/data/schema.sql b/data/schema.sql index 5287648e..de7ee92f 100644 --- a/data/schema.sql +++ b/data/schema.sql @@ -4,7 +4,7 @@ CREATE TABLE VersionHistory( time TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, author TEXT ); -INSERT INTO VersionHistory (version, author) VALUES (10, 'Initial Creation'); +INSERT INTO VersionHistory (version, author) VALUES (11, 'Initial Creation'); CREATE OR REPLACE FUNCTION update_timestamp_column() @@ -38,6 +38,15 @@ CREATE TABLE global_guild_blacklist( -- }}} +-- Sponsor Data {{{ +CREATE TABLE sponsor_text( + ID INTEGER PRIMARY KEY DEFAULT 0, + prompt_text TEXT, + command_response TEXT +); +-- }}} + + -- User configuration data {{{ CREATE TABLE user_config( userid BIGINT PRIMARY KEY,