Files
xiao/Xiao.js
T
Dragon Fire fe9b72d771 Fix
2021-03-10 17:30:46 -05:00

307 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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-image-text', 'Image Text Manipulation'],
['edit-avatar', 'Avatar Manipulation'],
['edit-meme', 'Meme Generators'],
['edit-text', 'Text Manipulation'],
['edit-number', 'Number Manipulation'],
['voice', 'Play Audio'],
['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}`);
// 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 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}`);
}
// Import 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);
// 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.`);
// Set up existing timers
await client.timers.fetchAll();
// Register all canvas fonts
await client.registerFontsIn(path.join(__dirname, 'assets', 'fonts'));
// Fetch adult site list
try {
await client.fetchAdultSiteList();
} catch (err) {
client.logger.error(`[ADULT SITES] Failed to fetch list\n${err.stack}`);
}
});
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;
if (msg.isCommand && msg.channel.type !== 'dm') return;
// Cleverbot handler
const cleverbot = client.cleverbots.get(msg.channel.id);
if (cleverbot) {
if (!cleverbot.shouldRespond(msg)) return;
client.registry.commands.get('cleverbot').uses++;
msg.channel.startTyping().catch(() => null);
try {
const response = await cleverbot.respond(msg.cleanContent);
msg.channel.stopTyping(true);
await msg.channel.send(response);
return;
} catch (err) {
msg.channel.stopTyping(true);
if (err.status === 503) {
await msg.channel.send('Monthly API limit reached. Ending conversation.');
client.cleverbots.delete(msg.channel.id);
return;
}
await msg.channel.send('Sorry, blacked out there for a second. Come again?');
return;
}
}
// Phone message handler
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', async (oldState, newState) => {
if (newState.channel) return;
if (oldState.id !== client.user.id) {
const channel = await client.channels.fetch(oldState.channelID);
if (channel.members.size === 1 && channel.members.has(client.user.id)) {
const dispatcher = client.dispatchers.get(oldState.guild.id);
if (dispatcher) {
dispatcher.end();
client.dispatchers.delete(oldState.guild.id);
}
channel.leave();
}
} else {
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);