· 5 years ago · May 04, 2020, 10:40 AM
1const Discord = require("discord.js");
2const client = new Discord.Client();
3const settings = require("./settings.json");
4const sql = require("sqlite");
5const prettyMS = require("pretty-ms");
6require("dotenv").config();
7sql.open("./database.sqlite");
8
9const DESC_MIN_LENGTH = 30;
10const DESC_MAX_LENGTH = 500;
11
12const lastDate = [];
13
14const commands = {
15 help: msg => {
16 msg.channel.send({
17 embed: {
18 title: "Help",
19 description: `The bot was Coded/Created by **JamieG#6884**, 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 \`${settings.prefix}\`.`,
20 fields: [
21 {
22 name: "invite",
23 value: "A Way To Invite Project Bump To Your Server!.",
24 inline: true
25 },
26 {
27 name: "setup",
28 value: "Synchronize An Advertisement Channel.",
29 inline: true
30 },
31 {
32 name: "desc",
33 value: "Set The Description Of Your Advertisement.",
34 inline: true
35 },
36 {
37 name: "preview",
38 value: "Preview Your Advertisement.",
39 inline: true
40 },
41 {
42 name: "support",
43 value: "Join Our Support Server!.",
44 inline: true
45 },
46 {
47 name: "bump",
48 value: "Bump Your Advertisements!.",
49 inline: true
50 },
51 {
52 name: "vote",
53 value: "Vote For Our Bot If You Are Enjoying It!.",
54 inline: true
55 },
56 {
57 name: "auto",
58 value: "Automatically Bump Your Advertisements Every Hour!.",
59 inline: true
60 }
61 ]
62 }
63 });
64 },
65 invite: msg => {
66 sendEmbed(msg, "I've sent you a private message with the bot invite link.");
67 msg.author.send(
68 `https://discordapp.com/api/oauth2/authorize?client_id=${client.user.id}&scope=bot&permissions=8`
69 );
70 },
71 support: msg => {
72 sendEmbed(
73 msg,
74 "I've Sent You A Private Message With The Support Server invite link."
75 );
76 msg.author.send("discord.gg/UGzCqTG");
77 },
78 vote: msg => {
79 sendEmbed(
80 msg,
81 "You Can Vote For Our Bot With The Link I Sent You In Your Messages."
82 );
83 msg.author.send(
84 "You Can Vote For Us Here We Appreciate Your Votes! :heart: : https://top.gg/bot/696284381429497907"
85 );
86 },
87 bump: msg => {
88 const ignoreCooldown = false;
89 const now = new Date();
90 const cooldown = 60 * 60 * 1000;
91 if (lastDate[msg.guild.id] === undefined) {
92 lastDate[msg.guild.id] = 0;
93 }
94 if (now - lastDate[msg.guild.id] > cooldown || ignoreCooldown) {
95 sql.all("SELECT * FROM settings").then(row => {
96 msg.guild.fetchInvites().then(invites => {
97 if (row.length - 1 <= 0) {
98 sendEmbed(
99 msg,
100 "There are no other guilds for your advertisement to go, `p%invite` and setup the bot on other guilds before trying again."
101 );
102 return;
103 }
104 if (invites.size > 0) {
105 const invite = invites.first();
106
107 bumpLogic(msg, row, invite);
108 } else {
109 let channelID;
110 const channels = msg.guild.channels.cache;
111 for (const c of channels) {
112 const channelType = c[1].type;
113 if (channelType === "text") {
114 channelID = c[0];
115 break;
116 }
117 }
118
119 const channel = channels.get(
120 msg.guild.systemChannelID || channelID
121 );
122 channel.createInvite().then(invite => {
123 bumpLogic(msg, row, invite);
124 sendEmbed(
125 msg,
126 `Bumped sucessfully to **${row.length - 1}** Servers.`
127 );
128 });
129 }
130 });
131 });
132 lastDate[msg.guild.id] = now;
133 } else {
134 const remaining = prettyMS(
135 Math.round(cooldown - (now - lastDate[msg.guild.id])),
136 { verbose: true, unitCount: 3, secondsDecimalDigits: 0 }
137 );
138 sendEmbed(
139 msg,
140 `You must wait **${remaining}** before you can use this command again.`
141 );
142 }
143 },
144 setup: msg => {
145 if (!msg.member.hasPermission("ADMINISTRATOR")) {
146 return sendEmbed(
147 msg,
148 "You need to have the administrator permission to do this."
149 );
150 }
151 const args = msg.content
152 .slice(settings.prefix.length)
153 .trim()
154 .split(/ +/g)
155 .slice(1);
156 if (args[0] === undefined) {
157 return sendEmbed(msg, "Please specify a channel.");
158 }
159 console.log(args[0].replace(/[<#>]/g, ""));
160 const channel = client.guilds.cache
161 .get(msg.guild.id)
162 .channels.cache.find(channel =>
163 [channel.name, channel.id].includes(args[0].replace(/[<#>]/g, ""))
164 );
165 if (channel) {
166 sql.run("UPDATE settings SET partner = ? WHERE guildid = ?", [
167 channel.id,
168 msg.guild.id
169 ]);
170 sendEmbed(
171 msg,
172 "Success! Now go ahead and give your advertisement a `p%desc` then `p%bump` it!"
173 );
174 } else {
175 sendEmbed(msg, "Invalid channel.");
176 }
177 },
178 desc: msg => {
179 if (!msg.member.hasPermission("ADMINISTRATOR")) {
180 return sendEmbed(
181 msg,
182 "You need to have the administrator permission to do this."
183 );
184 }
185 const desc = msg.content
186 .slice(settings.prefix.length)
187 .trim()
188 .split(/ +/g)
189 .slice(1)
190 .join(" ");
191 if (desc === undefined || desc === "") {
192 return sendEmbed(
193 msg,
194 "Specify a guild description. Note that your guild invite will be attached automatically."
195 );
196 }
197
198 if (desc.length > settings.ad.desc.max) {
199 return sendEmbed(
200 msg,
201 `Description can not be more then ${DESC_MAX_LENGTH} characters long.`
202 );
203 }
204 if (desc.length < settings.ad.desc.min) {
205 return sendEmbed(
206 msg,
207 `Description must have at least ${DESC_MIN_LENGTH} characters in it.`
208 );
209 }
210 if (
211 desc.includes("discord.gg/") ||
212 desc.includes("@everyone") ||
213 desc.includes("@here")
214 ) {
215 return msg.channel.send(
216 "No Invite links (We Provide One) or mentions in the description please."
217 );
218 }
219 sql.run("UPDATE settings SET desc = ? WHERE guildid = ?", [
220 desc,
221 msg.guild.id
222 ]);
223 sendEmbed(msg, "Description sucessfully updated.");
224 },
225 preview: msg => {
226 if (!msg.member.hasPermission("ADMINISTRATOR")) {
227 return sendEmbed(
228 msg,
229 "You need to have the administrator permission to do this."
230 );
231 }
232 sql
233 .get("SELECT * FROM settings WHERE guildid = ?", [msg.guild.id])
234 .then(row => {
235 const str = [
236 `__**${msg.guild.name}**__\n`,
237 `${row.desc} \`[Invite will Appear Here]\``
238 ];
239
240 msg.channel.send(str.join("\n"));
241 });
242 }
243};
244
245client.on("ready", () => {
246 console.clear();
247
248 console.log(`${settings.prefix}help`, {
249 type: "PLAYING"
250 });
251});
252
253client.on("message", function(message) {
254 if (message.content == "ab!start") {
255 message.channel.send(
256 "**Auto-bumping started! Next message in 20 minutes from now!**"
257 );
258 var interval = setInterval(function() {
259 message.channel.send("!bump");
260 }, 1200000);
261 }
262 client.user.setActivity(
263 `p%help | Running on ${client.guilds.cache.size} Servers with ${client.users.cache.size} users.`
264 );
265 client.setInterval(
266 () =>
267 client.user.setActivity(
268 `p%help | Running on ${client.guilds.cache.size} Servers with ${client.users.cache.size} users.`
269 ),
270 900000
271 );
272
273 sql
274 .run(
275 "CREATE TABLE IF NOT EXISTS settings (guildid TEXT UNIQUE, partner CHARACTER, desc VARCHAR)"
276 )
277 .then(() => {
278 for (const guild of client.guilds.cache.values()) {
279 sql.run("INSERT OR IGNORE INTO settings (guildid) VALUES (?)", [
280 guild.id
281 ]);
282 }
283 });
284});
285
286client.on("guildCreate", guild => {
287 console.log(`I have joined the guild ${guild.name}`);
288 sql.run("INSERT OR IGNORE INTO settings (guildid) VALUES (?)", [guild.id]);
289});
290
291client.on("guildDelete", guild => {
292 console.log(`I have left the guild ${guild.name}`);
293 sql.run("DELETE * FROM settings WHERE guildid = ?", [guild.id]);
294});
295
296client.on("message", async msg => {
297 if (msg.author.bot) return;
298 if (msg.channel.type !== "text") return;
299
300 if (msg.content.startsWith(settings.prefix + "ping")) {
301 const m = await msg.channel.send("Ping?");
302 m.edit(
303 `Pong! Latency is ${m.createdTimestamp -
304 msg.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms.`
305 );
306 return;
307 }
308
309 if (!msg.content.startsWith(settings.prefix)) return;
310 console.log(`[${msg.guild.name}] ${msg.author.tag} >> ${msg.content}`);
311 const cmd = msg.content
312 .toLowerCase()
313 .slice(settings.prefix.length)
314 .split(" ")[0];
315
316 if (commands.hasOwnProperty(cmd)) {
317 commands[cmd](msg);
318 }
319});
320
321function bumpLogic(msg, row, invite) {
322 for (let i = 0; i < row.length; i++) {
323 const guild = client.guilds.cache.get(row[i].guildid);
324 let desc = null;
325
326 for (let a = 0; a < row.length; a++) {
327 const temp = client.guilds.cache.get(row[a].guildid);
328 if (temp) {
329 if (temp.id === msg.guild.id) {
330 if (!msg.guild.channels.cache.has(row[a].partner)) {
331 sendEmbed(
332 msg,
333 `You must first initialize a channel for the bot in ${msg.guild.name} with \`${settings.prefix}init\` before you can bump your server.`
334 );
335 lastDate[msg.guild.id] = 0;
336 return;
337 }
338 desc = row[a].desc;
339 break;
340 }
341 }
342 }
343
344 if (desc === undefined || desc === null) {
345 lastDate[msg.guild.id] = 0;
346 return sendEmbed(
347 msg,
348 `A description for ${msg.guild.name} has not been set yet. Please set one.`
349 );
350 }
351 if (guild) {
352 if (
353 guild.channels.cache.has(row[i].partner) &&
354 guild.id !== msg.guild.id
355 ) {
356 const str = [`__**${msg.guild.name}**__`, `${desc} ${invite.url}`];
357
358 guild.channels.cache.get(row[i].partner).send(str.join("\n\n"));
359 }
360 }
361 }
362 sendEmbed(
363 msg,
364 `Bumped to ${row.length - 1} Servers! You May Bump Again In 1 Hour!`
365 );
366}
367
368function sendEmbed(msg, str) {
369 const embedObject = {
370 embed: {
371 description: str
372 }
373 };
374
375 if (!msg.channel.permissionsFor(client.user).has("EMBED_LINKS")) {
376 return msg.channel.send("I need the embed links permission.");
377 }
378
379 if (!msg.channel.permissionsFor(client.user).has("MANAGE_MESSAGES")) {
380 return msg.channel.send("I need manage messages permission.");
381 }
382
383 msg.channel.send(embedObject);
384}
385
386client.login(process.env.BOT_TOKEN);