-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
68 lines (60 loc) · 1.81 KB
/
index.ts
File metadata and controls
68 lines (60 loc) · 1.81 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
import {Client, Collection, Intents} from 'discord.js'
import dotenv from 'dotenv'
import {MongoClient} from 'mongodb'
import fs from 'fs'
import * as commandModules from "./commands"
dotenv.config()
export const client = new Client({ // Export so it can be used elsewhere
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_SCHEDULED_EVENTS
]
})
// Registering commands with client
const commands = Object(commandModules)
// Client logs into Discord and waits for Slash command interactions
client.once('ready', async () => {
console.log(`Allen's ready!`)
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) {return;}
const {commandName} = interaction;
commands[commandName].execute(interaction, client) // passes client as well to commands
})
// Setup mongodb connection
if (!process.env.MONGO_URI){
console.error('Mongo URI not found in Environment variables.');
process.exit(1);
}
const mongoClient = new MongoClient(process.env.MONGO_URI!)
async function run() {
try {
await mongoClient.connect();
mongoClient.db().admin().listDatabases()
.then(dbs => {
console.log("Mongo Databases:", dbs)
});
} catch (err) {
console.error(err);
}
}
run().catch(console.dir)
// Bot Login
if (process.env.TOKEN)
client.login(process.env.TOKEN);
else {
console.error('Token does not exist in environment!')
process.exit(1);
}
// Graceful closing of Allen
process.on('SIGINT', function () {
console.log('Disconnecting from MongoDB...')
mongoClient.close();
process.stdout.write("Disconnecting from discord...\n");
client.destroy();
process.stdout.write("Allen is now going to sleep o/");
process.exit();
});