This repository was archived by the owner on Nov 8, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.js
More file actions
220 lines (182 loc) · 6.76 KB
/
bot.js
File metadata and controls
220 lines (182 loc) · 6.76 KB
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
// Load up the discord.js library. Else throw an error.
try {
var Discord = require('discord.js')
if (process.version.slice(1).split('.')[0] < 12) {
throw new Error('Node 10.0.0 or higher is required. Please upgrade Node.js on your computer / server.')
}
}
catch (e) {
console.error(e.stack)
console.error('Current Node.js version: ' + process.version)
console.error('In case you´ve not installed any required module: \nPlease run \'npm install\' and ensure it passes with no errors!')
process.exit()
}
// Disable @everyone.
const client = new Discord.Client({ disableMentions: 'everyone' });
// Grab the prefix, version, development and token from config.
const { PREFIX, VERSION, DEVELOPMENT, TOKEN, NAME, STEAMAPI } = require('./config')
// const BotListUpdater = require('./modules/bot-list-updater').BotListUpdater
// Dependant Modules
const Util = require('./modules/util')
const SteamAPI = require('steamapi');
const Logger = new Util.Logger();
const fs = require('fs');
// Creating a Command Collection
client.commands = new Discord.Collection();
// Grab all files with a .js from the commands folder
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'))
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
// set a new item in the Collection
// with the key as the command name and the value as the exported module
client.commands.set(command.name, command);
// Check if command alias exists and use those too if they do exist.
if(command.alias) {
for(const alias of command.alias) {
client.commands.set(alias, command)
}
}
}
// Client Events Handlers
// Activate on warning.
client.on('warn', console.warn)
// Activate on error.
client.on('error', console.error)
// Activate when bot loads.
client.on('ready', async () => {
Logger.info('\nStarting ' + NAME + '...\nNode version: ' + process.version + '\nDiscord.js version: ' + Discord.version + '\n')
Logger.info('\n' + NAME + ' is online! Running on version: ' + VERSION + '\n')
// TRUE -> Debugging
// FALSE -> Production
// If development is set to true then change the status to idle.
if (DEVELOPMENT === true) {
// Setting the status and activity of the bot.
client.user.setPresence({
status: 'idle',
activity: {
name: `${PREFIX}help | ${client.guilds.cache.size} servers`,
},
}).catch(e => {
// Error handling
console.error(e)
})
// Warn in console that the bot is set to development mode.
Logger.warn('Bot is currently set in DEVELOPMENT mode. Please refer to config.js!')
} else {
// Setting the status and activity of the bot if development is not active
client.user.setPresence({
status: 'online',
activity: {
name: `${PREFIX}help | ${client.guilds.cache.size} servers`,
},
}).catch(e => {
// Error handling
console.error(e)
})
}
/*
THIS IS FOR WHEN THE BOT IS APPROVED ON SITES
// Creating a new updater
const updater = new BotListUpdater()
// Interval for updating the amount of servers the bot is used on on top.gg every 30 minutes
setInterval(() => {
updater.updateTopGg(client.guilds.cache.size)
}, 1800000);
// Interval for updating the amount of servers the bot is used on on bots.ondiscord.xyz every 10 minutes
setInterval(() => {
updater.updateBotsXyz(client.guilds.cache.size)
}, 600000);
// Interval for updating the amount of servers the bot is used on on discordbotlist.com every 5 minutes
setInterval(() => {
updater.updateDiscordBotList(client.guilds.cache.size, this.totalMembers(), client.voice.connections.size)
}, 300000);
*/
// Console info
Logger.info(`Ready to serve on ${client.guilds.cache.size} servers for a total of ${this.totalMembers()} users.`)
})
// Activates on bot disconnection
client.on('disconnect', () => Logger.info('Disconnected!'))
// Activates on bot reconnection
client.on('reconnecting', () => Logger.info('Reconnecting...'))
// This event will be triggered when the bot joins a guild.
client.on('guildCreate', guild => {
// Logging the event
Logger.info(`Joined server ${guild.name} with ${guild.memberCount} users. Total servers: ${client.guilds.cache.size}`)
// Updating the presence of the bot with the new server amount
client.user.setPresence({
activity: {
name: `${PREFIX}help | ${client.guilds.cache.size} servers`,
},
}).catch(e => {
console.error(e)
})
// Create news channel.
// Create a new channel with permission overwrites
/*guild.channels.create('new-voice', {
type: 'text',
permissionOverwrites: [
{
id: message.author.id,
deny: ['SEND_MESSAGES'],
},
],
}).then(createdChannel => {
var id = createdChannel.id
}) */
// Figure out how to send one update to all the channels
})
// This event will be triggered when the bot is removed from a guild.
// eslint-disable-next-line no-unused-vars
client.on('guildDelete', guild => {
// Logging the event
Logger.info(`Left a server. Total servers: ${client.guilds.cache.size}`)
// Updating the presence of the bot with the new server amount
client.user.setPresence({
activity: {
name: `${PREFIX}help | ${client.guilds.cache.size} servers`,
},
}).catch(e => {
console.error(e)
})
})
/**
* Returns the total amount of users (including bots (sadly...)) who use the bot.
* */
// TODO: How to just return the "normal" users amount without the bots??
exports.totalMembers = () => {
const totalMembersArray = client.guilds.cache.map(guild => {
return guild.memberCount
})
let total = 0;
for(let i = 0; i < totalMembersArray.length; i++) {
total = total + totalMembersArray[i]
}
return total
}
// The data will only be used for analysis.
/* COMMANDS */
client.on('message', async message => {
if (message.mentions.everyone === false && message.mentions.has(client.user)) {
// Send the message of the help command as a response to the user
client.commands.get('help').execute(message, null, { PREFIX, VERSION })
}
if (message.author.bot) return
if (!message.content.startsWith(PREFIX)) return undefined
const args = message.content.split(' ')
let command = message.content.toLowerCase().split(' ')[0]
command = command.slice(PREFIX.length)
// What should the bot do with an unknown command?
if (!client.commands.has(command)) return;
try {
message.react('703635736603262997');
client.commands.get(command).execute(message, args, { PREFIX, VERSION, STEAMAPI });
//message.delete()
//message.reactions.get('703635736603262997').remove().catch(error => console.error('Failed to remove reactions: ', error));
}
catch (error) {
console.error(error);
await message.reply('There was an error trying to execute that command!');
}
})
client.login(TOKEN);
process.on('unhandledRejection', (PromiseRejection) => console.error(`Promise Error -> ${PromiseRejection}`))