Fixed some DM command issues, made reload and restart use IPC

This commit is contained in:
TheEssem 2021-04-30 12:31:53 -05:00
parent e0e0c9c7d4
commit 56113a1cf8
18 changed files with 54 additions and 35 deletions

View file

@ -89,31 +89,30 @@ class ImageCommand extends Command {
if (magickParams.type === "image/gif") {
status = await this.processMessage(this.message);
} else {
this.message.channel.sendTyping();
this.client.sendChannelTyping(this.message.channel.id);
}
try {
const { buffer, type } = await magick.run(magickParams).catch(e => {
throw e;
});
if (status && status.channel.messages.get(status.id)) await status.delete();
if (type === "nogif" && this.constructor.requiresGIF) return `${this.message.author.mention}, that isn't a GIF!`;
return {
file: buffer,
name: `${this.constructor.command}.${type}`
};
} catch (e) {
if (status && status.channel.messages.get(status.id)) await status.delete();
if (e.toString().includes("Not connected to image server")) return `${this.message.author.mention}, I'm still trying to connect to the image servers. Please wait a little bit.`;
throw e;
} finally {
if (status && await this.client.getMessage(status.channel.id, status.id).catch(() => undefined)) await status.delete();
collections.runningCommands.delete(this.message.author.id);
}
}
processMessage(message) {
return message.channel.createMessage(`${random(emotes) || process.env.PROCESSING_EMOJI || "<a:processing:479351417102925854>"} Processing... This might take a while`);
return this.client.createMessage(message.channel.id, `${random(emotes) || process.env.PROCESSING_EMOJI || "<a:processing:479351417102925854>"} Processing... This might take a while`);
}
static requiresImage = true;

View file

