1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 20:23:12 +00:00

Welcome messages global fix

This commit is contained in:
wlinator 2024-03-07 14:26:11 -05:00
parent dbf3159205
commit 831cba1918
9 changed files with 116 additions and 52 deletions

View file

@ -11,7 +11,7 @@
"ping": "\uD83C\uDFD3 | **{0}** I'm online.",
"restarting": "Restarting Racu...",
"restart_error": "Error executing the script: {0}",
"intro_muted": "You're muted in the Rave Cave. You can't perform this command.",
"intro_muted": "REMOVED STRING",
"intro_no_perms": "It seems that you don't have permission to do that!",
"intro_start_descr": "This command will serve as a questionnaire for you entry to {0}. Please keep your answers \"PG-13\"",
"intro_start_short": "Click the blue button to use the short form, this one has 6 questions.",

View file

@ -79,6 +79,7 @@ CREATE TABLE guild_config (
command_channel_id BIGINT, /* NULL: users can do XP & Currency commands everywhere. */
intro_channel_id BIGINT,
welcome_channel_id BIGINT,
welcome_message TEXT,
level_channel_id BIGINT, /* level-up messages, if NULL the level-up message will be shown in current msg channel*/
level_message TEXT, /* if NOT NULL and LEVEL_TYPE = 2, this can be a custom level up message. */
level_message_type TINYINT(1) NOT NULL DEFAULT 1, /* 0: no level up messages, 1: levels.en-US.json, 2: generic message */

26
db/init/v2-update.sql Normal file
View file

@ -0,0 +1,26 @@
ALTER TABLE XP
ADD COLUMN guild_id BIGINT NOT NULL;
CREATE TABLE guild_config (
guild_id BIGINT NOT NULL,
birthday_channel_id BIGINT,
command_channel_id BIGINT, /* NULL: users can do XP & Currency commands everywhere. */
intro_channel_id BIGINT,
welcome_channel_id BIGINT,
level_channel_id BIGINT, /* level-up messages, if NULL the level-up message will be shown in current msg channel*/
level_message TEXT, /* if NOT NULL and LEVEL_TYPE = 2, this can be a custom level up message. */
level_message_type TINYINT(1) NOT NULL DEFAULT 1, /* 0: no level up messages, 1: levels.en-US.json, 2: generic message */
PRIMARY KEY (guild_id)
);
CREATE TABLE blacklist_user (
user_id BIGINT NOT NULL,
reason TEXT,
timestamp TIMESTAMP NOT NULL DEFAULT NOW(),
active BOOLEAN DEFAULT TRUE,
PRIMARY KEY (user_id)
);
UPDATE XP
SET guild_id = 719227135151046699
WHERE guild_id IS NULL;

View file

@ -1,8 +1,24 @@
import discord
from lib import formatter
from services.Xp import Xp
def welcome_message(member, template=None):
embed = discord.Embed(
color=discord.Color.embed_background(),
description=f"_ _\n**Welcome** to **{member.guild.name}**"
)
if template:
embed.description += "↓↓↓\n" + formatter.template(template, member.name)
embed.set_thumbnail(url=member.display_avatar)
return embed
def command_error_1(error):
embed = discord.Embed(
color=discord.Color.red(),

21
lib/formatter.py Normal file
View file

@ -0,0 +1,21 @@
def template(text, username, level=None):
"""
Replaces placeholders in the given text with actual values.
Args:
text (str): The template text containing placeholders.
username (str): The username to replace "{user}" placeholder.
level (int, optional): The level to replace "{level}" placeholder. Defaults to None.
Returns:
str: The formatted text.
"""
replacements = {
"{user}": username,
"{level}": str(level) if level is not None else ""
}
for placeholder, value in replacements.items():
text = text.replace(placeholder, value)
return text

44
main.py
View file

@ -7,11 +7,12 @@ import discord
from discord.ext import commands
from dotenv import load_dotenv
import lib.resources
from lib import embeds
from config import json_loader
from handlers.ReactionHandler import ReactionHandler
from handlers.XPHandler import XPHandler
from handlers import LoggingHandler
from services.GuildConfig import GuildConfig
load_dotenv('.env')
instance = os.getenv("INSTANCE")
@ -19,14 +20,9 @@ instance = os.getenv("INSTANCE")
client = discord.Bot(
owner_id=os.getenv('OWNER_ID'),
intents=discord.Intents.all(),
activity=discord.Activity(
name="The Rave Cave",
type=discord.ActivityType.listening,
),
status=discord.Status.online
)
logs = LoggingHandler.setup_logger()
@ -64,32 +60,21 @@ async def on_message(message):
@client.event
async def on_member_join(member):
guild = member.guild
config = GuildConfig(member.guild.id)
if guild.id != 719227135151046699:
if (not config.welcome_channel_id
# comment next line if debugging greetings
or instance.lower() != "main"
):
return
# remove if debugging welcome messages:
if instance.lower() != "main":
return
embed = embeds.welcome_message(member, config.welcome_message)
welcome_channel_id = 721862236112420915
rules_channel_id = 719665850373898290
introductions_channel_id = 973619250507972618
rules_channel = guild.get_channel(rules_channel_id)
introductions_channel = guild.get_channel(introductions_channel_id)
embed = discord.Embed(
color=discord.Color.embed_background(),
description=f"_ _\n**Welcome** to **The Rave Cave** ↓↓↓\n"
f"[rules]({rules_channel.jump_url}) - "
f"[introductions]({introductions_channel.jump_url})\n_ _"
)
embed.set_thumbnail(url=member.display_avatar)
await guild.get_channel(welcome_channel_id).send(embed=embed, content=member.mention)
try:
await member.guild.get_channel(config.welcome_channel_id).send(embed=embed, content=member.mention)
except Exception as e:
logs.info(f"[GreetingHandler] Message not sent in '{member.guild.name}'. Channel ID may be invalid. {e}")
@client.event
@ -144,7 +129,8 @@ async def on_application_command_error(ctx, error) -> None:
logs.info(f"[CommandHandler] Racu is missing permissions: {ctx.command.qualified_name}")
elif isinstance(error, discord.CheckFailure) or isinstance(error, commands.CheckFailure):
logs.info(f"[CommandHandler] {ctx.author.name} tried to do \"/{ctx.command.qualified_name}\" but a check returned False.")
logs.info(
f"[CommandHandler] {ctx.author.name} tried to do \"/{ctx.command.qualified_name}\" but a check returned False.")
else:
logs.error(f"[CommandHandler] on_application_command_error: {error}", exc_info=True)

View file

@ -4,7 +4,7 @@ import discord
from discord.ext import commands
from discord.commands import SlashCommandGroup
from services.GuildConfig import GuildConfig
from services.Xp import Xp
from lib import formatter, embeds
from main import strings
@ -183,6 +183,28 @@ class ConfigCog(commands.Cog):
embed.description = "✅ | The greeting module was successfully disabled."
return await ctx.respond(embed=embed)
@welcome_config.command(
name="template",
description="Racu will use this template text in the \"description\" field in the welcome embed."
)
async def config_welcome_template(self, ctx, *, text: discord.Option(str, max_length=2000)):
guild_config = GuildConfig(ctx.guild.id)
guild_config.welcome_message = text
guild_config.push()
embed = discord.Embed(
color=discord.Color.orange(),
description=f"✅ | The greeting 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 = embeds.welcome_message(ctx.author, text)
return await ctx.send(embed=embed, content=ctx.author.mention)
@level_config.command(
name="channel",
description="Sets the channel for level announcements."
@ -303,12 +325,12 @@ class ConfigCog(commands.Cog):
name="template",
description="If set, Racu will only use this template for level announcements. See '/config help' for info."
)
async def config_level_template(self, ctx, *, text: discord.Option(str, max_length=60)):
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 = Xp.level_template_format(text, "Lucas", 15)
preview = formatter.template(text, "Lucas", 15)
embed = discord.Embed(
color=discord.Color.orange(),

View file

@ -12,6 +12,7 @@ class GuildConfig:
self.command_channel_id = None
self.intro_channel_id = None
self.welcome_channel_id = None
self.welcome_message = None
self.level_channel_id = None
self.level_message = None
self.level_message_type = 1
@ -24,19 +25,21 @@ class GuildConfig:
"""
query = """
SELECT birthday_channel_id, command_channel_id, intro_channel_id,
welcome_channel_id, level_channel_id, level_message, level_message_type
welcome_channel_id, welcome_message, level_channel_id,
level_message, level_message_type
FROM guild_config WHERE guild_id = %s
"""
try:
(birthday_channel_id, command_channel_id, intro_channel_id,
welcome_channel_id, level_channel_id, level_message, level_message_type) = \
welcome_channel_id, welcome_message, level_channel_id, level_message, level_message_type) = \
database.select_query(query, (self.guild_id,))[0]
self.birthday_channel_id = birthday_channel_id
self.command_channel_id = command_channel_id
self.intro_channel_id = intro_channel_id
self.welcome_channel_id = welcome_channel_id
self.welcome_message = welcome_message
self.level_channel_id = level_channel_id
self.level_message = level_message
self.level_message_type = level_message_type
@ -54,6 +57,7 @@ class GuildConfig:
command_channel_id = %s,
intro_channel_id = %s,
welcome_channel_id = %s,
welcome_message = %s,
level_channel_id = %s,
level_message = %s,
level_message_type = %s
@ -61,7 +65,7 @@ class GuildConfig:
"""
database.execute_query(query, (self.birthday_channel_id, self.command_channel_id,
self.intro_channel_id, self.welcome_channel_id,
self.intro_channel_id, self.welcome_channel_id, self.welcome_message,
self.level_channel_id, self.level_message,
self.level_message_type, self.guild_id))

View file

@ -150,15 +150,3 @@ class Xp:
# For levels below 10 and levels 110 and above
return 9 * current_level + 27 if current_level < 30 else 42 * current_level + 27
@staticmethod
def level_template_format(text, username, level):
replacements = {
"{user}": username,
"{level}": str(level)
}
for placeholder, value in replacements.items():
text = text.replace(placeholder, value)
return text