· 6 years ago · Nov 18, 2019, 01:54 AM
1const http = require("http");
2const express = require("express");
3const app = express();
4app.get("/", (request, response) => {
5 console.log(Date.now() + " Ping Received");
6 response.sendStatus(200);
7});
8app.listen(process.env.PORT);
9setInterval(() => {
10 http.get(`http://${process.env.PROJECT_DOMAIN}.glitch.me/`);
11}, 280000);
12
13//
14const Discord = require("discord.js");
15const client = new Discord.Client();
16const tokens = require("./tokens.json");
17//const secret = require('./secret.json');
18const sql = require("sqlite");
19sql.open("./database.sqlite");
20const DESC_MIN_LENGTH = 30;
21const DESC_MAX_LENGTH = 2000;
22
23let lastDate = [];
24
25const commands = {
26 help: msg => {
27 msg.channel.send({
28 embed: {
29 title: "Help",
30 description: `The bot was created by **Rohan**, if you have any questions or would like to suggest new features or report bugs, please send them a direct message. All commands start with \`${tokens.prefix}\`.`,
31 fields: [
32 {
33 name: "invite",
34 value: "A way to invite this bot to your own guild.",
35 inline: true
36 },
37 {
38 name: "channel",
39 value: "Synchronize advertisement channel.",
40 inline: true
41 },
42 {
43 name: "desc",
44 value: "Set the description of your advertisement.",
45 inline: true
46 },
47 {
48 name: "preview",
49 value: "Preview your advertisement.",
50 inline: true
51 },
52 {
53 name: "advertise",
54 value:
55 "Advertise your guild to every other guild that has the bot setup.",
56 inline: true
57 }
58 ]
59 }
60 });
61 },
62 invite: msg => {
63 sendEmbed(
64 msg,
65 msg.guild.id,
66 msg.channel.id,
67 "Bot can be invited with this [link](https://discordapp.com/api/oauth2/authorize?client_id=645658081555906562&permissions=8&scope=bot)."
68 );
69 },
70 advertise: msg => {
71 const ignoreCooldown = false;
72 const now = new Date();
73 let cooldown = 2 * 60 * 1000;
74 if (lastDate[msg.guild.id] === undefined) {
75 lastDate[msg.guild.id] = 0;
76 }
77 if (now - lastDate[msg.guild.id] > cooldown || ignoreCooldown) {
78
79 let desc = null;
80 sql.all("SELECT * FROM settings").then(row => {
81 msg.guild.fetchInvites().then(invites => {
82 if (invites.size > 0) {
83 const invite = invites.first();
84
85 advertiseLogic(msg, row, invite);
86 } else {
87 let channelID;
88 let channels = msg.guild.channels;
89 for (let c of channels) {
90 let channelType = c[1].type;
91 if (channelType === "text") {
92 channelID = c[0];
93 break;
94 }
95 }
96
97 let channel = channels.get(msg.guild.systemChannelID || channelID);
98 channel.createInvite().then(invite => {
99 advertiseLogic(msg, row, invite);
100 });
101 }
102 });
103 });
104 lastDate[msg.guild.id] = now;
105 } else {
106
107 let remaining = Math.round(
108 (cooldown - (now - lastDate[msg.guild.id])) / 1000
109 );
110 sendEmbed(
111 msg,
112 msg.guild.id,
113 msg.channel.id,
114 `You must wait **${remaining} seconds** before you can use this command again.`
115 );
116 }
117 },
118 init: msg => {
119 if (!msg.member.hasPermission("ADMINISTRATOR")) {
120 return sendEmbed(
121 msg,
122 msg.guild.id,
123 msg.channel.id,
124 "You need to have the administrator permission to do this."
125 );
126 }
127 const args = msg.content
128 .slice(tokens.prefix.length)
129 .trim()
130 .split(/ +/g)
131 .slice(1);
132 if (args[0] === undefined) {
133 return sendEmbed(
134 msg,
135 msg.guild.id,
136 msg.channel.id,
137 "Please specifty a channel."
138 );
139 }
140 let channel = client.guilds
141 .get(msg.guild.id)
142 .channels.find("name", args[0]);
143 if (channel) {
144 sql.run("UPDATE settings SET partner = ? WHERE guildid = ?", [
145 channel.id,
146 msg.guild.id
147 ]);
148 sendEmbed(
149 msg,
150 msg.guild.id,
151 msg.channel.id,
152 "Channel sucessfully synchronized."
153 );
154 } else {
155 sendEmbed(msg, msg.guild.id, msg.channel.id, "Invalid channel.");
156 }
157 },
158 desc: msg => {
159 if (!msg.member.hasPermission("ADMINISTRATOR")) {
160 return sendEmbed(
161 msg,
162 msg.guild.id,
163 msg.channel.id,
164 "You need to have the administrator permission to do this."
165 );
166 }
167 const string = msg.content
168 .slice(tokens.prefix.length)
169 .trim()
170 .split(/ +/g)
171 .slice(1)
172 .join(" ");
173 if (string === undefined || string === "") {
174 return sendEmbed(
175 msg,
176 msg.guild.id,
177 msg.channel.id,
178 "Please specify a description."
179 );
180 }
181
182 if (string.length > DESC_MAX_LENGTH) {
183 return sendEmbed(
184 msg,
185 msg.guild.id,
186 msg.channel.id,
187 `Description can not be more then ${DESC_MAX_LENGTH} characters long.`
188 );
189 }
190 if (string.length < DESC_MIN_LENGTH) {
191 return sendEmbed(
192 msg,
193 msg.guild.id,
194 msg.channel.id,
195 `Description must have at least ${DESC_MIN_LENGTH} characters in it.`
196 );
197 }
198
199 sql.run("UPDATE settings SET desc = ? WHERE guildid = ?", [
200 string,
201 msg.guild.id
202 ]);
203 sendEmbed(
204 msg,
205 msg.guild.id,
206 msg.channel.id,
207 "Description sucessfully updated."
208 );
209 },
210 preview: msg => {
211 if (!msg.member.hasPermission("ADMINISTRATOR")) {
212 return sendEmbed(
213 msg,
214 msg.guild.id,
215 msg.channel.id,
216 "You need to have the administrator permission to do this."
217 );
218 }
219 sql
220 .get("SELECT * FROM settings WHERE guildid = ?", [msg.guild.id])
221 .then(row => {
222 let str = [`__**${msg.guild.name}**__`, `${row.desc} [Invite]`];
223
224 msg.channel.send(str.join("\n\n"));
225 });
226 }
227};
228
229client.on("ready", () => {
230
231 client.user.setActivity(`${tokens.prefix}help`, {
232 url: "https://www.twitch.tv/sup beeches"
233 });
234 console.log(
235 `${client.user.tag} running on ${client.guilds.size} guilds with ${client.users.size} users.yo`
236 );
237
238 sql
239 .run(
240 "CREATE TABLE IF NOT EXISTS settings (guildid TEXT UNIQUE, partner CHARACTER, desc VARCHAR)"
241 )
242 .then(() => {
243 for (const guild of client.guilds.values()) {
244 sql.run("INSERT OR IGNORE INTO settings (guildid) VALUES (?)", [
245 guild.id
246 ]);
247 }
248 });
249});
250
251client.on("guildCreate", guild => {
252 console.log(`I have joined the guild ${guild.name}`);
253 sql.run("INSERT OR IGNORE INTO settings (guildid) VALUES (?)", [guild.id]);
254});
255
256client.on("guildDelete", guild => {
257 console.log(`I have left the guild ${guild.name}`);
258 sql.run("DELETE * FROM settings WHERE guildid = ?", [guild.id]);
259});
260
261client.on("message", async msg => {
262
263 if (msg.author.bot) return;
264 if (msg.channel.type !== "text") return;
265
266 if (msg.content.startsWith(tokens.prefix + "ping")) {
267 const m = await msg.channel.send("Ping?");
268 m.edit(
269 `Pong! Latency is ${m.createdTimestamp -
270 msg.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms.`
271 );
272 return;
273 }
274
275
276 if (!msg.content.startsWith(tokens.prefix)) return;
277 console.log(`[${msg.guild.name}] ${msg.author.tag} >> ${msg.content}`);
278 const cmd = msg.content
279 .toLowerCase()
280 .slice(tokens.prefix.length)
281 .split(" ")[0];
282 if (commands.hasOwnProperty(cmd)) {
283
284 commands[cmd](msg);
285 }
286});
287
288function advertiseLogic(msg, row, invite) {
289 for (let i = 0; i < row.length; i++) {
290 let guild = client.guilds.get(row[i].guildid);
291
292 for (let a = 0; a < row.length; a++) {
293 let temp = client.guilds.get(row[a].guildid);
294 if (temp) {
295 if (temp.id === msg.guild.id) {
296 if (!msg.guild.channels.has(row[a].partner)) {
297 sendEmbed(
298 msg,
299 msg.guild.id,
300 msg.channel.id,
301 `You must first initialize a channel for the bot in ${msg.guild.name} with \`${tokens.prefix}init\`before you can bump your server.`
302 );
303 lastDate[msg.guild.id] = 0;
304 return;
305 }
306 desc = row[a].desc;
307 break;
308 }
309 }
310 }
311
312 if (desc === undefined || desc === null) {
313 lastDate[msg.guild.id] = 0;
314 return sendEmbed(
315 msg,
316 msg.guild.id,
317 msg.channel.id,
318 `A description for ${msg.guild.name} has not been set yet. Please set one.`
319 );
320 }
321 if (guild) {
322 if (guild.channels.has(row[i].partner) && guild.id !== msg.guild.id) {
323 let str = [`__**${msg.guild.name}**__`, `${desc} ${invite.url}`];
324
325 guild.channels.get(row[i].partner).send(str.join("\n\n"));
326 }
327 }
328 }
329 sendEmbed(
330 msg,
331 msg.guild.id,
332 msg.channel.id,
333 `Bumped sucessfully to **${row.length - 1}** guilds.`
334 );
335}
336
337function sendEmbed(msg, guildid, channelid, str) {
338 const embed_object = {
339 embed: {
340 description: str,
341 color: 0xffc4f9
342 }
343 };
344
345 if (!msg.channel.permissionsFor(client.user).has("EMBED_LINKS")) {
346 return msg.channel.send("I need the embed links permission.");
347 }
348
349 if (!msg.channel.permissionsFor(client.user).has("MANAGE_MESSAGES")) {
350 return msg.channel.send("I need manage messages permission.");
351 }
352
353 let guild = client.guilds.get(guildid);
354
355 guild.channels
356 .get(channelid)
357 .send(embed_object)
358 .then(m => {});
359}
360
361client.login(process.env.BOT_TOKEN);