Port to ESM modules (haha funny), removed cache request, many other changes that I forgot about

This commit is contained in:
Essem 2021-08-19 09:19:14 -05:00
parent 2fe45d842b
commit ae2ebe0337
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
157 changed files with 1661 additions and 897 deletions

View file

@ -1,9 +1,9 @@
const db = require("../utils/database.js");
const logger = require("../utils/logger.js");
import db from "../utils/database.js";
import { log } from "../utils/logger.js";
// run when the bot is added to a guild
module.exports = async (client, cluster, worker, ipc, guild) => {
logger.log(`[GUILD JOIN] ${guild.name} (${guild.id}) added the bot.`);
export default async (client, cluster, worker, ipc, guild) => {
log(`[GUILD JOIN] ${guild.name} (${guild.id}) added the bot.`);
const guildDB = await db.getGuild(guild.id);
if (!guildDB) await db.addGuild(guild);
};

View file

@ -1,6 +1,6 @@
const logger = require("../utils/logger.js");
import { log } from "../utils/logger.js";
// run when the bot is removed from a guild
module.exports = async (client, cluster, worker, ipc, guild) => {
logger.log(`[GUILD LEAVE] ${guild.name} (${guild.id}) removed the bot.`);
export default async (client, cluster, worker, ipc, guild) => {
log(`[GUILD LEAVE] ${guild.name} (${guild.id}) removed the bot.`);
};

View file

@ -1,13 +1,12 @@
const fs = require("fs");
const fetch = require("node-fetch");
const database = require("../utils/database.js");
const logger = require("../utils/logger.js");
const collections = require("../utils/collections.js");
const parseCommand = require("../utils/parseCommand.js");
const { clean } = require("../utils/misc.js");
import { promises } from "fs";
import database from "../utils/database.js";
import { log, error as _error } from "../utils/logger.js";
import { prefixCache, aliases, disabledCache, disabledCmdCache, commands } from "../utils/collections.js";
import parseCommand from "../utils/parseCommand.js";
import { clean } from "../utils/misc.js";
// run when someone sends a message
module.exports = async (client, cluster, worker, ipc, message) => {
export default async (client, cluster, worker, ipc, message) => {
// ignore other bots
if (message.author.bot) return;
@ -17,7 +16,7 @@ module.exports = async (client, cluster, worker, ipc, message) => {
let prefixCandidate;
let guildDB;
if (message.channel.guild) {
const cachedPrefix = collections.prefixCache.get(message.channel.guild.id);
const cachedPrefix = prefixCache.get(message.channel.guild.id);
if (cachedPrefix) {
prefixCandidate = cachedPrefix;
} else {
@ -26,7 +25,7 @@ module.exports = async (client, cluster, worker, ipc, message) => {
guildDB = await database.fixGuild(message.channel.guild);
}
prefixCandidate = guildDB.prefix;
collections.prefixCache.set(message.channel.guild.id, guildDB.prefix);
prefixCache.set(message.channel.guild.id, guildDB.prefix);
}
}
@ -58,35 +57,35 @@ module.exports = async (client, cluster, worker, ipc, message) => {
preArgs.shift();
const command = rawContent.split(/\s+/g).shift().toLowerCase();
const parsed = parseCommand(preArgs);
const aliased = collections.aliases.get(command);
const aliased = aliases.get(command);
// don't run if message is in a disabled channel
if (message.channel.guild) {
const disabled = collections.disabledCache.get(message.channel.guild.id);
const disabled = disabledCache.get(message.channel.guild.id);
if (disabled) {
if (disabled.includes(message.channel.id) && command != "channel") return;
} else {
guildDB = await database.getGuild(message.channel.guild.id);
collections.disabledCache.set(message.channel.guild.id, guildDB.disabled);
disabledCache.set(message.channel.guild.id, guildDB.disabled);
if (guildDB.disabled.includes(message.channel.id) && command !== "channel") return;
}
const disabledCmds = collections.disabledCmdCache.get(message.channel.guild.id);
const disabledCmds = disabledCmdCache.get(message.channel.guild.id);
if (disabledCmds) {
if (disabledCmds.includes(aliased ? aliased : command)) return;
} else {
guildDB = await database.getGuild(message.channel.guild.id);
collections.disabledCmdCache.set(message.channel.guild.id, guildDB.disabled_commands ? guildDB.disabled_commands : guildDB.disabledCommands);
disabledCmdCache.set(message.channel.guild.id, guildDB.disabled_commands ? guildDB.disabled_commands : guildDB.disabledCommands);
if ((guildDB.disabled_commands ? guildDB.disabled_commands : guildDB.disabledCommands).includes(aliased ? aliased : command)) return;
}
}
// check if command exists and if it's enabled
const cmd = aliased ? collections.commands.get(aliased) : collections.commands.get(command);
const cmd = aliased ? commands.get(aliased) : commands.get(command);
if (!cmd) return;
// actually run the command
logger.log("log", `${message.author.username} (${message.author.id}) ran command ${command}`);
log("log", `${message.author.username} (${message.author.id}) ran command ${command}`);
const reference = {
messageReference: {
channelID: message.channel.id,
@ -99,7 +98,7 @@ module.exports = async (client, cluster, worker, ipc, message) => {
}
};
try {
await database.addCount(collections.aliases.has(command) ? collections.aliases.get(command) : command);
await database.addCount(aliases.has(command) ? aliases.get(command) : command);
const startTime = new Date();
// eslint-disable-next-line no-unused-vars
const commandClass = new cmd(client, cluster, worker, ipc, message, parsed._, message.content.substring(prefix.length).trim().replace(command, "").trim(), (({ _, ...o }) => o)(parsed)); // we also provide the message content as a parameter for cases where we need more accuracy
@ -116,18 +115,8 @@ module.exports = async (client, cluster, worker, ipc, message) => {
} else if (typeof result === "object" && result.file) {
if (result.file.length > 8388119 && process.env.TEMPDIR !== "") {
const filename = `${Math.random().toString(36).substring(2, 15)}.${result.name.split(".")[1]}`;
await fs.promises.writeFile(`${process.env.TEMPDIR}/${filename}`, result.file);
await promises.writeFile(`${process.env.TEMPDIR}/${filename}`, result.file);
const imageURL = `${process.env.TMP_DOMAIN == "" ? "https://tmp.projectlounge.pw" : process.env.TMP_DOMAIN}/${filename}`;
const controller = new AbortController(); // eslint-disable-line no-undef
const timeout = setTimeout(() => {
controller.abort();
}, 5000);
try {
await fetch(imageURL, { signal: controller.signal });
clearTimeout(timeout);
} catch {
// this is here to make sure the image is properly cached by discord
}
await client.createMessage(message.channel.id, Object.assign({
embed: {
color: 16711680,
@ -161,7 +150,7 @@ module.exports = async (client, cluster, worker, ipc, message) => {
content: "The request timed out before I could download that image. Try uploading your image somewhere else or reducing its size."
}, reference));
} else {
logger.error(`Error occurred with command message ${message.cleanContent}: ${error.toString()}`);
_error(`Error occurred with command message ${message.cleanContent}: ${error.toString()}`);
try {
await client.createMessage(message.channel.id, Object.assign({
content: "Uh oh! I ran into an error while running this command. Please report the content of the attached file at the following link or on the esmBot Support server: <https://github.com/esmBot/esmBot/issues>"

View file

@ -1,17 +1,17 @@
const player = require("../utils/soundplayer.js");
import { manager } from "../utils/soundplayer.js";
// run when a raw packet is sent, used for sending data to lavalink
module.exports = async (client, cluster, worker, ipc, packet) => {
if (!player.manager) return;
export default async (client, cluster, worker, ipc, packet) => {
if (!manager) return;
switch (packet.t) {
case "VOICE_SERVER_UPDATE":
await player.manager.voiceServerUpdate(packet.d);
await manager.voiceServerUpdate(packet.d);
break;
case "VOICE_STATE_UPDATE":
await player.manager.voiceStateUpdate(packet.d);
await manager.voiceStateUpdate(packet.d);
break;
case "GUILD_CREATE":
for (const state of packet.d.voice_states) await player.manager.voiceStateUpdate({ ...state, guild_id: packet.d.id });
for (const state of packet.d.voice_states) await manager.voiceStateUpdate({ ...state, guild_id: packet.d.id });
break;
}
};

View file

@ -1,25 +1,25 @@
const soundPlayer = require("../utils/soundplayer.js");
const AwaitRejoin = require("../utils/awaitrejoin.js");
const { random } = require("../utils/misc.js");
import { players, manager, queues } from "../utils/soundplayer.js";
import AwaitRejoin from "../utils/awaitrejoin.js";
import { random } from "../utils/misc.js";
module.exports = async (client, cluster, worker, ipc, member, oldChannel) => {
export default async (client, cluster, worker, ipc, member, oldChannel) => {
if (!oldChannel) return;
const connection = soundPlayer.players.get(oldChannel.guild.id);
const connection = players.get(oldChannel.guild.id);
if (connection && connection.type === "music" && oldChannel.id === connection.voiceChannel.id) {
if (oldChannel.voiceMembers.filter((i) => i.id !== client.user.id).length === 0) {
const waitMessage = await client.createMessage(connection.originalChannel.id, "🔊 Waiting 10 seconds for someone to return...");
const awaitRejoin = new AwaitRejoin(oldChannel, true);
awaitRejoin.on("end", (rejoined, member) => {
if (rejoined) {
soundPlayer.players.set(connection.voiceChannel.guild.id, { player: connection.player, type: connection.type, host: member.id, voiceChannel: connection.voiceChannel, originalChannel: connection.originalChannel });
players.set(connection.voiceChannel.guild.id, { player: connection.player, type: connection.type, host: member.id, voiceChannel: connection.voiceChannel, originalChannel: connection.originalChannel });
waitMessage.edit(`🔊 ${member.mention} is the new voice channel host.`);
} else {
if (waitMessage.channel.messages.get(waitMessage.id)) waitMessage.delete();
connection.player.stop(connection.originalChannel.guild.id);
soundPlayer.manager.leave(connection.originalChannel.guild.id);
manager.leave(connection.originalChannel.guild.id);
connection.player.destroy();
soundPlayer.players.delete(connection.originalChannel.guild.id);
soundPlayer.queues.delete(connection.originalChannel.guild.id);
players.delete(connection.originalChannel.guild.id);
queues.delete(connection.originalChannel.guild.id);
client.createMessage(connection.originalChannel.id, "🔊 The current voice channel session has ended.");
}
});
@ -34,24 +34,24 @@ module.exports = async (client, cluster, worker, ipc, member, oldChannel) => {
if (members.length === 0) {
if (waitMessage.channel.messages.get(waitMessage.id)) waitMessage.delete();
connection.player.stop(connection.originalChannel.guild.id);
soundPlayer.manager.leave(connection.originalChannel.guild.id);
manager.leave(connection.originalChannel.guild.id);
connection.player.destroy();
soundPlayer.players.delete(connection.originalChannel.guild.id);
soundPlayer.queues.delete(connection.originalChannel.guild.id);
players.delete(connection.originalChannel.guild.id);
queues.delete(connection.originalChannel.guild.id);
client.createMessage(connection.originalChannel.id, "🔊 The current voice channel session has ended.");
} else {
const randomMember = random(members);
soundPlayer.players.set(connection.voiceChannel.guild.id, { player: connection.player, type: connection.type, host: randomMember.id, voiceChannel: connection.voiceChannel, originalChannel: connection.originalChannel });
players.set(connection.voiceChannel.guild.id, { player: connection.player, type: connection.type, host: randomMember.id, voiceChannel: connection.voiceChannel, originalChannel: connection.originalChannel });
waitMessage.edit(`🔊 ${randomMember.mention} is the new voice channel host.`);
}
}
});
} else if (member.id === client.user.id) {
connection.player.stop(connection.originalChannel.guild.id);
soundPlayer.manager.leave(connection.originalChannel.guild.id);
manager.leave(connection.originalChannel.guild.id);
connection.player.destroy();
soundPlayer.players.delete(connection.originalChannel.guild.id);
soundPlayer.queues.delete(connection.originalChannel.guild.id);
players.delete(connection.originalChannel.guild.id);
queues.delete(connection.originalChannel.guild.id);
await client.createMessage(connection.originalChannel.id, "🔊 The current voice channel session has ended.");
}
}

View file

@ -1,5 +1,5 @@
const leaveHandler = require("./voiceChannelLeave.js");
import leaveHandler from "./voiceChannelLeave.js";
module.exports = async (client, cluster, worker, ipc, member, newChannel, oldChannel) => {
export default async (client, cluster, worker, ipc, member, newChannel, oldChannel) => {
await leaveHandler(client, cluster, worker, ipc, member, oldChannel);
};