forked from Natemo2625/xiao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXiao.js
262 lines (238 loc) · 8.83 KB
/
Xiao.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
require('dotenv').config();
const { XIAO_TOKEN, OWNERS, XIAO_PREFIX, INVITE } = process.env;
const path = require('path');
const { Intents, MessageEmbed } = require('discord.js');
const Client = require('./structures/Client');
const client = new Client({
commandPrefix: XIAO_PREFIX,
owner: OWNERS.split(','),
invite: INVITE,
disableMentions: 'everyone',
partials: ['GUILD_MEMBER'],
ws: { intents: [Intents.NON_PRIVILEGED, 'GUILD_MEMBERS'] }
});
const { formatNumber } = require('./util/Util');
client.registry
.registerDefaultTypes()
.registerTypesIn(path.join(__dirname, 'types'))
.registerGroups([
['util-public', 'Utility'],
['util-voice', 'Utility (Voice)'],
['util', 'Utility (Owner)'],
['info', 'Discord Information'],
['random-res', 'Random Response'],
['random-img', 'Random Image'],
['random-seed', 'Seeded Randomizers'],
['single', 'Single Response'],
['auto', 'Automatic Response'],
['events', 'Events'],
['search', 'Search'],
['pokedex', 'Pokédex'],
['analyze', 'Analyzers'],
['games-sp', 'Single-Player Games'],
['games-mp', 'Multi-Player Games'],
['edit-image', 'Image Manipulation'],
['edit-avatar', 'Avatar Manipulation'],
['edit-meme', 'Meme Generators'],
['edit-text', 'Text Manipulation'],
['edit-number', 'Number Manipulation'],
['voice', 'Play Audio'],
['music', 'Music'],
['remind', 'Reminders'],
['phone', 'Phone'],
['code', 'Coding Tools'],
['other', 'Other'],
['roleplay', 'Roleplay']
])
.registerDefaultCommands({
help: false,
ping: false,
prefix: false,
commandState: false,
unknownCommand: false
})
.registerCommandsIn(path.join(__dirname, 'commands'));
client.on('ready', async () => {
client.logger.info(`[READY] Logged in as ${client.user.tag}! ID: ${client.user.id}`);
// Register all canvas fonts
await client.registerFontsIn(path.join(__dirname, 'assets', 'fonts'));
// Set up existing timers
await client.timers.fetchAll();
// Push client-related activities
client.activities.push(
{ text: () => `${formatNumber(client.guilds.cache.size)} servers`, type: 'WATCHING' },
{ text: () => `with ${formatNumber(client.registry.commands.size)} commands`, type: 'PLAYING' },
{ text: () => `${formatNumber(client.channels.cache.size)} channels`, type: 'WATCHING' }
);
// Interval to change activity every minute
client.setInterval(() => {
const activity = client.activities[Math.floor(Math.random() * client.activities.length)];
const text = typeof activity.text === 'function' ? activity.text() : activity.text;
client.user.setActivity(text, { type: activity.type });
}, 60000);
// Import blacklist
try {
const results = client.importBlacklist();
if (!results) client.logger.error('[BLACKLIST] blacklist.json is not formatted correctly.');
} catch (err) {
client.logger.error(`[BLACKLIST] Could not parse blacklist.json:\n${err.stack}`);
}
// Make sure bot is not in any blacklisted guilds
for (const id of client.blacklist.guild) {
try {
const guild = await client.guilds.fetch(id, false);
await guild.leave();
client.logger.info(`[BLACKLIST] Left blacklisted guild ${id}.`);
} catch {
if (!client.guilds.cache.has(id)) continue;
client.logger.info(`[BLACKLIST] Failed to leave blacklisted guild ${id}.`);
}
}
// Make sure bot is not in any guilds owned by a blacklisted user
let guildsLeft = 0;
for (const guild of client.guilds.cache.values()) {
if (client.blacklist.user.includes(guild.ownerID)) {
try {
await guild.leave();
guildsLeft++;
} catch {
client.logger.info(`[BLACKLIST] Failed to leave blacklisted guild ${guild.id}.`);
}
}
}
client.logger.info(`[BLACKLIST] Left ${guildsLeft} guilds owned by blacklisted users.`);
// Import command-leaderboard.json
try {
const results = client.importCommandLeaderboard();
if (!results) client.logger.error('[LEADERBOARD] command-leaderboard.json is not formatted correctly.');
} catch (err) {
client.logger.error(`[LEADERBOARD] Could not parse command-leaderboard.json:\n${err.stack}`);
}
// Export command-last-run.json
try {
const results = client.importLastRun();
if (!results) client.logger.error('[LASTRUN] command-last-run.json is not formatted correctly.');
} catch (err) {
client.logger.error(`[LASTRUN] Could not parse command-last-run.json:\n${err.stack}`);
}
// Export command-leaderboard.json and command-last-run.json every 30 minutes
client.setInterval(() => {
try {
client.exportCommandLeaderboard();
} catch (err) {
client.logger.error(`[LEADERBOARD] Failed to export command-leaderboard.json:\n${err.stack}`);
}
try {
client.exportLastRun();
} catch (err) {
client.logger.error(`[LASTRUN] Failed to export command-last-run.json:\n${err.stack}`);
}
}, 1.8e+6);
});
client.on('message', async msg => {
const hasText = Boolean(msg.content);
const hasImage = msg.attachments.size !== 0;
const hasEmbed = msg.embeds.length !== 0;
if (msg.author.bot || (!hasText && !hasImage && !hasEmbed)) return;
if (client.blacklist.user.includes(msg.author.id)) return;
const origin = client.phone.find(call => call.origin.id === msg.channel.id);
const recipient = client.phone.find(call => call.recipient.id === msg.channel.id);
if (!origin && !recipient) return;
const call = origin || recipient;
if (call.originDM && call.startUser.id !== msg.author.id) return;
if (!call.adminCall && (msg.guild && (!msg.channel.topic || !msg.channel.topic.includes('<xiao:phone>')))) return;
if (!call.active) return;
if (call.adminCall && msg.guild.id === call.origin.guild.id && !client.isOwner(msg.author)) return;
try {
await call.send(origin ? call.recipient : call.origin, msg, hasText, hasImage, hasEmbed);
} catch {
return; // eslint-disable-line no-useless-return
}
});
client.on('guildCreate', async guild => {
if (client.blacklist.guild.includes(guild.id) || client.blacklist.user.includes(guild.ownerID)) {
try {
await guild.leave();
return;
} catch {
return;
}
}
if (guild.systemChannel && guild.systemChannel.permissionsFor(client.user).has('SEND_MESSAGES')) {
try {
const usage = client.registry.commands.get('help').usage();
await guild.systemChannel.send(`Hi! I'm Xiao, use ${usage} to see my commands, yes?`);
} catch {
// Nothing!
}
}
const joinLeaveChannel = await client.fetchJoinLeaveChannel();
if (joinLeaveChannel) {
if (!guild.members.cache.has(guild.ownerID)) await guild.members.fetch(guild.ownerID);
const embed = new MessageEmbed()
.setColor(0x7CFC00)
.setThumbnail(guild.iconURL({ format: 'png' }))
.setTitle(`Joined ${guild.name}!`)
.setFooter(`ID: ${guild.id}`)
.setTimestamp()
.addField('❯ Members', formatNumber(guild.memberCount))
.addField('❯ Owner', guild.owner.user.tag);
await joinLeaveChannel.send({ embed });
}
});
client.on('guildDelete', async guild => {
const joinLeaveChannel = await client.fetchJoinLeaveChannel();
if (joinLeaveChannel) {
const embed = new MessageEmbed()
.setColor(0xFF0000)
.setThumbnail(guild.iconURL({ format: 'png' }))
.setTitle(`Left ${guild.name}...`)
.setFooter(`ID: ${guild.id}`)
.setTimestamp()
.addField('❯ Members', formatNumber(guild.memberCount))
.addField('❯ Owner', guild.ownerID);
await joinLeaveChannel.send({ embed });
}
});
client.on('guildMemberRemove', async member => {
if (member.id === client.user.id) return null;
const channel = member.guild.systemChannel;
if (!channel || !channel.permissionsFor(client.user).has('SEND_MESSAGES')) return null;
if (channel.topic && channel.topic.includes('<xiao:disable-leave>')) return null;
try {
const leaveMessage = client.leaveMessages[Math.floor(Math.random() * client.leaveMessages.length)];
await channel.send(leaveMessage.replaceAll('{{user}}', `**${member.user.tag}**`));
return null;
} catch {
return null;
}
});
client.on('voiceStateUpdate', (oldState, newState) => {
if (newState.id !== client.user.id || oldState.id !== client.user.id) return;
if (newState.channel) return;
const dispatcher = client.dispatchers.get(oldState.guild.id);
if (!dispatcher) return;
dispatcher.end();
client.dispatchers.delete(oldState.guild.id);
});
client.on('disconnect', event => {
client.logger.error(`[DISCONNECT] Disconnected with code ${event.code}.`);
client.exportCommandLeaderboard();
client.exportLastRun();
process.exit(0);
});
client.on('error', err => client.logger.error(err.stack));
client.on('warn', warn => client.logger.warn(warn));
client.on('commandRun', command => {
if (command.uses === undefined) return;
command.uses++;
if (command.lastRun === undefined) return;
command.lastRun = new Date();
});
client.dispatcher.addInhibitor(msg => {
if (client.blacklist.user.includes(msg.author.id)) return 'blacklisted';
if (msg.guild && client.blacklist.guild.includes(msg.guild.id)) return 'blacklisted';
return false;
});
client.on('commandError', (command, err) => client.logger.error(`[COMMAND:${command.name}]\n${err.stack}`));
client.login(XIAO_TOKEN);