-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
132 lines (97 loc) · 3.39 KB
/
index.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const isDevelopment = process.env.NODE_ENV === "development"
const isProduction = process.env.NODE_ENV === "production"
const logger = require('./utils/logger')
logger.info(`Bot starting... current time: ${Date()}`)
logger.info(`NODE_ENV === ${process.env.NODE_ENV}`)
const fs = require('fs');
const Discord = require('discord.js');
const { Intents } = require('discord.js')
//const discordToken = secretsJSON.discordToken
const cron = require('cron');
const terminate = require('./utils/terminate')
const prefix = "!"
// const updateUsernameCache = require('./update-username-cache')
const client = new Discord.Client({ intents: [Intents.FLAGS.GUILDS] });
let secretsJSON
if (isDevelopment) {
logger.info("Credentials source: dotenv")
const dotenv = require('dotenv').config()
client.login(process.env.discordToken)
}else if (isProduction) {
logger.info("Credentials source: AWS Secrets Manager")
const getAWSSecrets = require('./utils/getAWSSecrets')
getAWSSecrets("production")
.then(res => JSON.parse(res))
.then(data => {
secretsJSON = data
})
.then(() => {
for (secretName in secretsJSON) {
process.env[secretName] = secretsJSON[secretName]
}
logger.info('Logging into Discord...')
client.login(process.env.discordToken)
})
.catch(err => {
logger.error(err)
})
}
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
const cooldowns = new Discord.Collection();
client.once('ready', () => {
logger.info('I am ready!');
// updateUsernameCache(client);
});
client.on("guildMemberUpdate", function(oldMember, newMember){
// updateUsernameCache(client, newMember)
// .catch((errors) => {
// logger.error(errors)
// })
});
//TODO trigger updateUsernameCache when a guild member joins the server
client.on("guildMemberAdd", function(newMember){
// updateUsernameCache(client, newMember)
});
// const list = client.guilds.cache.get("803429347750576138");
// logger.debug(list.members.cache.array());
// Go through each of the members, and console.log() their name
// list.members.fetch().then(members => logger.info(members))
let counter = 0
// let scheduledMessage = new cron.CronJob('* 00 * * * *', () => {
// updateUsernameCache(client)
// logger.info('cache updated ' + Date());
// });
// When you want to start it, use:
// scheduledMessage.start()
client.on(
'message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
logger.info({
user: message.author.username,
content: message.content
})
const args = message.content.slice(prefix.length).trim().split(' ');
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
if (command.args && !args.length) {
return message.channel.send(`You didn't provide any arguments, ${message.author}!`);
}
try {
command.execute(message, args);
} catch (error) {
message.reply('Error:' + error);
logger.error(error);
}
});
const exitHandler = terminate(client)
process.on('uncaughtException', exitHandler);
process.on('unhandledRejection', exitHandler);
process.on('SIGINT', exitHandler)
process.on('SIGTERM', exitHandler)