1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-18 04:40:59 -05:00
Daggerbot-TS/src/commands/rank.ts

79 lines
5.1 KiB
TypeScript
Raw Normal View History

2023-05-23 01:14:17 -04:00
import Discord from 'discord.js';
2023-04-14 06:47:58 -04:00
import TClient from '../client.js';
2023-10-05 07:56:29 -04:00
import MessageTool from '../helpers/MessageTool.js';
2023-12-24 10:21:40 -05:00
import CanvasBuilder from '../components/CanvasGraph.js';
export default class Rank {
static async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){
if (interaction.guildId !== client.config.dcServer.id) return interaction.reply({content: 'This command doesn\'t work in this server.', ephemeral: true});
// const allData = await client.userLevels._content.find();
const allData = await client.userLevels.fetchEveryone();
2023-03-05 05:04:10 -05:00
({
view: async()=>{
// fetch user or user interaction sender
const member = interaction.options.getMember('member') ?? interaction.member as Discord.GuildMember;
2023-05-23 01:14:17 -04:00
if (member.user.bot) return interaction.reply('Bots don\'t level up, try viewing the rank data from the users instead.');
2023-03-05 05:04:10 -05:00
// information about users progress on level roles
2023-12-24 10:21:40 -05:00
// const userData = await client.userLevels._content.findById(member.user.id);
const userData = await client.userLevels.fetchUser(member.user.id);
2023-03-05 05:04:10 -05:00
const pronounBool = (you: string, they: string) => { // takes 2 words and chooses which to use based on if user did this command on themself
if (interaction.user.id === member.user.id) return you || true;
else return they || false;
};
if (!userData) return interaction.reply(`${pronounBool('You', 'They')} currently don't have a level, send some messages to level up.`)
2023-12-24 10:21:40 -05:00
const index = allData.sort((a, b) => b.messages - a.messages).map(x=>x.dataValues.id).indexOf(member.id) + 1;
const memberDifference = userData.dataValues.messages - client.userLevels.algorithm(userData.dataValues.level);
const levelDifference = client.userLevels.algorithm(userData.dataValues.level+1) - client.userLevels.algorithm(userData.dataValues.level);
let ptText = 'Ping toggle ';
interaction.reply({embeds: [new client.embed().setColor(member.displayColor).setTitle(`Level: **${userData.dataValues.level}**\nRank: **${index ? '#' + index : 'last'}**\nProgress: **${memberDifference}/${levelDifference} (${(memberDifference/levelDifference*100).toFixed(2)}%)**\nTotal: **${userData.dataValues.messages/* .toLocaleString('en-US') */}**`).setThumbnail(member.avatarURL({extension:'png',size:1024}) || member.user.avatarURL({extension:'png',size:1024}) || member.user.defaultAvatarURL).setFooter({text: userData.pingToggle === true ? ptText += 'enabled' : ptText += 'disabled'})]})
2023-03-05 05:04:10 -05:00
},
2023-12-24 10:21:40 -05:00
leaderboard: async()=>{
const data = (await client.dailyMsgs.fetchDays()).map(x=>[x.dataValues.day, x.dataValues.total]).sort((a,b)=>a[0]-b[0]).slice(-60).map((x: number[], i: number, a: any)=>{
return x[1] - ((a[i - 1] || [])[1] || x[1])
2023-12-24 10:21:40 -05:00
});
if (data.length < 3) return interaction.reply('Not enough data to generate graph.');
const graph = await new CanvasBuilder().generateGraph(data, 'leaderboard');
interaction.reply({
embeds: [new client.embed().setColor(client.config.embedColor).setTitle('Leaderboard')
.setDescription(MessageTool.concatMessage(
`Level System was created **${Math.floor((Date.now()-client.config.LRSstart)/1000/60/60/24)}** days ago.`,
`Since then, a total of **${allData.reduce((a, b)=>a+b.messages, 0).toLocaleString('en-US')}** messages have been sent in this server.`
)).addFields({
name: 'Top users sorted by messages sent:',
value: allData.sort((a,b)=>b.messages - a.messages).slice(0,15).map((x,i)=>`${i+1}. <@${x.dataValues.id}>: ${x.messages.toLocaleString('en-US')}`).join('\n')
}).setImage('attachment://dailyMessages.jpg').setFooter({text: 'Graph updates daily'})],
files: [new client.attachment(graph.toBuffer(),{name: 'dailyMessages.jpg'})]
})
},
notification: async()=>{
2023-12-24 10:21:40 -05:00
const findUserInDatabase = await client.userLevels.fetchUser(interaction.user.id);
2023-10-05 07:56:29 -04:00
const textDeco = ' be pinged for level-up notification in the future.'
2023-12-24 10:21:40 -05:00
if (!findUserInDatabase.pingToggle) {
await findUserInDatabase.update({pingToggle: true}, {where: {id: interaction.user.id}})
2023-10-05 07:56:29 -04:00
interaction.reply({content: 'You will'+textDeco, ephemeral: true})
2023-12-24 10:21:40 -05:00
} else if (findUserInDatabase.pingToggle) {
await findUserInDatabase.update({pingToggle: false}, {where: {id: interaction.user.id}})
2023-10-05 07:56:29 -04:00
interaction.reply({content: 'You won\'t'+textDeco, ephemeral: true})
}
}
2023-03-05 05:04:10 -05:00
} as any)[interaction.options.getSubcommand()]();
2023-12-24 10:21:40 -05:00
}
static data = new Discord.SlashCommandBuilder()
2023-03-05 05:04:10 -05:00
.setName('rank')
.setDescription('Level system')
2023-05-23 01:14:17 -04:00
.addSubcommand(x=>x
2023-03-05 05:04:10 -05:00
.setName('view')
.setDescription('View your rank or someone else\'s rank')
2023-05-23 01:14:17 -04:00
.addUserOption(x=>x
2023-03-05 05:04:10 -05:00
.setName('member')
.setDescription('Which member do you want to view?')))
2023-05-23 01:14:17 -04:00
.addSubcommand(x=>x
2023-03-05 05:04:10 -05:00
.setName('leaderboard')
2023-12-25 12:00:17 -05:00
.setDescription('View top 15 users on leaderboard'))
.addSubcommand(x=>x
.setName('notification')
.setDescription('Allow the bot to ping you or not when you level up'))
2023-10-05 07:56:29 -04:00
}