-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
175 lines (151 loc) · 5.77 KB
/
bot.js
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
// LIBRARIES & FUNCTIONS
const Discord = require('discord.js');
const client = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES, Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS] });
const func = require("./functions.js");
const fs = require("fs");
const database = require("./databaseWrapper.js");
// SET TRUE WHEN TESTING TO DISABLE TOPGG Posting & TO USE TEST BOT TOKEN
var TESTING = false;
// DATA IMPORT
const package = require("./package.json");
const COMMANDCODE = require("./commandcodes.json");
// GLOBAL VARIABLES
var botData =
{
servercount: 0,
usercount: 0,
botcount: 0,
channelcount: 0,
version: package.version,
hltvURL: "https://www.hltv.org",
topggVoteURL: "https://top.gg/bot/548165454158495745/vote",
titleSpacer: "\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800",
interactionRow: {},
reactionControls:
{
PREV_PAGE: '⬅',
NEXT_PAGE: '➡',
STOP: '⏹',
},
COMMANDCODE: COMMANDCODE,
hltvIMG: ""
}
const row = new Discord.MessageActionRow()
.addComponents(
new Discord.MessageButton()
.setStyle('LINK')
.setLabel("Vote!")
.setURL(botData.topggVoteURL),
new Discord.MessageButton()
.setCustomId(botData.reactionControls.PREV_PAGE)
.setStyle('SECONDARY')
.setLabel(" ")
.setEmoji(botData.reactionControls.PREV_PAGE),
new Discord.MessageButton()
.setCustomId(botData.reactionControls.STOP)
.setStyle('SECONDARY')
.setLabel(" ")
.setEmoji(botData.reactionControls.STOP),
new Discord.MessageButton()
.setCustomId(botData.reactionControls.NEXT_PAGE)
.setStyle('SECONDARY')
.setLabel(" ")
.setEmoji(botData.reactionControls.NEXT_PAGE),
new Discord.MessageButton()
.setStyle('LINK')
.setLabel("HLTV")
.setURL(botData.hltvURL)
);
if(!TESTING)
{
// handle top.gg stat posting
const { AutoPoster } = require('topgg-autoposter');
AutoPoster(process.env.TOPGG_TOKEN, client);
// ap.on('posted', (stats) =>
// {
// console.log(`Posted stats to Top.gg | ${stats.serverCount} servers`)
// })
}
else
{
const testConfig = require('./config.json');
process.env.prefix = testConfig.prefix;
process.env.BOT_TOKEN = testConfig.BOT_TOKEN;
process.env.DATABASE_URL = testConfig.DATABASE_URL;
process.env.DISCORDBOTS_TOKEN = testConfig.DISCORDBOTS_TOKEN;
}
const commandFiles = fs.readdirSync("./commands").filter(file => file.endsWith(".js"));
const commandsArr = [];
client.commands = new Discord.Collection();
for (const file of commandFiles)
{
const command = require(`./commands/${file}`);
commandsArr.push(command.data.toJSON());
client.commands.set(command.data.name, command);
}
console.log(`Loaded ${commandsArr.length} commands`);
client.on("ready", () =>
{
// STATISTICS GATHERING
client.guilds.cache.forEach((guild) =>
{
if (guild.id == "264445053596991498") //top.gg discord guildId, ignored since it isn't "real" users for statistics
return;
botData = func.checkStats(guild, botData, true);
})
database.authenticate(true);
botData.hltvIMG = client.user.displayAvatarURL();
botData.interactionRow = row;
const guild = client.guilds.cache.get('509391645226172420'); //development server guildid
if(TESTING)
guild.commands.set(commandsArr);
else
client.application.commands.set(commandsArr);
//func.postGuildCount(client.user.id, botData.servercount, process.env.DISCORDBOTS_TOKEN);
console.log(`HLTVBot v${botData.version} is currently serving ${botData.usercount} users, in ${botData.channelcount} channels of ${botData.servercount} servers. Alongside ${botData.botcount} bot brothers.`);
client.user.setActivity(`${botData.servercount} servers | /help | re-add for slash command permissions`, { type: 'WATCHING' });
});
client.on("guildCreate", guild =>
{
botData = func.checkStats(guild, botData, true);
//func.postGuildCount(client.user.id, botData.servercount, process.env.DISCORDBOTS_TOKEN);
console.log(`New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members! Guild Count:${botData.servercount}`);
client.user.setActivity(`${botData.servercount} servers | /help | re-add for slash command permissions`, { type: 'WATCHING' });
});
client.on("guildDelete", guild =>
{
botData = func.checkStats(guild, botData, false);
//func.postGuildCount(client.user.id, botData.servercount, process.env.DISCORDBOTS_TOKEN);
console.log(`I have been removed from: ${guild.name} (id: ${guild.id}). This guild had ${guild.memberCount} members! Guild Count:${botData.servercount}`);
client.user.setActivity(`${botData.servercount} servers | /help | re-add for slash command permissions`, { type: 'WATCHING' });
});
client.on("interactionCreate", async (interaction) =>
{
if (!interaction.isCommand())
return;
const command = client.commands.get(interaction.commandName);
if (!command)
return;
try
{
await interaction.deferReply();
await command.execute(interaction, client, botData);
}
catch(err)
{
console.log("\n\nEntered root command error state\n\n");
if (err)
console.log(err);
var embed = new Discord.MessageEmbed()
.setTitle("Error Occurred")
.setColor(0x00AE86)
.setTimestamp()
.setFooter({text: "Sent by HLTVBot", iconURL: botData.hltvIMG})
.setDescription(`An error occurred whilst executing slash command.\nPlease try again or visit [hltv.org](${botData.hltvURL})\nIf this persists please re-add the bot to the server to refresh bot permissions.`);
if(interaction.deferred)
await interaction.editReply({ embeds: [embed] });
else
await interaction.reply({ embeds: [embed] });
}
})
client.login(process.env.BOT_TOKEN);