-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.ts
188 lines (164 loc) · 5.3 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
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
import { Message, VoiceChannel } from "discord.js";
import { config } from "dotenv";
import {
handleIncomingChannelCommand,
handleIncomingDMCommand,
} from "./src/controllers/incomingMessageHandler";
import { getDiscordBot, initDiscordBot } from "./src/utils/discord";
import { COMMANDS } from "./src/utils/constants";
import { initDbClient, initEventDbClient } from "./src/utils/database";
import { initCache, refreshKeys } from "./src/utils/nodecache";
import {
handleAvatarUpdate,
handleChannelCreate,
handleChannelDelete,
handleChannelUpdate,
handleEmojiCreate,
handleEmojiDelete,
handleMemberBan,
handleMemberJoin,
handleMemberLeave,
handleMemberUnban,
handleMemberUpdate,
handleRoleCreate,
handleRoleDelete,
handleRoleUpdate,
handleVoiceStatus,
} from "./src/helper/memberLogs";
import { serverLogger } from "./src/utils/logger";
import { checkForAccessByRoles } from "./src/helper/roleAuth";
import { incomingMessageSchema } from "./src/models/incomingMessage";
import { Delete } from "./src/models/customTypes";
import { guildJoin } from "./src/controllers/sendMessageHandler";
import { addReaction, removeReaction } from "./src/helper/reactionRole";
/******************************************
Initialize Server
*******************************************/
async function createServer() {
// Mount Initializers
config();
await initDbClient();
await initEventDbClient();
await initCache();
await refreshKeys();
const client = await getDiscordBot();
client!.on("ready", () => {
if (client) console.log(`Logged in as ${client.user!.tag}!`);
});
client!.on("message", async (message: Message) => {
/******************************************
Check if input is by Human
*******************************************/
if (!message.author.bot) {
if (message.content.split(/\s+/)[0] == COMMANDS.prefix) {
const messageType: incomingMessageSchema = {
channelType: message.channel.type,
incomingUser: {
username: message.author.username,
discriminator: message.author.discriminator,
id: message.author.id,
isMod: await checkForAccessByRoles(message.member, [
`${process.env.OPERATOR_ROLE_ID}`,
]),
},
};
let isError: boolean = false;
switch (message.channel.type) {
/******************************************
Text channel
*******************************************/
case "text": {
//check for our command
isError = await handleIncomingChannelCommand(message, messageType);
break;
}
/******************************************
DM channel
*******************************************/
case "dm": {
isError = await handleIncomingDMCommand(message, messageType);
break;
}
default: {
serverLogger(
"user-error",
"ChannelNotSupported",
"Channel Not Supported"
);
isError = true;
}
}
message.react((isError) ? "❌" : process.env.CUSTOM_EMOJI_ID!).catch((err) => {
serverLogger("non-fatal-error", "Could not find custom emoji", err);
});
}
}
});
client!.on("guildMemberAdd", (member) => {
handleMemberJoin(member, client);
});
client!.on("guildMemberRemove", (member) => {
handleMemberLeave(member, client);
});
client!.on("channelCreate", (channel) => {
handleChannelCreate(channel, client);
});
client!.on("channelDelete", (channel) => {
handleChannelDelete((channel as unknown) as Delete, client);
});
client!.on("channelUpdate", (channel) => {
handleChannelUpdate(channel, client);
});
client!.on("emojiCreate", (emoji) => {
handleEmojiCreate(emoji, client);
});
client!.on("emojiDelete", (event) => {
handleEmojiDelete((event as unknown) as Delete, client);
});
client!.on("error", (error) => {
serverLogger("error", "InternalError", error);
});
client!.on("guildBanAdd", (guild, user) => {
handleMemberBan(guild, user);
});
client!.on("guildBanRemove", (guild, user) => {
handleMemberUnban(guild, user);
});
client!.on("guildCreate", (guild) => {
guildJoin(guild);
}); //gets the job done but throws an unknown error
client!.on("guildMemberUpdate", (oldUser, newUser) => {
handleMemberUpdate(oldUser, newUser);
});
client!.on("roleCreate", (role) => {
handleRoleCreate(role);
});
client!.on("roleDelete", (role) => {
handleRoleDelete(role);
});
client!.on("roleUpdate", (oldRole, newrRole) => {
handleRoleUpdate(oldRole, newrRole);
});
client!.on("voiceStateUpdate", (oldState, newState) => {
handleVoiceStatus(oldState, newState);
});
client!.on("messageReactionAdd", (reaction, user) => {
addReaction(reaction, user);
});
client!.on("messageReactionRemove", (reaction, user) => {
removeReaction(reaction, user);
});
client!.on("userUpdate", (oldUser, newUser) => {
handleAvatarUpdate(oldUser, newUser, client!);
});
}
createServer();
/**
* For better development experience
*/
process.on("SIGINT", () => {
process.exit(0);
});
process.on("SIGTERM", () => {
process.exit(0);
});