This repository has been archived by the owner on Aug 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.ts
273 lines (225 loc) · 9.4 KB
/
bot.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Taiga Bot. A bot that aims to provide interactive experiences to Taiga's fans.
// Copyright(C) 2020 Tetsuki Syu
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see < https://www.gnu.org/licenses/>.
//
// **This is a modified and rewritten version of Yuuto bot.**
// **Please refer to Yuuto bot's repository for more information.**
// **https://github.com/Yuuto-Project/yuuto-bot**
// **Codebase also influenced by Hiro bot by dunste123.**
// **https://github.com/dunste123/hirobot**
import * as Discord from 'discord.js';
import * as DotEnv from 'dotenv';
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'fs';
import Command from './commands/base/command';
import IMemberConfig from './interfaces/IMemberConfig';
import * as localizedStrings from './storage/localizedStrings.json';
import AdminUtility from './utility/adminUtility';
import ClientUtility from './utility/clientUtility';
import { getRandomInt } from './utility/helper';
// Configures the environment variables
DotEnv.config();
export const PREFIX = process.env.PREFIX || '!';
export const COMMANDS = new Discord.Collection<string, Command>();
export const ALIASES = new Discord.Collection<string, string>();
export const COOLDOWNS = new Discord.Collection<string, Discord.Collection<string, number>>();
export const MEMBER_CONFIG = new Array<IMemberConfig>();
// Collections and Sets
const taiga = new Discord.Client({
ws: {
// Read more https://discord.js.org/#/docs/main/stable/class/Intents?scrollTo=s-FLAGS
intents: [
"GUILDS",
"GUILD_MEMBERS",
"GUILD_MESSAGES",
"GUILD_MESSAGE_REACTIONS"
]
},
// We need to fetch all members to make sure that finderUtil works properly
fetchAllMembers: true
});
const enStrings = localizedStrings.find(val => val.lang === 'en')!;
const jpStrings = localizedStrings.find(val => val.lang === 'jp')!;
const configPath = './storage/memberConfigs.json';
(async () => {
// Load command files
const commandsFiles = readdirSync('./commands/').filter(file => file.endsWith('.js'));
// Load commands
for (const file of commandsFiles) {
const { default: ctor } = await import(`./commands/${file}`);
if (!ctor) continue;
// Checks if we actually have a command here, we don't want to register classes that are not commands
if (!((<Function>ctor).prototype instanceof Command)) {
console.error(`ERROR: Command ${(<Function>ctor).name} does not extend command base class and is ignored.`);
continue;
}
const command: Command = new ctor();
COMMANDS.set(command.Name, command);
if (command.Alias) {
command.Alias.forEach(_alias => ALIASES.set(_alias, command.Name));
}
}
})().catch(console.error);
(async () => {
console.log(`Reading config files...`);
if (existsSync(configPath)) {
const file = readFileSync(configPath, 'utf8');
const arr = JSON.parse(file) as Array<IMemberConfig>;
console.log(arr);
arr.forEach(obj => {
MEMBER_CONFIG.push(obj);
});
console.log(`Config files successfully read.`);
console.log(`MEMBER_CONFIG: ${MEMBER_CONFIG.length} data available.`);
}
})().catch(console.error);
// Initialize the bot's startup
taiga.once('ready', () => {
console.log('Connected');
console.log(`Logged in as: ${taiga.user?.username} - (${taiga.user?.id})`);
console.log(`Hello, ${taiga.user?.username} is now online.`);
const presenceFn = () => {
console.log('Setting presence');
const activity = enStrings.texts.presence[getRandomInt(0, enStrings.texts.presence.length)];
taiga.user?.setPresence({
status: 'online',
activity: {
name: activity,
type: 'PLAYING'
}
});
}
const writeConfigFn = () => {
console.log('Writing member configs...');
const content = JSON.stringify(MEMBER_CONFIG, null, 4);
writeFileSync(configPath, content);
}
// This math stuff is one hour in milliseconds
// This interval makes sure that Taiga keeps playing his games
setInterval(presenceFn, 1000 * 60 * 60);
setInterval(writeConfigFn, 1000 * 60 * 60);
presenceFn();
});
taiga.on('guildMemberAdd', member => {
const generalChannelIds = [process.env.GENCHN, process.env.TESTGENCHN];
for (const id of generalChannelIds) {
if (!id) continue;
const channel = member.guild.channels.resolve(id);
if (!channel) continue;
const greetings = enStrings.texts.greetings;
const msg = greetings[getRandomInt(0, greetings.length)]
.replace('{name}', `<@${member.id || member.user!.id}>`);
(<Discord.TextChannel>channel).send(msg);
}
});
taiga.on('message', async message => {
if (message.author.bot || !message.guild) return;
let config = MEMBER_CONFIG.find(config => config.userId === message.author.id);
if (config) {
console.log(config);
console.log(`Found config.`);
}
else {
MEMBER_CONFIG.push({ user: message.author, userId: message.author.id, lang: 'en' });
config = MEMBER_CONFIG.find(config => config.userId === message.author.id);
console.log(`Member config pushed to the array.`);
}
const startsWithPrefix = message.content.startsWith(PREFIX);
const ignoreChannel: string[] = [process.env.VENTCHN!];
const responseStrings = (config!.lang === 'en') ? enStrings : jpStrings;
// Don't do anything in venting channel
if (ignoreChannel.includes(message.channel.id)) return;
// React to mentions
ClientUtility.mentionHandler(message);
// React to messages
ClientUtility.randomReactionHandler(message);
ClientUtility.randomUserReactionHandler(message);
// Randomly edit a message
ClientUtility.randomEditHandler(message);
// Randomly reply a message
const chance = parseInt(process.env.RDMCHANCE!);
const hitMiss = getRandomInt(0, 100) < chance;
if (hitMiss && !startsWithPrefix) {
ClientUtility.randomMsgHandler(message);
}
// Admin commands
if (message.content.startsWith(process.env.ADMIN_PREFIX!) &&
message.author.id === process.env.ADMIN_ID!) {
AdminUtility.execute(taiga, message,
message.content
.slice(process.env.ADMIN_PREFIX!.length)
.trim()
.split(/ +/g));
}
if (!startsWithPrefix) return;
if (((process.env.BOTCHN && process.env.BOTMODCHN) &&
(message.channel.id != process.env.BOTCHN &&
message.channel.id != process.env.BOTMODCHN)) &&
(process.env.TESTCHN && message.channel.id != process.env.TESTCHN)) {
return;
}
// Get the command name. or return
const args = message.content
.slice(PREFIX.length)
.trim()
.split(/ +/g);
const cmd = args.shift()?.toLowerCase()!;
if (!cmd) return;
// Find command or alias
let command = COMMANDS.get(cmd);
if (!command && ALIASES.has(cmd)) {
command = COMMANDS.get(ALIASES.get(cmd)!);
}
// If we still didn't find a command, fail the command and return.
if (!command) {
const failedMessage = responseStrings.texts.failed_messages;
const msg = failedMessage[getRandomInt(0, failedMessage.length)]
.replace('{command}', cmd);
message.channel.send(msg);
return;
}
// Add the command to the cooldown
if (!COOLDOWNS.has(command!.Name)) {
COOLDOWNS.set(command!.Name, new Discord.Collection<string, number>());
}
// Set the cooldown's timestamp
const now = Date.now();
const timestamps = COOLDOWNS.get(command!.Name)!;
const cooldownAmount = (command!.Cooldown || 3) * 1000;
// Check if the user is on cooldown
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id)! + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
let cooldownMsg = responseStrings.texts.cooldown;
cooldownMsg = cooldownMsg.replace('{timeLeft}', timeLeft.toFixed(1));
cooldownMsg = cooldownMsg.replace('{cmd}', command!.Name);
message
.reply(cooldownMsg);
return;
}
}
// Add the author to the cooldown timestamps,
// then remove the command after cooldown expires.
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
// Execute the command, and catch errors.
try {
await command!.run(taiga, message, args);
} catch (error) {
console.error(error);
message.reply(responseStrings.texts.execution_failure);
}
});
taiga.login(process.env.TOKEN).catch(console.error);