1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-09-29 12:30:58 -04:00

Add level-up notification setting (False by default)

This commit is contained in:
toast-ts 2023-06-30 19:24:34 +10:00
parent e7b1ff5eb6
commit 3707a45b87
2 changed files with 20 additions and 6 deletions

View File

@ -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'))
}

View File

@ -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.