-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
96 lines (77 loc) · 2.8 KB
/
Copy pathapp.js
File metadata and controls
96 lines (77 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Client, Collection, GatewayIntentBits } from 'discord.js';
import dotenv from 'dotenv';
import klaw from 'klaw';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { EmbedBase } from './classes/EmbedBase.js';
dotenv.config();
const client = new Client({
allowedMentions: { parse: ['everyone'] },
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
});
client.commands = new Collection();
await mkdir('cache', { recursive: true });
const levels = JSON.parse(await readFile('cache/levels.json', 'utf-8').catch(() => '{}'));
console.log(levels);
// Import commands
for await (const item of klaw('./commands')) {
const cmdFile = path.parse(item.path);
if (!cmdFile.ext || cmdFile.ext !== '.js') continue;
const cmdName = cmdFile.name.split('.')[0];
try {
const cmd = new (
await import('./' + path.relative(process.cwd(), `${cmdFile.dir}${path.sep}${cmdFile.name}${cmdFile.ext}`))
).default();
client.commands.set(cmdName, cmd);
} catch (error) {
console.error(error);
}
}
console.log(`Loaded ${client.commands.size} commands`);
client.on('messageCreate', (message) => {
if (message.author.id === client.user.id) return; //dont respond to myself
console.log(message.content);
if (message.content === '!ping') return message.reply('Pong!'); //respond to !ping
if (message.content === '!level')
return message.reply({
embeds: [
new EmbedBase({
title: 'Your Level',
description: `Level ${levels[message.author.id] ?? 0}`,
}),
],
});
const args = message.content.split(' ').slice(1); //split message into array of args
if (message.content.startsWith('!bible')) return bible(message, args);
// message.reply('@everyone');
});
// Levelling logic
client.on('messageCreate', async (message) => {
if (message.author.id === client.user.id) return; //dont respond to myself
console.log('levels before', levels);
let level = levels[message.author.id] ?? 0;
levels[message.author.id] = ++level; // reassign user's level in the levels object
console.log('levels after', levels);
await writeFile('cache/levels.json', JSON.stringify(levels), 'utf-8');
});
// Slash commands
client.on('interactionCreate', async (intr) => {
if (!intr.isCommand()) return;
// Ignore commands sent by other bots
if (intr.user.bot) return;
const command = client.commands.get(intr.commandName);
await command.run({ intr, opts: intr.options });
});
client.on('ready', async () => {
await postInit();
console.log(`Logged in as ${client.user.tag}!`);
});
async function postInit() {
try {
await client.guilds.resolve('284847469492437002').commands.set(client.commands);
// await client.application.commands.set([]);
} catch (err) {
console.error('postInit error', err);
}
}
client.login(process.env.BOT_TOKEN);