forked from Fuseteam/xiaobot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Xiao.js
184 lines (167 loc) · 6.11 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
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',
ws: { intents: Intents.ALL }
});
const { formatNumber } = require('./util/Util');
client.registry
.registerDefaultTypes()
.registerTypesIn(path.join(__dirname, 'types'))
.registerGroups([
['util-public', 'Utility'],
['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'],
['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', 'Voice-Based'],
['phone', 'Phone'],
['other', 'Other'],
['roleplay', 'Roleplay']
])
.registerDefaultCommands({
help: false,
ping: false,
prefix: false,
commandState: false,
unknownCommand: false
})
.registerCommandsIn(path.join(__dirname, 'commands'));
client.on('ready', () => {
client.logger.info(`[READY] Logged in as ${client.user.tag}! ID: ${client.user.id}`);
// 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);
// Set up meme poster interval
if (client.memePoster) {
client.setInterval(async () => {
try {
const post = await client.memePoster.fetchRandomPost(false);
await client.memePoster.post(post);
} catch (err) {
client.logger.error(err);
}
}, client.memePoster.postInterval);
}
// 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-leaderboard.json every 30 minutes
client.setInterval(() => {
try {
client.exportCommandLeaderboard();
} catch (err) {
client.logger.error(`[LEADERBOARD] Failed to export command-leaderboard.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;
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.ownerOrigin && (!msg.channel.topic || !msg.channel.topic.includes('<xiao:phone>'))) return;
if (!call.active) return;
if (call.ownerOrigin && 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 (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) {
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));
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));
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.replace(/{{user}}/gi, `**${member.user.tag}**`));
return null;
} catch {
return null;
}
});
client.on('disconnect', event => {
client.logger.error(`[DISCONNECT] Disconnected with code ${event.code}.`);
client.exportCommandLeaderboard();
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++;
});
client.on('commandError', (command, err) => client.logger.error(`[COMMAND:${command.name}]\n${err.stack}`));
client.login(XIAO_TOKEN);