From 55b60742b8e3c8edf30ade81d364e4282627b2a9 Mon Sep 17 00:00:00 2001 From: AnxietyisReal <96593068+AnxietyisReal@users.noreply.github.com> Date: Fri, 30 Jun 2023 19:24:34 +1000 Subject: [PATCH] Add level-up notification setting (False by default) --- src/commands/rank.ts | 17 +++++++++++++++-- src/models/userLevels.ts | 9 +++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/commands/rank.ts b/src/commands/rank.ts index d2b43fe..2ac230e 100644 --- a/src/commands/rank.ts +++ b/src/commands/rank.ts @@ -24,7 +24,7 @@ export default { const index = allData.sort((a, b) => b.messages - a.messages).map(x => x._id).indexOf(member.id) + 1; const memberDifference = userData.messages - client.userLevels.algorithm(userData.level); const levelDifference = client.userLevels.algorithm(userData.level+1) - client.userLevels.algorithm(userData.level); - interaction.reply({embeds: [new client.embed().setColor(member.displayColor).setTitle(`Level: **${userData.level}**\nRank: **${index ? '#' + index : 'last'}**\nProgress: **${memberDifference}/${levelDifference} (${(memberDifference/levelDifference*100).toFixed(2)}%)**\nTotal: **${userData.messages.toLocaleString('en-US')}**`).setThumbnail(member.avatarURL({extension:'png',size:1024}) || member.user.avatarURL({extension:'png',size:1024}) || member.user.defaultAvatarURL)]}) + interaction.reply({embeds: [new client.embed().setColor(member.displayColor).setTitle(`Level: **${userData.level}**\nRank: **${index ? '#' + index : 'last'}**\nProgress: **${memberDifference}/${levelDifference} (${(memberDifference/levelDifference*100).toFixed(2)}%)**\nTotal: **${userData.messages.toLocaleString('en-US')}**`).setThumbnail(member.avatarURL({extension:'png',size:1024}) || member.user.avatarURL({extension:'png',size:1024}) || member.user.defaultAvatarURL).setFooter({text: userData.notificationPing === true ? 'Ping notification enabled' : 'Ping notification disabled'})]}) }, leaderboard: ()=>{ const messageCountsTotal = allData.reduce((a, b) => a + b.messages, 0); @@ -156,7 +156,17 @@ export default { .setImage('attachment://dailymsgs.png').setColor(client.config.embedColor) .setFooter({text: 'Graph updates daily.'}) interaction.reply({embeds: [embed], files: [graphImage]}) - } + }, + notification: async()=>{ + const findUserInMongo = await client.userLevels._content.findById(interaction.user.id); + if (!findUserInMongo.notificationPing ?? findUserInMongo.notificationPing === false) { + await findUserInMongo.updateOne({_id: interaction.user.id, notificationPing: true}) + interaction.reply({content: 'You will be pinged for level-up notification in the future.', ephemeral: true}) + } else if (findUserInMongo.notificationPing === true) { + await findUserInMongo.updateOne({_id: interaction.user.id, notificationPing: false}) + interaction.reply({content: 'You won\'t be pinged for level-up notification in the future.', ephemeral: true}) + } + } } as any)[interaction.options.getSubcommand()](); }, data: new Discord.SlashCommandBuilder() @@ -171,4 +181,7 @@ export default { .addSubcommand(x=>x .setName('leaderboard') .setDescription('View top 10 users on leaderboard')) + .addSubcommand(x=>x + .setName('notification') + .setDescription('Allow the bot to ping you or not when you level up')) } \ No newline at end of file diff --git a/src/models/userLevels.ts b/src/models/userLevels.ts index 9aa32e9..d4e2ba2 100644 --- a/src/models/userLevels.ts +++ b/src/models/userLevels.ts @@ -5,7 +5,8 @@ import mongoose from 'mongoose'; const Schema = mongoose.model('userLevels', new mongoose.Schema({ _id: {type: String}, messages: {type: Number, required: true}, - level: {type: Number, required: true} + level: {type: Number, required: true}, + notificationPing: {type: Boolean} }, {versionKey: false})); export default class userLevels extends Schema { @@ -18,7 +19,6 @@ export default class userLevels extends Schema { } async incrementUser(userid:string){ const userData = await this._content.findById(userid) - if (userData){ await this._content.findByIdAndUpdate(userid, {messages: userData.messages + 1}); if (userData.messages >= this.algorithm(userData.level+2)){ @@ -28,9 +28,10 @@ export default class userLevels extends Schema { } } else if (userData.messages >= this.algorithm(userData.level+1)) { const newData = await this._content.findByIdAndUpdate(userid, {level:userData.level+1}, {new: true}); - (this.client.channels.resolve(this.client.config.mainServer.channels.botcommands) as Discord.TextChannel).send({content: `<@${userid}> has reached level **${newData.level}**. GG!`, allowedMentions: {parse: ['users']}}) + const fetchUserSchema = await this._content.findById(userid); + (this.client.channels.resolve(this.client.config.mainServer.channels.botcommands) as Discord.TextChannel).send({content: `${fetchUserSchema.notificationPing === true ? `<@${userid}>` : `**${(await this.client.users.fetch(userid)).username}**`} has reached level **${newData.level}**. GG!`, allowedMentions: {parse: ['users']}}); } - } else await this._content.create({_id: userid, messages: 1, level: 0}) + } else await this._content.create({_id: userid, notificationPing: true, messages: 1, level: 0}) } algorithm = (level:number)=>level*level*15; // Algorithm for determining levels. If adjusting, recommended to only change the integer at the end of equation.