From 23d59175e10ce82834dbc120c3798c1290d5401e Mon Sep 17 00:00:00 2001 From: wlinator Date: Sat, 3 Aug 2024 14:24:55 -0400 Subject: [PATCH] chore: Update welcome template and boost module configuration commands --- config/JSON/strings.json | 23 ++- modules/config/__init__.py | 342 +++++-------------------------------- modules/config/c_boost.py | 98 +++++++++++ modules/config/c_greet.py | 2 +- modules/config/c_level.py | 136 +++++++++++++++ 5 files changed, 299 insertions(+), 302 deletions(-) create mode 100644 modules/config/c_boost.py create mode 100644 modules/config/c_level.py diff --git a/config/JSON/strings.json b/config/JSON/strings.json index f66751e..b59bc55 100644 --- a/config/JSON/strings.json +++ b/config/JSON/strings.json @@ -44,7 +44,28 @@ "config_welcome_module_disabled": "the greeting module was successfully disabled.", "config_welcome_template_field": "New Template:", "config_welcome_template_updated": "the welcome message template has been updated.", - "config_welcome_template_updated_footer": "An example will be sent next.", + "config_example_next_footer": "An example will be sent next.", + "config_boost_channel_set": "boost announcements will be sent in {0}.", + "config_boost_module_already_disabled": "the boost module was already disabled.", + "config_boost_module_disabled": "the boost module was successfully disabled.", + "config_boost_template_updated": "the boost message template has been updated.", + "config_boost_template_field": "New Template:", + "config_boost_image_updated": "the boost image has been updated.", + "config_boost_image_field": "New Image URL:", + "config_boost_image_original": "Original (default)", + "config_level_channel_set": "all level announcements will be sent in {0}.", + "config_level_current_channel_set": "members will receive level announcements in their current channel.", + "config_level_module_disabled": "the Lumi XP system was successfully disabled.", + "config_level_module_already_enabled": "the Lumi XP system was already enabled.", + "config_level_module_enabled": "the Lumi XP system was successfully enabled.", + "config_level_type_whimsical": "level announcements will be **sarcastic comments**.", + "config_level_type_generic": "level announcements will be **generic messages**.", + "config_level_type_example": "Example:", + "config_level_type_whimsical_example": "📈 | **lucas** Lol it took you this long to reach **Level 15**.", + "config_level_type_generic_example": "📈 | **lucas** you have reached **Level 15**.", + "config_level_template": "Template:", + "config_level_template_updated": "the level template was successfully updated.", + "config_level_module_disabled_warning": "Warning: this module is disabled, please do '/config levels enable'", "daily_already_claimed_author": "Already Claimed", "daily_already_claimed_description": "you can claim your daily reward again .", "daily_already_claimed_footer": "Daily reset is at 7 AM EST", diff --git a/modules/config/__init__.py b/modules/config/__init__.py index 7b6ef54..f9898b5 100644 --- a/modules/config/__init__.py +++ b/modules/config/__init__.py @@ -3,11 +3,15 @@ from discord.commands import SlashCommandGroup from discord.ext import bridge, commands from config.parser import JsonCache -from lib import formatter -from lib.embeds.boost import Boost -from lib.embeds.error import GenericErrors -from modules.config import c_show, c_birthday, c_greet, set_prefix, xp_reward -from services.config_service import GuildConfig +from modules.config import ( + c_show, + c_birthday, + c_greet, + c_boost, + c_level, + set_prefix, + xp_reward, +) strings = JsonCache.read_json("strings") @@ -102,7 +106,7 @@ class Config(commands.Cog): await c_show.cmd(ctx) @birthday_config.command(name="channel") - async def config_birthdays_channel(self, ctx, *, channel: discord.TextChannel): + async def config_birthdays_channel(self, ctx, channel: discord.TextChannel): await c_birthday.set_birthday_channel(ctx, channel) @birthday_config.command(name="disable") @@ -110,7 +114,7 @@ class Config(commands.Cog): await c_birthday.disable_birthday_module(ctx) @welcome_config.command(name="channel") - async def config_welcome_channel(self, ctx, *, channel: discord.TextChannel): + async def config_welcome_channel(self, ctx, channel: discord.TextChannel): await c_greet.set_welcome_channel(ctx, channel) @welcome_config.command(name="disable") @@ -118,314 +122,52 @@ class Config(commands.Cog): await c_greet.disable_welcome_module(ctx) @welcome_config.command(name="template") - async def config_welcome_template( - self, - ctx, - text: discord.Option(str, max_length=2000), - ): + @discord.commands.option(name="text", type=str, max_length=2000) + async def config_welcome_template(self, ctx, text): await c_greet.set_welcome_template(ctx, text) - @boost_config.command( - name="channel", - description="Set the boost announcements channel.", - ) - async def config_boosts_channel(self, ctx, *, channel: discord.TextChannel): - guild_config = GuildConfig(ctx.guild.id) - guild_config.boost_channel_id = channel.id - guild_config.push() + @boost_config.command(name="channel") + async def config_boosts_channel(self, ctx, channel: discord.TextChannel): + await c_boost.set_boost_channel(ctx, channel) - embed = discord.Embed( - color=discord.Color.orange(), - description=f"✅ | I will announce server boosts in {channel.mention}.", - ) - guild_icon = ( - ctx.guild.icon if ctx.guild.icon else "https://i.imgur.com/79XfsbS.png" - ) - embed.set_author(name="Server Configuration", icon_url=guild_icon) - - return await ctx.respond(embed=embed) - - @boost_config.command( - name="disable", - description="Disable boost announcements in this server.", - ) + @boost_config.command(name="disable") async def config_boosts_disable(self, ctx): - guild_config = GuildConfig(ctx.guild.id) + await c_boost.disable_boost_module(ctx) - embed = discord.Embed( - color=discord.Color.orange(), - ) - guild_icon = ( - ctx.guild.icon if ctx.guild.icon else "https://i.imgur.com/79XfsbS.png" - ) - embed.set_author(name="Server Configuration", icon_url=guild_icon) + @boost_config.command(name="template") + @discord.commands.option(name="text", type=str, max_length=2000) + async def config_boosts_template(self, ctx, text): + await c_boost.set_boost_template(ctx, text) - if not guild_config.boost_channel_id: - embed.description = "👍 | Boost announcements were already disabled." - return await ctx.respond(embed=embed) + @boost_config.command(name="image") + @discord.commands.option(name="url", type=str, max_length=2000) + async def config_boosts_image(self, ctx, url): + await c_boost.set_boost_image(ctx, url) - else: - guild_config.boost_channel_id = None - guild_config.boost_message = None - guild_config.push() - embed.description = "✅ | Boost announcements are successfully disabled." - return await ctx.respond(embed=embed) + @level_config.command(name="channel") + async def config_level_channel(self, ctx, channel: discord.TextChannel): + await c_level.set_level_channel(ctx, channel) - @boost_config.command( - name="template", - description="Make a custom boost announcement template.", - ) - async def config_boosts_template( - self, - ctx, - *, - text: discord.Option(str, max_length=2000), - ): - guild_config = GuildConfig(ctx.guild.id) - guild_config.boost_message = text - guild_config.push() - - embed = discord.Embed( - color=discord.Color.orange(), - description="✅ | The booster template was successfully updated.", - ) - guild_icon = ( - ctx.guild.icon if ctx.guild.icon else "https://i.imgur.com/79XfsbS.png" - ) - embed.add_field(name="Template", value=text, inline=False) - embed.add_field( - name="Example", - value="An example will be sent in a separate message.", - inline=False, - ) - embed.set_author(name="Server Configuration", icon_url=guild_icon) - await ctx.respond(embed=embed) - - embed = Boost.message(ctx.author, text, guild_config.boost_image_url) - return await ctx.send(embed=embed, content=ctx.author.mention) - - @boost_config.command( - name="image", - description="Add a custom image that will used for booster announcements.", - ) - async def config_boosts_image(self, ctx, *, image_url: str): - guild_config = GuildConfig(ctx.guild.id) - - if image_url.lower() == "original": - guild_config.boost_image_url = None - guild_config.push() - image_url = None - - elif not image_url.endswith(".jpg") and not image_url.lower().endswith(".png"): - return await ctx.respond(embed=GenericErrors.bad_url(ctx)) - - elif not image_url.startswith("http://") and not image_url.startswith( - "https://", - ): - return await ctx.respond(embed=GenericErrors.bad_url(ctx, "invalid URL.")) - - else: - guild_config.boost_image_url = image_url - guild_config.push() - - embed = discord.Embed( - color=discord.Color.orange(), - description="✅ | The booster image was successfully updated.", - ) - guild_icon = ( - ctx.guild.icon if ctx.guild.icon else "https://i.imgur.com/79XfsbS.png" - ) - embed.add_field( - name="Image", - value=image_url if image_url else "Original Image", - inline=False, - ) - embed.add_field( - name="Example", - value="An example will be sent in a separate message.", - inline=False, - ) - embed.set_author(name="Server Configuration", icon_url=guild_icon) - await ctx.respond(embed=embed) - - embed = Boost.message(ctx.author, guild_config.boost_message, image_url) - return await ctx.send(embed=embed, content=ctx.author.mention) - - @level_config.command( - name="channel", - description="Set the level announcements channel.", - ) - async def config_level_channel(self, ctx, *, channel: discord.TextChannel): - guild_config = GuildConfig(ctx.guild.id) - guild_config.level_channel_id = channel.id - guild_config.push() - - embed = discord.Embed( - color=discord.Color.orange(), - description=f"✅ | All level announcements will be sent in {channel.mention}.", - ) - guild_icon = ( - ctx.guild.icon if ctx.guild.icon else "https://i.imgur.com/79XfsbS.png" - ) - embed.set_author(name="Server Configuration", icon_url=guild_icon) - - if guild_config.level_message_type == 0: - embed.set_footer( - text="Warning: this module is disabled, please do '/config levels enable'", - ) - - return await ctx.respond(embed=embed) - - @level_config.command( - name="currentchannel", - description="Send level announcements in the member's current channel.", - ) + @level_config.command(name="currentchannel") async def config_level_samechannel(self, ctx): - guild_config = GuildConfig(ctx.guild.id) - guild_config.level_channel_id = None - guild_config.push() + await c_level.set_level_current_channel(ctx) - embed = discord.Embed( - color=discord.Color.orange(), - description="✅ | Members will receive level announcements in their current channel.", - ) - guild_icon = ( - ctx.guild.icon if ctx.guild.icon else "https://i.imgur.com/79XfsbS.png" - ) - embed.set_author(name="Server Configuration", icon_url=guild_icon) - - if guild_config.level_message_type == 0: - embed.set_footer( - text="Warning: this module is disabled, please do '/config levels enable'", - ) - - return await ctx.respond(embed=embed) - - @level_config.command( - name="disable", - description="Disable levels and the Lumi XP system.", - ) + @level_config.command(name="disable") async def config_level_disable(self, ctx): - guild_config = GuildConfig(ctx.guild.id) - guild_config.level_message_type = 0 - guild_config.push() + await c_level.disable_level_module(ctx) - embed = discord.Embed( - color=discord.Color.orange(), - description="✅ | The Lumi XP system was successfully disabled.", - ) - guild_icon = ( - ctx.guild.icon if ctx.guild.icon else "https://i.imgur.com/79XfsbS.png" - ) - embed.set_author(name="Server Configuration", icon_url=guild_icon) - - return await ctx.respond(embed=embed) - - @level_config.command( - name="enable", - description="Enable levels and the Lumi XP system.", - ) + @level_config.command(name="enable") async def config_level_enable(self, ctx): - guild_config = GuildConfig(ctx.guild.id) + await c_level.enable_level_module(ctx) - embed = discord.Embed( - color=discord.Color.orange(), - ) - guild_icon = ( - ctx.guild.icon if ctx.guild.icon else "https://i.imgur.com/79XfsbS.png" - ) - embed.set_author(name="Server Configuration", icon_url=guild_icon) + @level_config.command(name="type") + @discord.commands.option(name="type", choices=["whimsical", "generic"]) + async def config_level_type(self, ctx, type): + await c_level.set_level_type(ctx, type) - if guild_config.level_message_type != 0: - embed.description = "👍 | The Lumi XP system was already enabled." - return await ctx.respond(embed=embed) - - else: - guild_config.level_message_type = 1 - guild_config.push() - embed.description = "✅ | The Lumi XP system was successfully enabled." - embed.set_footer(text="Note: see '.help config' for more info.") - return await ctx.respond(embed=embed) - - @level_config.command( - name="type", - description="Set the level announcements type.", - ) - async def config_level_type( - self, - ctx, - *, - type: discord.Option(choices=["whimsical", "generic"]), - ): - guild_config = GuildConfig(ctx.guild.id) - - embed = discord.Embed( - color=discord.Color.orange(), - ) - guild_icon = ( - ctx.guild.icon if ctx.guild.icon else "https://i.imgur.com/79XfsbS.png" - ) - embed.set_author(name="Server Configuration", icon_url=guild_icon) - - if type == "whimsical": - guild_config.level_message_type = 1 - guild_config.level_message = None - guild_config.push() - - embed.description = "✅ | Level announcements will be sarcastic comments." - embed.add_field( - name="Example", - value="📈 | **lucas** Lol it took you this long to reach **Level 15**.", - inline=False, - ) - return await ctx.respond(embed=embed) - - else: - guild_config.level_message_type = 2 - guild_config.level_message = None - guild_config.push() - - embed.description = "✅ | Level announcements will be generic messages." - embed.add_field( - name="Example", - value="📈 | **lucas** you have reached **Level 15**.", - inline=False, - ) - return await ctx.respond(embed=embed) - - @level_config.command( - name="template", - description="Make a custom leveling template.", - ) - async def config_level_template( - self, - ctx, - *, - text: discord.Option(str, max_length=2000), - ): - guild_config = GuildConfig(ctx.guild.id) - guild_config.level_message = text - guild_config.push() - - preview = formatter.template(text, "Lucas", 15) - - embed = discord.Embed( - color=discord.Color.orange(), - description="✅ | The level template was successfully updated.", - ) - guild_icon = ( - ctx.guild.icon if ctx.guild.icon else "https://i.imgur.com/79XfsbS.png" - ) - embed.add_field(name="Template", value=text, inline=False) - embed.add_field(name="Example", value=preview, inline=False) - embed.set_author(name="Server Configuration", icon_url=guild_icon) - - if guild_config.level_message_type == 0: - embed.set_footer( - text="Warning: this module is disabled, please do '/config levels enable'", - ) - - return await ctx.respond(embed=embed) + @level_config.command(name="template") + async def config_level_template(self, ctx, text: str): + await c_level.set_level_template(ctx, text) def setup(client): diff --git a/modules/config/c_boost.py b/modules/config/c_boost.py new file mode 100644 index 0000000..68e0231 --- /dev/null +++ b/modules/config/c_boost.py @@ -0,0 +1,98 @@ +import discord +from lib.embed_builder import EmbedBuilder +from lib.constants import CONST +from services.config_service import GuildConfig +from lib.embeds.boost import Boost +from lib.embeds.error import GenericErrors + + +async def set_boost_channel(ctx, channel: discord.TextChannel): + guild_config = GuildConfig(ctx.guild.id) + guild_config.boost_channel_id = channel.id + guild_config.push() + + embed = EmbedBuilder().create_success_embed( + ctx=ctx, + author_text=CONST.STRINGS["config_author"], + description=CONST.STRINGS["config_boost_channel_set"].format(channel.mention), + ) + + return await ctx.respond(embed=embed) + + +async def disable_boost_module(ctx): + guild_config = GuildConfig(ctx.guild.id) + + if not guild_config.boost_channel_id: + embed = EmbedBuilder().create_warning_embed( + ctx=ctx, + author_text=CONST.STRINGS["config_author"], + description=CONST.STRINGS["config_boost_module_already_disabled"], + ) + else: + guild_config.boost_channel_id = None + guild_config.boost_message = None + guild_config.push() + embed = EmbedBuilder().create_success_embed( + ctx=ctx, + author_text=CONST.STRINGS["config_author"], + description=CONST.STRINGS["config_boost_module_disabled"], + ) + + return await ctx.respond(embed=embed) + + +async def set_boost_template(ctx, text: str): + guild_config = GuildConfig(ctx.guild.id) + guild_config.boost_message = text + guild_config.push() + + embed = EmbedBuilder().create_success_embed( + ctx=ctx, + author_text=CONST.STRINGS["config_author"], + description=CONST.STRINGS["config_boost_template_updated"], + footer_text=CONST.STRINGS["config_example_next_footer"], + ) + embed.add_field( + name=CONST.STRINGS["config_boost_template_field"], + value=f"```{text}```", + inline=False, + ) + + await ctx.respond(embed=embed) + + example_embed = Boost.message(ctx.author, text, guild_config.boost_image_url) + return await ctx.send(embed=example_embed, content=ctx.author.mention) + + +async def set_boost_image(ctx, image_url: str | None): + guild_config = GuildConfig(ctx.guild.id) + + if image_url is None or image_url.lower() == "original": + guild_config.boost_image_url = None + guild_config.push() + image_url = None + elif not image_url.endswith((".jpg", ".png")): + return await ctx.respond(embed=GenericErrors.bad_url(ctx)) + elif not image_url.startswith(("http://", "https://")): + return await ctx.respond(embed=GenericErrors.bad_url(ctx, "invalid URL.")) + else: + guild_config.boost_image_url = image_url + guild_config.push() + + embed = EmbedBuilder().create_success_embed( + ctx=ctx, + author_text=CONST.STRINGS["config_author"], + description=CONST.STRINGS["config_boost_image_updated"], + footer_text=CONST.STRINGS["config_example_next_footer"], + ) + embed.add_field( + name=CONST.STRINGS["config_boost_image_field"], + value=image_url or CONST.STRINGS["config_boost_image_original"], + inline=False, + ) + + await ctx.respond(embed=embed) + + example_embed = Boost.message(ctx.author, guild_config.boost_message, image_url) + return await ctx.send(embed=example_embed, content=ctx.author.mention) diff --git a/modules/config/c_greet.py b/modules/config/c_greet.py index 75d5393..3b5561f 100644 --- a/modules/config/c_greet.py +++ b/modules/config/c_greet.py @@ -50,7 +50,7 @@ async def set_welcome_template(ctx, text: str): ctx=ctx, author_text=CONST.STRINGS["config_author"], description=CONST.STRINGS["config_welcome_template_updated"], - footer_text=CONST.STRINGS["config_welcome_template_updated_footer"], + footer_text=CONST.STRINGS["config_example_next_footer"], ) embed.add_field( name=CONST.STRINGS["config_welcome_template_field"], diff --git a/modules/config/c_level.py b/modules/config/c_level.py new file mode 100644 index 0000000..e8ee746 --- /dev/null +++ b/modules/config/c_level.py @@ -0,0 +1,136 @@ +import discord +from lib.embed_builder import EmbedBuilder +from lib.constants import CONST +from services.config_service import GuildConfig +from lib import formatter + + +async def set_level_channel(ctx, channel: discord.TextChannel): + guild_config = GuildConfig(ctx.guild.id) + guild_config.level_channel_id = channel.id + guild_config.push() + + embed = EmbedBuilder().create_success_embed( + ctx=ctx, + author_text=CONST.STRINGS["config_author"], + description=CONST.STRINGS["config_level_channel_set"].format(channel.mention), + ) + + if guild_config.level_message_type == 0: + embed.set_footer(text=CONST.STRINGS["config_level_module_disabled_warning"]) + + return await ctx.respond(embed=embed) + + +async def set_level_current_channel(ctx): + guild_config = GuildConfig(ctx.guild.id) + guild_config.level_channel_id = None + guild_config.push() + + embed = EmbedBuilder().create_success_embed( + ctx=ctx, + author_text=CONST.STRINGS["config_author"], + description=CONST.STRINGS["config_level_current_channel_set"], + ) + + if guild_config.level_message_type == 0: + embed.set_footer(text=CONST.STRINGS["config_level_module_disabled_warning"]) + + return await ctx.respond(embed=embed) + + +async def disable_level_module(ctx): + guild_config = GuildConfig(ctx.guild.id) + guild_config.level_message_type = 0 + guild_config.push() + + embed = EmbedBuilder().create_success_embed( + ctx=ctx, + author_text=CONST.STRINGS["config_author"], + description=CONST.STRINGS["config_level_module_disabled"], + ) + + return await ctx.respond(embed=embed) + + +async def enable_level_module(ctx): + guild_config = GuildConfig(ctx.guild.id) + + if guild_config.level_message_type != 0: + embed = EmbedBuilder().create_info_embed( + ctx=ctx, + author_text=CONST.STRINGS["config_author"], + description=CONST.STRINGS["config_level_module_already_enabled"], + ) + else: + guild_config.level_message_type = 1 + guild_config.push() + embed = EmbedBuilder().create_success_embed( + ctx=ctx, + author_text=CONST.STRINGS["config_author"], + description=CONST.STRINGS["config_level_module_enabled"], + ) + + return await ctx.respond(embed=embed) + + +async def set_level_type(ctx, type: str): + guild_config = GuildConfig(ctx.guild.id) + + embed = EmbedBuilder().create_success_embed( + ctx=ctx, + author_text=CONST.STRINGS["config_author"], + ) + + guild_config.level_message = None + if type == "whimsical": + guild_config.level_message_type = 1 + guild_config.push() + + embed.description = CONST.STRINGS["config_level_type_whimsical"] + embed.add_field( + name=CONST.STRINGS["config_level_type_example"], + value=CONST.STRINGS["config_level_type_whimsical_example"], + inline=False, + ) + else: + guild_config.level_message_type = 2 + guild_config.push() + + embed.description = CONST.STRINGS["config_level_type_generic"] + embed.add_field( + name=CONST.STRINGS["config_level_type_example"], + value=CONST.STRINGS["config_level_type_generic_example"], + inline=False, + ) + + return await ctx.respond(embed=embed) + + +async def set_level_template(ctx, text: str): + guild_config = GuildConfig(ctx.guild.id) + guild_config.level_message = text + guild_config.push() + + preview = formatter.template(text, "Lucas", 15) + + embed = EmbedBuilder().create_success_embed( + ctx=ctx, + author_text=CONST.STRINGS["config_author"], + description=CONST.STRINGS["config_level_template_updated"], + ) + embed.add_field( + name=CONST.STRINGS["config_level_template"], + value=f"```{text}```", + inline=False, + ) + embed.add_field( + name=CONST.STRINGS["config_level_type_example"], + value=preview, + inline=False, + ) + + if guild_config.level_message_type == 0: + embed.set_footer(text=CONST.STRINGS["config_level_module_disabled_warning"]) + + return await ctx.respond(embed=embed)