Skip to content

Commit

Permalink
command(gamification): add support for a custom level-up message
Browse files Browse the repository at this point in the history
Allows admin to use `/config gamification` to set a custom level-up
message for users. It supports some predefined tokens: %%level%%,
%%date%% and %%time%%.

Fixes: TheBastionBot#1081
  • Loading branch information
cotton105 committed Jan 12, 2025
1 parent cdaa78f commit ecc3b05
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/commands/config/gamification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class GamificationCommand extends Command {
description: "The channel where the level up messages will be sent.",
channel_types: [ ChannelType.GuildText ],
},
{
type: ApplicationCommandOptionType.String,
name: "message",
description: "A custom message to send on level up. Supports tokens: %%level%%, %%date%%, %%time%%."
},
{
type: ApplicationCommandOptionType.Number,
name: "multiplier",
Expand All @@ -40,6 +45,7 @@ class GamificationCommand extends Command {
await interaction.deferReply();
const messages = interaction.options.getBoolean("messages");
const channel = interaction.options.getChannel("channel");
const customMessage = interaction.options.getString("message");
const multiplier = interaction.options.getNumber("multiplier");

// check for premium membership
Expand All @@ -55,6 +61,7 @@ class GamificationCommand extends Command {
guildDocument.gamification = messages || multiplier ? true : guildDocument.gamification ? undefined : true;
guildDocument.gamificationMessages = typeof messages === "boolean" ? messages : guildDocument.gamificationMessages || undefined;
guildDocument.gamificationChannel = channel?.id || undefined;
guildDocument.gamificationCustomMessage = customMessage || undefined;
guildDocument.gamificationMultiplier = multiplier || guildDocument.gamificationMultiplier || undefined;

await guildDocument.save();
Expand Down
18 changes: 17 additions & 1 deletion src/listeners/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,23 @@ class MessageCreateListener extends Listener<"messageCreate"> {

// achievement message
if (guildDocument.gamificationMessages) {
const gamificationMessage = (message.client as Client).locales.getText(message.guild.preferredLocale, "leveledUp", { level: `Level ${ computedLevel }` });
let gamificationMessage = guildDocument.gamificationCustomMessage;
if (guildDocument.gamificationCustomMessage) {
const now = new Date();
const [year, month, day, hour, minute] = [
() => now.getUTCFullYear().toString(),
() => (now.getUTCMonth() + 1).toString().padStart(2, "0"),
() => now.getUTCDate().toString(),
() => now.getUTCHours().toString(),
() => now.getUTCMinutes().toString()
];
gamificationMessage = gamificationMessage
.replaceAll(/%%level%%/g, `${computedLevel}`)
.replaceAll(/%%date%%/g, `${year()}-${month()}-${day()}`)
.replaceAll(/%%time%%/g, `${hour()}:${minute()}`)
} else {
gamificationMessage = (message.client as Client).locales.getText(message.guild.preferredLocale, "leveledUp", { level: `Level ${ computedLevel }` });
}

if (guildDocument.gamificationChannel && message.guild.channels.cache.has(guildDocument.gamificationChannel)) {
(message.guild.channels.cache.get(guildDocument.gamificationChannel) as GuildTextBasedChannel)
Expand Down
4 changes: 4 additions & 0 deletions src/models/Guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface Guild {
musicRole?: string;
// gamification
gamification?: boolean;
gamificationCustomMessage?: string;
gamificationMessages?: boolean;
gamificationChannel?: string;
gamificationMultiplier?: number;
Expand Down Expand Up @@ -110,6 +111,9 @@ export default mongoose.model<Guild & mongoose.Document>("Guild", new mongoose.S
gamification: {
type: Boolean,
},
gamificationCustomMessage: {
type: String
},
gamificationMessages: {
type: Boolean,
},
Expand Down

0 comments on commit ecc3b05

Please sign in to comment.