-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
109 lines (82 loc) · 4.24 KB
/
index.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
import { ActivityOptions, Client, Guild, GuildChannel, GuildMember, Message, MessageReaction, PartialUser, User } from 'discord.js';
import * as files from 'fs';
import { Command } from './command';
import { CommandError } from './commandError';
import { GetRolesCommand } from './commands/getRolesCommand';
import { HelpCommand } from './commands/helpCommand';
import { RemoveRoleCommand } from './commands/removeRoleCommand';
import { SetRoleCommand } from './commands/setRoleCommand';
import { WikiCommand } from './commands/wikiCommand';
import { Config } from './config';
import { Data } from './data';
import * as emoji from 'node-emoji';
import { EmojiArgument } from './arguments/emojiArgument';
export const config: Config = JSON.parse(files.readFileSync('config.json').toString());
export const data: Data = new Data();
export const client = new Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
export const commands: Array<Command> = [
new SetRoleCommand(),
new RemoveRoleCommand(),
new GetRolesCommand(),
new WikiCommand(),
new HelpCommand()
];
(async () => {
await client.login(config.discord.token);
const activity: ActivityOptions = {
type: 'WATCHING',
name: 'wiki.t2linux.org'
};
client.user.setActivity(activity);
setInterval(() => client.user.setActivity(activity), 60 * 60 * 1000);
client.on('message', async message => {
if (message.author.bot || !message.member)
return;
const content: string = message.content;
if (content.startsWith(config.discord.commandPrefix)) {
const args: Array<string> = content.substring(1).split(' ').filter(argument => argument.trim() !== '');
for (const command of commands) {
if (command.name().toLowerCase() === args[0].toLowerCase()) {
if (!await command.permitted(message.member))
return message.channel.send(CommandError.generic(command.constructor.name, 'Insufficient permission').content);
return command.handle(message, args.slice(1)).catch(error => {
if (error instanceof CommandError)
return message.channel.send(error.content);
message.channel.send('An error occured: ' + error);
});
}
}
}
});
const processReactionEvent = async (reaction: MessageReaction, user: User | PartialUser, callback: (member: GuildMember, role: string) => void) => {
const message: Message = await reaction.message.fetch();
if (reaction.partial) reaction = await reaction.fetch();
if (user.id === client.user.id) return;
if (data.hasChannel(message.channel.id)) {
let identifier: string;
if (emoji.hasEmoji(reaction.emoji.name))
identifier = emoji.unemojify(reaction.emoji.name);
if (EmojiArgument.regionalIndicators.has(reaction.emoji.name))
identifier = EmojiArgument.regionalIndicators.get(reaction.emoji.name);
if (data.hasEmoji(message.channel.id, reaction.emoji.id))
identifier = reaction.emoji.id;
if (data.hasEmoji(message.channel.id, reaction.emoji.name))
identifier = reaction.emoji.name;
if (!identifier)
return;
const guild: Guild = (await client.channels.fetch(message.channel.id) as GuildChannel).guild;
const member: GuildMember = await guild.members.fetch(user.id);
const role: string = data.getRole(message.channel.id, identifier);
callback(member, role);
}
};
client.on('messageReactionAdd', async (reaction: MessageReaction, user: User | PartialUser) => processReactionEvent(reaction, user, (member, role) => {
console.log('Roles: ' + user.tag + ' added role ' + role);
member.roles.add(role);
}));
client.on('messageReactionRemove', async (reaction: MessageReaction, user: User | PartialUser) => processReactionEvent(reaction, user, (member, role) => {
console.log('Roles: ' + member.user.tag + ' removed role ' + role);
member.roles.remove(role);
}));
console.log('Discord: Logged in as ' + client.user.tag);
})();