-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
248 lines (234 loc) · 6.8 KB
/
main.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
import dotenv from "dotenv";
import * as commands from "./commands/index";
import * as Discord from "discord.js";
import c from "./config.json";
import * as fs from "fs";
dotenv.config();
const client = new Discord.Client({ intents: [Discord.GatewayIntentBits.Guilds] });
const startTime = Date.now();
let config = c;
let errorChannel: any;
// Command Run Loop
client.on("interactionCreate", async (interaction: Discord.BaseInteraction) => {
if (!interaction.isChatInputCommand()) {
return;
}
// Add commands here
// Yes this is fuck ugly but it's better than checking if a file exists and then loading it every time
switch (interaction.commandName) {
case "ping":
commands.ping(client, interaction);
break;
case "apply":
commands.apply(interaction);
break;
case "github":
commands.github(interaction);
break;
case "god-armor":
commands.godArmor(interaction);
break;
case "hibye":
commands.hiBye(interaction);
break;
case "map":
commands.map(interaction);
break;
case "spacebar":
commands.spacebar(interaction);
break;
case "specs":
commands.specs(interaction);
break;
case "tryit":
commands.tryit(interaction);
break;
case "world-age":
commands.worldAge(interaction);
break;
case "bot-info":
commands.botInfo(interaction);
break;
case "seed":
commands.seed(interaction);
break;
case "welcome":
config = commands.welcomeMessage(interaction, config);
fs.writeFileSync("config.json", JSON.stringify(config));
break;
case "leave":
config = commands.leaveMessage(interaction, config);
fs.writeFileSync("config.json", JSON.stringify(config));
break;
case "embed":
commands.embed(interaction);
break;
case "uptime":
commands.uptime(interaction, startTime, errorChannel, commands.error, process.env.KEY);
break;
case "players-online":
commands.playersOnline(interaction, errorChannel, commands.error, process.env.KEY);
break;
case "kick":
commands.kick(interaction, client, errorChannel, commands.error);
break;
case "ban":
commands.ban(interaction, client, errorChannel, commands.error);
break;
case "link":
commands.link(interaction, errorChannel, commands.error, process.env.KEY);
case "whitelist":
commands.whitelist(interaction, errorChannel, commands.error, process.env.KEY);
break;
case "error-messages":
config = commands.errorMessage(interaction, config);
fs.writeFileSync("config.json", JSON.stringify(config));
break;
default:
interaction.reply("How the fuck did you get this message");
}
});
// Run when someone joins the server
client.on("guildMemberAdd", async (member: any) => {
// Get channel to send welcome message in
let channel: any = member.guild.channels.cache.get(`${config.joinMsgChannel}`);
if (!channel || !config.joinMsgEnabled) {
// If invalid, log error unless it's -1 as that's the janky-ass way to disable this shit
console.log(config.joinMsgEnabled ? "invalid channel id" : "join msg disabled");
return;
}
let msgToSub: string = "";
if (config.isJoinEmbed) {
msgToSub = JSON.parse(config.joinMsg).description;
} else {
msgToSub = config.joinMsg;
}
// Both can only be equal if at same position, which is fucking impossible, or if both are not found (i.e. both return -1)
if (msgToSub.indexOf("{") == msgToSub.indexOf("}")) {
// If you didn't customise the join message like a fucking monster
channel.send(msgToSub);
return;
}
// Value substitution
let subStart: number = -1;
for (let i = 0; i < msgToSub.length; i++) {
if (msgToSub == "{") {
// Start of field to substitute
subStart = i;
continue;
} else if (msgToSub != "}") {
continue;
} // If not needed
else if (subStart == -1) {
continue;
} // If no substitution needed
// End of bit to substitute
let subValue: string = "";
switch (
msgToSub.slice(subStart + 1, i) // Add substitution values here
) {
case "user":
subValue = `<@${member.user.id}>`;
}
if (subValue != "") {
msgToSub.replace(msgToSub.slice(subStart, i + 1), subValue);
}
subStart = -1;
}
// Send message
if (!config.isJoinEmbed) {
channel.send(msgToSub);
return;
}
// If embed
let embed: any = config.joinMsg;
embed.description = msgToSub;
channel.send({ embeds: [embed] });
});
// Run when someone leaves the server
client.on("guildMemberRemove", async (member: any) => {
// Check if the user got banned to set the bit for the API
const banLogs: any = await member.guild
.fetchAuditLogs({
limit: 1,
type: "MEMBER_BAN_ADD"
})
.entries.first();
fetch("https://api.prosperitymc.net/leave", {
method: "POST",
body: JSON.stringify({
id: member.id,
banned: banLogs.target.id == member.user.id ? 1 : 0
}),
headers: {
"Content-Type": "application/json",
"authorization": process.env.KEY ? process.env.KEY : ""
}
}).then(async (response: Response) => {
if (response.status != 200) {
commands.error("Error calling /leave", `${response}`, errorChannel);
}
});
// Get channel to send leave message in
let channel: any = member.guild.channels.cache.get(`${config.leaveMsgChannel}`);
if (!channel || !config.leaveMsgEnabled) {
// If invalid, log error unless it's -1 as that's the janky-ass way to disable this shit
console.log(config.leaveMsgEnabled ? "invalid channel id" : "leave msg disabled");
return;
}
// Get the bit which actually needs substituting
let msgToSub: string = "";
if (config.isLeaveEmbed) {
msgToSub = JSON.parse(config.leaveMsg).description;
} else {
msgToSub = config.leaveMsg;
}
// Both can only be equal if at same position, which is fucking impossible, or if both are not found (i.e. both return -1)
if (msgToSub.indexOf("{") == msgToSub.indexOf("}")) {
// If you didn't customise the join message like a fucking monster
channel.send(msgToSub);
return;
}
// Value substitution
let subStart: number = -1;
for (let i = 0; i < msgToSub.length; i++) {
if (msgToSub == "{") {
// Start of field to substitute
subStart = i;
continue;
} else if (msgToSub != "}") {
continue;
} // If not needed
else if (subStart == -1) {
continue;
} // If no substitution needed
// End of bit to substitute
let subValue: string = "";
switch (
msgToSub.slice(subStart + 1, i) // Add substitution values here
) {
case "user":
subValue = `<@${member.user.username}>`;
}
if (subValue != "") {
msgToSub.replace(msgToSub.slice(subStart, i + 1), subValue);
}
subStart = -1;
}
// Send message
if (!config.isLeaveEmbed) {
channel.send(msgToSub);
return;
}
// If embed
let embed: any = config.leaveMsg;
embed.description = msgToSub;
channel.send({ embeds: [embed] });
});
// Run when ready
client.on("ready", async () => {
console.log(`logged in as ${client?.user?.tag}`);
errorChannel = client.channels.cache.get(`${config.botErrorChannel}`);
});
// Login
client.login(process.env.TOKEN);