Skip to content

Commit dd5ce40

Browse files
committed
Overhauled backend, changed over to using MySQL2, Added dynamic command suggestions, made 'fetchers' into separate module
1 parent cfc1a0c commit dd5ce40

8 files changed

Lines changed: 3968 additions & 950 deletions

File tree

commands/utility/characters.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
const { SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, StringSelectMenuBuilder, StringSelectMenuOptionBuilder } = require('discord.js');
2+
//function GetCounts(con) {
3+
// return new Promise((resolve, reject) => {
4+
5+
// });
6+
//}
7+
const back = new ButtonBuilder().setCustomId('0')
8+
.setLabel('⮌')
9+
.setStyle(ButtonStyle.Secondary);
10+
module.exports = {
11+
data: new SlashCommandBuilder()
12+
.setName('characters')
13+
.setDescription('Shows stats'),
14+
async execute(interaction, client, con, stats) {
15+
/*
16+
const right = new ButtonBuilder()
17+
.setCustomId('right')
18+
.setLabel('🡺')
19+
.setStyle(ButtonStyle.Primary);
20+
const left = new ButtonBuilder()
21+
.setCustomId('left')
22+
.setLabel('🡸')
23+
.setStyle(ButtonStyle.Primary);
24+
const row = new ActionRowBuilder()
25+
.addComponents(left, right);
26+
console.log(Object.keys(list));
27+
*/
28+
let total_chars = 0, options = [];
29+
con.query('SELECT c.*, COUNT(ch.id) FROM categories c INNER JOIN characters ch ON c.id = ch.cat GROUP BY c.id;', async function(err,results) {
30+
if (err) {
31+
throw err;
32+
return;
33+
}
34+
results.forEach((c) => {
35+
const count = c['COUNT(ch.id)'];
36+
total_chars+=count;
37+
options.push(new StringSelectMenuOptionBuilder()
38+
.setLabel(c.name)
39+
.setDescription(`${count} Character`+((count!=1)?'s':''))
40+
.setValue(`${c.id}`));
41+
});
42+
const row = new ActionRowBuilder().addComponents(new StringSelectMenuBuilder()
43+
.setCustomId('characters')
44+
.setPlaceholder('Make a selection!')
45+
.addOptions(options));
46+
47+
await interaction.reply({content: `## Lists\nChoose the list in the section below\n-# *Total Characters: ${total_chars}*`, components: [row]});
48+
});
49+
/* const rxx = await interaction.reply({
50+
content: `## Lists\nChoose the list in the section below\n-# *Total Characters: ${total_chars}*\n-# *Last scan: ${timeDiff(stats.last_reset, Date.now())}*`,
51+
components: [row],
52+
});*/
53+
},
54+
async otherInteraction(interaction, client, con, stats) {
55+
switch(interaction.customId) {
56+
case 'characters':
57+
con.execute('SELECT s.id,s.name,COUNT(ch.id) AS count FROM categories s INNER JOIN characters ch ON ch.cat = s.id WHERE s.id=?;', [interaction.values[0]], async (err, results) => {
58+
if (err) {
59+
throw err;
60+
return;
61+
}
62+
const result = results[0];
63+
let text = `## ${result.name}\n`;
64+
const list = await new Promise((resolve,reject) => {
65+
con.execute('SELECT * FROM characters WHERE cat=? LIMIT ? OFFSET ?', [result.id,'10','0'],(err,results)=> {
66+
if (err) reject(err);
67+
resolve(results);
68+
});
69+
});
70+
list.forEach((c) => {
71+
text += `- ${c.name}\n-# ${c.fullname}\n`;
72+
})
73+
const page = Math.ceil(result.count/10);
74+
text += `\n*Page 1 of ${page}*`;
75+
const right = new ButtonBuilder()
76+
.setCustomId(`A${result.name}{${result.id}a${2}b${result.count}`) //name { cat id a page b total
77+
.setLabel('>')
78+
//.setEmoji({name: '➡️'})
79+
.setStyle(ButtonStyle.Primary);
80+
if (page == 1) right.setDisabled(true);
81+
const left = new ButtonBuilder()
82+
.setCustomId('left')
83+
.setLabel('<')
84+
//.setEmoji({name: '⬅️'})
85+
.setStyle(ButtonStyle.Primary)
86+
.setDisabled(true);
87+
88+
const row = new ActionRowBuilder().addComponents(back, left, right);
89+
90+
await interaction.update({content:text, components:[row]});
91+
});
92+
break;
93+
default:
94+
break;
95+
}
96+
//await interaction.reply({content: 'whatever'});
97+
},
98+
async buttonInteraction(interaction, client, con, stats) {
99+
const str = interaction.customId;
100+
switch (str.substring(0,1)) {
101+
case '0':
102+
let total_chars = 0, options = [];
103+
con.query('SELECT c.*, COUNT(ch.id) FROM categories c INNER JOIN characters ch ON c.id = ch.cat GROUP BY c.id;', async function(err,results) {
104+
if (err) {
105+
throw err;
106+
return;
107+
}
108+
results.forEach((c) => {
109+
const count = c['COUNT(ch.id)'];
110+
total_chars+=count;
111+
options.push(new StringSelectMenuOptionBuilder()
112+
.setLabel(c.name)
113+
.setDescription(`${count} Character`+((count!=1)?'s':''))
114+
.setValue(`${c.id}`));
115+
});
116+
const row = new ActionRowBuilder().addComponents(new StringSelectMenuBuilder()
117+
.setCustomId('characters')
118+
.setPlaceholder('Make a selection!')
119+
.addOptions(options));
120+
121+
await interaction.update({content: `## Lists\nChoose the list in the section below\n-# *Total Characters: ${total_chars}*`, components: [row]});
122+
});
123+
break;
124+
case 'A':
125+
const ida = str.substring(str.lastIndexOf('{')+1);
126+
console.log(ida);
127+
const info = {
128+
name: str.substring(1,str.lastIndexOf('{')),
129+
cat: parseInt(ida.substring(0,ida.indexOf('a'))),
130+
page: parseInt(ida.substring(ida.indexOf('a')+1,ida.indexOf('b'))),
131+
max: parseInt(ida.substring(ida.indexOf('b')+1))
132+
};
133+
let text = `## ${info.name}\n`;
134+
const list = await new Promise((resolve,reject) => {
135+
con.execute('SELECT * FROM characters WHERE cat=? LIMIT ? OFFSET ?', [info.cat,'10',(info.page-1)*10],(err,results)=> {
136+
if (err) reject(err);
137+
resolve(results);
138+
});
139+
});
140+
list.forEach((c) => {
141+
text += `- ${c.name}\n-# ${c.fullname}\n`;
142+
});
143+
const right = new ButtonBuilder()
144+
.setCustomId('Zph1')
145+
.setLabel('>')
146+
//.setEmoji({name: '➡️'})
147+
.setStyle(ButtonStyle.Primary);
148+
if (Math.ceil(info.max/10) <= info.page)
149+
right.setDisabled(true);
150+
else right.setCustomId(`A${info.name}{${info.cat}a${info.page+1}b${info.max}`).setDisabled(false);
151+
const left = new ButtonBuilder()
152+
.setCustomId('Zph2')
153+
.setLabel('<')
154+
//.setEmoji({name: '⬅️'})
155+
.setStyle(ButtonStyle.Primary)
156+
if (info.page-1 < 1)
157+
left.setDisabled(true);
158+
else left.setCustomId(`A${info.name}{${info.cat}a${info.page-1}b${info.max}`).setDisabled(false);
159+
text += `\n*Page ${info.page} of ${Math.ceil(info.max/10)}*`;
160+
const row = new ActionRowBuilder().addComponents(back, left, right);
161+
await interaction.update({content:text, components:[row]});
162+
break;
163+
}
164+
}
165+
};

0 commit comments

Comments
 (0)