Skip to content

Commit e5e8db9

Browse files
committed
feat(0.0.6): added new command and added buttons
1 parent f813e3d commit e5e8db9

File tree

6 files changed

+173
-19
lines changed

6 files changed

+173
-19
lines changed

api/index.ts

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,37 @@ app.get('/get/:guild', async (req, res) => {
277277

278278
app.post('/admin/:action/:guild/:target', (req, res) => {
279279
const { guild, action, target } = req.params;
280-
if (action === 'include') {
281-
// run function to include target to guild
282-
return;
283-
}
284-
else if (action === 'exclude') {
285-
// run function to exclude target from guild
286-
return;
287-
}
288-
else if (action === 'updates') {
289-
return;
280+
const { auth } = req.body;
281+
282+
if (auth !== process.env.AUTH) {
283+
return res.status(403).json({ message: 'Access denied. Auth token is missing' });
290284
}
291-
else {
292-
return res.status(400).json({ message: 'Illegal request' });
285+
286+
switch (action) {
287+
case 'include':
288+
// target: channel id
289+
// run function to include target to guild
290+
break;
291+
case 'exclude':
292+
// target: channel id
293+
// run function to exclude target from guild
294+
break;
295+
case 'updates':
296+
if (target !== 'enable') {
297+
return res.status(400).json({ message: 'Illegal request' });
298+
}
299+
// targets: get (it will toggle it on the database)
300+
// run function to disable/enable updates for guild
301+
break;
302+
case 'roles':
303+
if (target !== 'add' && target !== 'remove') {
304+
return res.status(400).json({ message: 'Illegal request' });
305+
}
306+
// targets: add, remove
307+
// run function to add/remove level roles for guild
308+
break;
309+
default:
310+
return res.status(400).json({ message: 'Illegal request' });
293311
}
294312
});
295313

@@ -303,6 +321,56 @@ app.get('/leaderboard/:guild', async (req, res) => {
303321
res.render('leaderboard', { guild: data.guild, leaderboard: data.leaderboard });
304322
});
305323

324+
async function getBotInfo() {
325+
const selectGuildsCountQuery = `
326+
SELECT COUNT(*) AS total_guilds FROM info_guilds;
327+
`;
328+
329+
const selectTotalMembersQuery = `
330+
SELECT SUM(guild_members) AS total_members FROM info_guilds;
331+
`;
332+
333+
try {
334+
const guildsCountResult = await new Promise((resolve, reject) => {
335+
pool.query(selectGuildsCountQuery, (err, results) => {
336+
if (err) {
337+
console.error('Error fetching guilds count:', err);
338+
reject(err);
339+
} else {
340+
resolve(results[0].total_guilds);
341+
}
342+
});
343+
});
344+
345+
const totalMembersResult = await new Promise((resolve, reject) => {
346+
pool.query(selectTotalMembersQuery, (err, results) => {
347+
if (err) {
348+
console.error('Error fetching total members:', err);
349+
reject(err);
350+
} else {
351+
resolve(results[0].total_members);
352+
}
353+
});
354+
});
355+
356+
const botInfo = {
357+
total_guilds: guildsCountResult,
358+
total_members: totalMembersResult,
359+
};
360+
361+
return botInfo
362+
} catch (error) {
363+
console.error('Error fetching bot info:', error);
364+
return null
365+
}
366+
}
367+
368+
app.get('/', async (req, res) => {
369+
const botInfo = await getBotInfo();
370+
console.log(botInfo)
371+
res.render('index', { botInfo: botInfo });
372+
});
373+
306374
app.listen(PORT, () => {
307375
console.log(`Server running on http://localhost:${PORT}`);
308376
});

api/views/index.ejs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Chatr - A Discord XP Bot
8+
</title>
9+
<meta name="description" content="Chatr - A Discord XP Bot">
10+
<link rel="stylesheet" href="/styles.css">
11+
</head>
12+
13+
<body>
14+
<main>
15+
<div class="box">
16+
<div class="guildInfo">
17+
<h1>Chatr - A Discord XP Bot</h1>
18+
</div>
19+
</div>
20+
<div class="box">
21+
<div class="guildInfo">
22+
<h1>
23+
<%=botInfo.total_guilds%> Guilds | <%=botInfo.total_members%> Members
24+
</h1>
25+
</div>
26+
</div>
27+
</main>
28+
</body>
29+
30+
</html>

bot/commands.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Commands taken from https://github.com/NiaAxern/discord-youtube-subscriber-count/blob/main/src/commands/utilities.ts
22

33
import client from '.';
4-
import { type CommandInteraction } from 'discord.js';
4+
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, type CommandInteraction, ChannelType } from 'discord.js';
55
import { heapStats } from 'bun:jsc';
66
import { getGuildLeaderboard, makeGETRequest } from './utils/requestAPI';
77
import convertToLevels from './utils/convertToLevels';
@@ -206,8 +206,16 @@ const commands: Record<string, Command> = {
206206
]);
207207
});
208208