@ -3,7 +3,7 @@ const Command = require("../../classes/command.js");
class BirdCommand extends Command {
async run() {
this.message.channel.sendTyping();
this.client.sendChannelTyping(this.message.channel.id);
const imageData = await fetch("http://shibe.online/api/birds");
const json = await imageData.json();
return {

View file

@ -3,7 +3,7 @@ const Command = require("../../classes/command.js");
class CatCommand extends Command {
async run() {
this.message.channel.sendTyping();
this.client.sendChannelTyping(this.message.channel.id);
const data = await fetch("https://projectlounge.pw/cta/", { redirect: "manual" });
return {
embed: {

View file

@ -3,7 +3,7 @@ const Command = require("../../classes/command.js");
class DogCommand extends Command {
async run() {
this.message.channel.sendTyping();
this.client.sendChannelTyping(this.message.channel.id);
const imageData = await fetch("https://dog.ceo/api/breeds/image/random");
const json = await imageData.json();
return {

View file

@ -4,7 +4,7 @@ const Command = require("../../classes/command.js");
class MCCommand extends Command {
async run() {
if (this.args.length === 0) return `${this.message.author.mention}, you need to provide some text to generate a Minecraft achievement!`;
this.message.channel.sendTyping();
this.client.sendChannelTyping(this.message.channel.id);
const request = await fetch(`https://www.minecraftskinstealer.com/achievement/a.php?i=13&h=Achievement+get%21&t=${encodeURIComponent(this.args.join("+"))}`);
return {
file: await request.buffer(),

View file

@ -5,7 +5,7 @@ const Command = require("../../classes/command.js");
class RetroCommand extends Command {
async run() {
if (this.args.length === 0) return `${this.message.author.mention}, you need to provide some text to generate some retro text!`;
this.message.channel.sendTyping();
this.client.sendChannelTyping(this.message.channel.id);
let [line1, line2, line3] = this.args.join(" ").replaceAll("&", "\\&amp;").replaceAll(">", "\\&gt;").replaceAll("<", "\\&lt;").replaceAll("\"", "\\&quot;").replaceAll("'", "\\&apos;").replaceAll("%", "\\%").split(",").map(elem => elem.trim());
if (!line2 && line1.length > 15) {
const [split1, split2, split3] = wrap(line1, { width: 15, indent: "" }).split("\n");

View file

@ -3,7 +3,7 @@ const Command = require("../../classes/command.js");
class WikihowCommand extends Command {
async run() {
this.message.channel.sendTyping();
this.client.sendChannelTyping(this.message.channel.id);
const request = await fetch("https://hargrimm-wikihow-v1.p.rapidapi.com/images?count=1", {
headers: {
"X-RapidAPI-Key": process.env.MASHAPE,

View file

@ -4,7 +4,7 @@ const Command = require("../../classes/command.js");
class LengthenCommand extends Command {
async run() {
this.message.channel.sendTyping();
this.client.sendChannelTyping(this.message.channel.id);
if (this.args.length === 0 || !urlCheck(this.args[0])) return `${this.message.author.mention}, you need to provide a short URL to lengthen!`;
if (urlCheck(this.args[0])) {
const url = await fetch(encodeURI(this.args[0]), { redirect: "manual" });

View file

@ -2,7 +2,7 @@ const Command = require("../../classes/command.js");
class PingCommand extends Command {
async run() {
const pingMessage = await this.message.channel.createMessage("🏓 Ping?");
const pingMessage = await this.client.createMessage(this.message.channel.id, "🏓 Ping?");
return pingMessage.edit(`🏓 Pong!\n\`\`\`\nLatency: ${pingMessage.timestamp - this.message.timestamp}ms${this.message.channel.guild ? `\nShard Latency: ${Math.round(this.client.shards.get(this.client.guildShardMap[this.message.channel.guild.id]).latency)}ms` : ""}\n\`\`\``);
}

View file

@ -5,7 +5,7 @@ const Command = require("../../classes/command.js");
class QrCreateCommand extends Command {
async run() {
if (this.args.length === 0) return `${this.message.author.mention}, you need to provide some text to generate a QR code!`;
this.message.channel.sendTyping();
this.client.sendChannelTyping(this.message.channel.id);
const writable = new stream.PassThrough();
qrcode.toFileStream(writable, this.content, { margin: 1 });
const file = await this.streamToBuf(writable);

View file

@ -8,7 +8,7 @@ class QrReadCommand extends Command {
async run() {
const image = await require("../../utils/imagedetect.js")(this.client, this.message);
if (image === undefined) return `${this.message.author.mention}, you need to provide an image with a QR code to read!`;
this.message.channel.sendTyping();
this.client.sendChannelTyping(this.message.channel.id);
const data = await (await fetch(image.path)).buffer();
const rawData = await sharp(data).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
const qrBuffer = jsqr(rawData.data, rawData.info.width, rawData.info.height);

View file

@ -1,16 +1,23 @@
const handler = require("../../utils/handler.js");
const collections = require("../../utils/collections.js");
const Command = require("../../classes/command.js");
class ReloadCommand extends Command {
async run() {
if (this.message.author.id !== process.env.OWNER) return `${this.message.author.mention}, only the bot owner can reload commands!`;
if (this.args.length === 0) return `${this.message.author.mention}, you need to provide a command to reload!`;
const result = await handler.unload(this.args[0]);
if (result) return result;
const result2 = await handler.load(collections.paths.get(this.args[0]));
if (result2) return result2;
return `${this.message.author.mention}, the command \`${this.args[0]}\` has been reloaded.`;
// quite possibly one of the hackiest commands in the bot
run() {
return new Promise((resolve) => {
if (this.message.author.id !== process.env.OWNER) resolve(`${this.message.author.mention}, only the bot owner can reload commands!`);
if (this.args.length === 0) resolve(`${this.message.author.mention}, you need to provide a command to reload!`);
this.ipc.broadcast("reload", { cmd: this.args[0] });
this.ipc.register("reloadSuccess", () => {
this.ipc.unregister("reloadSuccess");
this.ipc.unregister("reloadFail");
resolve(`${this.message.author.mention}, the command \`${this.args[0]}\` has been reloaded.`);
});
this.ipc.register("reloadFail", (message) => {
this.ipc.unregister("reloadSuccess");
this.ipc.unregister("reloadFail");
resolve(message);
});
});
}
static description = "Reloads a command";

View file

@ -5,11 +5,11 @@ const Command = require("../../classes/command.js");
class RestartCommand extends Command {
async run() {
if (this.message.author.id !== process.env.OWNER) return `${this.message.author.mention}, only the bot owner can restart me!`;
await this.message.channel.createMessage(`${this.message.author.mention}, esmBot is restarting.`);
await this.client.createMessage(this.message.channel.id, `${this.message.author.mention}, esmBot is restarting.`);
for (const command of collections.commands) {
await handler.unload(command);
}
process.exit(1);
this.ipc.broadcast("restart");
}
static description = "Restarts me";

View file

@ -54,7 +54,7 @@ class StatsCommand extends Command {
},
{
"name": "Shard",
"value": this.client.guildShardMap[this.message.channel.guild.id],
"value": this.message.channel.guild ? this.client.guildShardMap[this.message.channel.guild.id] : "N/A",
"inline": true
}
]

View file

@ -6,7 +6,7 @@ const Command = require("../../classes/command.js");
class YouTubeCommand extends Command {
async run() {
if (this.args.length === 0) return `${this.message.author.mention}, you need to provide something to search for!`;
this.message.channel.sendTyping();
this.client.sendChannelTyping(this.message.channel.id);
const messages = [];
const request = await fetch(`https://www.googleapis.com/youtube/v3/search?part=snippet&q=${encodeURIComponent(this.args.join(" "))}&key=${process.env.GOOGLE}&maxResults=50`);
const result = await request.json();

View file

@ -106,6 +106,19 @@ class Shard extends Base {
collections.stats = message;
});
this.ipc.register("restart", () => {
process.exit(1);
});
this.ipc.register("reload", async (message) => {
const result = await handler.unload(message.cmd);
if (result) return this.ipc.broadcast("reloadFail", { result: result });
const result2 = await handler.load(collections.paths.get(message.cmd));
if (result2) return this.ipc.broadcast("reloadFail", { result: result2 });
//return this.ipc.broadcast("reloadSuccess", this.clusterID);
return this.ipc.broadcast("reloadSuccess");
});
// connect to lavalink
if (!sound.status && !sound.connected) await sound.connect(this.bot);

View file

@ -113,7 +113,7 @@ const checkImages = async (message, video) => {
module.exports = async (client, cmdMessage, video = false) => {
// we start by checking if the message is a reply to another message
if (cmdMessage.messageReference) {
const replyMessage = await client.getMessage(cmdMessage.messageReference.channelID, cmdMessage.messageReference.messageID);
const replyMessage = await client.getMessage(cmdMessage.messageReference.channelID, cmdMessage.messageReference.messageID).catch(() => undefined);
if (replyMessage) {
const replyResult = await checkImages(replyMessage, video);
if (replyResult !== false) return replyResult;
@ -123,7 +123,7 @@ module.exports = async (client, cmdMessage, video = false) => {
const result = await checkImages(cmdMessage, video);
if (result !== false) return result;
// if there aren't any replies then iterate over the last few messages in the channel
const messages = await cmdMessage.channel.getMessages();
const messages = await client.getMessages(cmdMessage.channel.id);
// iterate over each message
for (const message of messages) {
const result = await checkImages(message, video);

View file

@ -4,7 +4,7 @@ const MessageCollector = require("./awaitmessages.js");
module.exports = async (client, message, pages, timeout = 120000) => {
const manageMessages = message.channel.guild && message.channel.permissionsOf(client.user.id).has("manageMessages") ? true : false;
let page = 0;
let currentPage = await message.channel.createMessage(pages[page]);
let currentPage = await client.createMessage(message.channel.id, pages[page]);
const emojiList = ["◀", "🔢", "▶", "🗑"];
for (const emoji of emojiList) {
await currentPage.addReaction(emoji);
@ -19,13 +19,13 @@ module.exports = async (client, message, pages, timeout = 120000) => {
if (manageMessages) msg.removeReaction("◀", member.id);
break;
case "🔢":
message.channel.createMessage(`${message.author.mention}, what page do you want to jump to?`).then(askMessage => {
client.createMessage(message.channel.id, `${message.author.mention}, what page do you want to jump to?`).then(askMessage => {
const messageCollector = new MessageCollector(client, askMessage.channel, (response) => response.author.id === message.author.id && !isNaN(response.content) && Number(response.content) <= pages.length && Number(response.content) > 0, {
time: timeout,
maxMatches: 1
});
return messageCollector.on("message", async (response) => {
if (askMessage.channel.messages.get(askMessage.id)) askMessage.delete();
if (await client.getMessage(askMessage.channel.id, askMessage.id).catch(() => undefined)) askMessage.delete();
if (manageMessages) await response.delete();
page = Number(response.content) - 1;
currentPage = await currentPage.edit(pages[page]);
@ -42,7 +42,7 @@ module.exports = async (client, message, pages, timeout = 120000) => {
break;
case "🗑":
reactionCollector.emit("end");
if (currentPage.channel.messages.get(currentPage.id)) await currentPage.delete();
if (await client.getMessage(currentPage.channel.id, currentPage.id).catch(() => undefined)) await currentPage.delete();
return;
default:
break;
@ -51,7 +51,7 @@ module.exports = async (client, message, pages, timeout = 120000) => {
});
reactionCollector.once("end", async () => {
try {
await currentPage.channel.getMessage(currentPage.id);
await client.getMessage(currentPage.channel, currentPage.id);
if (manageMessages) {
await currentPage.removeReactions();
}