209+
const button = new ButtonBuilder()
210+
.setLabel('Leaderboard')
211+
.setURL(`https://chatr.imgalvin.me/guild/${interaction.guildId}`)
212+
.setStyle(ButtonStyle.Link);
213+
214+
const row = new ActionRowBuilder<ButtonBuilder>()
215+
.addComponents(button);
216+
209217
// Send the embed
210-
await interaction.reply({ embeds: [leaderboardEmbed] });
218+
await interaction.reply({ embeds: [leaderboardEmbed], components: [row] });
211219
} catch (error) {
212220
console.error('Error executing command:', error);
213221
await interaction.reply('There was an error retrieving the leaderboard.');
@@ -216,7 +224,41 @@ const commands: Record<string, Command> = {
216224
await interaction.reply('This command can only be used in a guild.');
217225
}
218226
}
219-
}
227+
},
228+
cansee: {
229+
data: {
230+
options: [],
231+
name: 'cansee',
232+
description: 'Check what channels the bot can see',
233+
integration_types: [0],
234+
contexts: [0, 2],
235+
},
236+
execute: async (interaction) => {
237+
if (!interaction.memberPermissions?.has('ManageChannels')) {
238+
const errorEmbed = quickEmbed({
239+
color: 'Red',
240+
title: 'Error!',
241+
description: 'Missing permissions: `Manage Channels`'
242+
}, interaction);
243+
await interaction.reply({
244+
ephemeral: true,
245+
embeds: [errorEmbed]
246+
})
247+
.catch(console.error);
248+
return;
249+
}
250+
251+
const channels = await interaction.guild?.channels.fetch();
252+
const accessibleChannels = channels?.filter(channel => channel && channel.permissionsFor(interaction.client.user)?.has('ViewChannel') && channel.type !== ChannelType.GuildCategory);
253+
254+
await interaction
255+
.reply({
256+
ephemeral: true,
257+
content: accessibleChannels?.map(channel => `<#${channel?.id}>`).join('\n')
258+
})
259+
.catch(console.error);
260+
},
261+
},
220262
};
221263

222264
// Convert commands to a Map

bot/utils/handleLevelChange.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
// Provide updates in the guild, if enabled
2+
export default async function(guild, user, level) {
3+
await checkIfGuildHasUpdatesEnabled(guild);
4+
}
25

36
export async function checkIfGuildHasUpdatesEnabled(guild: string) {
4-
const response = await fetch(`http://localhost:18103/admin/${guild}/updates/check`);
7+
const response = await fetch(`http://localhost:18103/admin/${guild}/updates/check`, {
8+
headers: {
9+
'Content-Type': 'application/json',
10+
},
11+
body: JSON.stringify({ auth: process.env.AUTH }),
12+
method: 'GET',
13+
}).then(res => {
14+
return res.json()
15+
}).then(data => {
16+
return data
17+
18+
});
519
// just to shut up eslint
620
console.log(response)
721
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "xpbot",
33
"type": "module",
4-
"version": "0.0.5",
4+
"version": "0.0.6",
55
"scripts": {
66
"api": "bun --watch api/index.ts",
77
"bot": "bun --watch bot/index.ts"

0 commit comments

Comments
 (0)