From 8a2d4e66699e833140906d1d03183c71f22f374b Mon Sep 17 00:00:00 2001 From: TheEssem Date: Sat, 26 Dec 2020 12:17:10 -0600 Subject: [PATCH] Fixed image API request closing prematurely, disabled music commands in production, various fixes regarding direct messages, fixed reply image checking, and removed yoda --- api/index.js | 9 ++++++--- commands/help.js | 4 ++-- commands/info.js | 2 +- commands/loop.js | 1 + commands/nowplaying.js | 1 + commands/pause.js | 1 + commands/play.js | 1 + commands/qrread.js | 4 +++- commands/queue.js | 1 + commands/skip.js | 1 + commands/stop.js | 1 + commands/yoda.js | 14 -------------- events/messageCreate.js | 34 +++++++++++++++++++--------------- utils/image.js | 14 ++++++++------ utils/imagedetect.js | 14 ++++---------- utils/soundplayer.js | 7 +++++++ 16 files changed, 57 insertions(+), 52 deletions(-) delete mode 100644 commands/yoda.js diff --git a/api/index.js b/api/index.js index d5bd25e..d6caff7 100644 --- a/api/index.js +++ b/api/index.js @@ -157,9 +157,12 @@ if (isMainThread) { log(`${job.uuid} is done`, job.threadNum); const server = net.createServer(function(socket) { - socket.write(Buffer.concat([Buffer.from(type ? type : "image/png"), Buffer.from("\n"), data])); - socket.end(); - process.exit(); + socket.write(Buffer.concat([Buffer.from(type ? type : "image/png"), Buffer.from("\n"), data]), (err) => { + if (err) console.error(err); + socket.end(() => { + process.exit(); + }); + }); }); server.listen(job.port, job.addr); // handle address in use errors diff --git a/commands/help.js b/commands/help.js index 0238f8b..740a4bd 100644 --- a/commands/help.js +++ b/commands/help.js @@ -24,7 +24,7 @@ exports.run = async (message, args) => { "name": "esmBot Help", "icon_url": client.user.avatarURL }, - "title": `${prefix}${command}`, + "title": `${message.channel.guild ? prefix : ""}${command}`, "url": "https://projectlounge.pw/esmBot/help.html", "description": command === "tags" ? "The main tags command. Check the help page for more info: https://projectlounge.pw/esmBot/help.html" : info.description, "color": 16711680, @@ -106,7 +106,7 @@ exports.run = async (message, args) => { }, "fields": [{ "name": "Prefix", - "value": prefix + "value": message.channel.guild ? prefix : "N/A" }, { "name": "Tip", "value": misc.random(tips) diff --git a/commands/info.js b/commands/info.js index 05a7151..5902f3a 100644 --- a/commands/info.js +++ b/commands/info.js @@ -16,7 +16,7 @@ exports.run = async () => { }, { "name": "📝 Credits:", - "value": "Bot by **[Essem](https://essem.space)**\nIcon by **[MintBorrow](https://mintborrow.newgrounds.com)**" + "value": "Bot by **[Essem](https://essem.space)** and **[various contributors](https://github.com/esmBot/esmBot/graphs/contributors)**\nIcon by **[MintBorrow](https://mintborrow.newgrounds.com)**" }, { "name": "💬 Total Servers:", diff --git a/commands/loop.js b/commands/loop.js index f3bcfeb..abfb8e1 100644 --- a/commands/loop.js +++ b/commands/loop.js @@ -1,6 +1,7 @@ const soundPlayer = require("../utils/soundplayer.js"); exports.run = async (message) => { + if (process.env.NODE_ENV === "production") return "Music commands are coming soon, but they aren't ready yet. Stay tuned to @esmBot_ on Twitter for updates!"; return await soundPlayer.loop(message); }; diff --git a/commands/nowplaying.js b/commands/nowplaying.js index 8f9d214..11a7b2f 100644 --- a/commands/nowplaying.js +++ b/commands/nowplaying.js @@ -1,6 +1,7 @@ const soundPlayer = require("../utils/soundplayer.js"); exports.run = async (message) => { + if (process.env.NODE_ENV === "production") return "Music commands are coming soon, but they aren't ready yet. Stay tuned to @esmBot_ on Twitter for updates!"; return await soundPlayer.playing(message); }; diff --git a/commands/pause.js b/commands/pause.js index ee216da..b749888 100644 --- a/commands/pause.js +++ b/commands/pause.js @@ -1,6 +1,7 @@ const soundPlayer = require("../utils/soundplayer.js"); exports.run = async (message) => { + if (process.env.NODE_ENV === "production") return "Music commands are coming soon, but they aren't ready yet. Stay tuned to @esmBot_ on Twitter for updates!"; return await soundPlayer.pause(message); }; diff --git a/commands/play.js b/commands/play.js index 757e438..cec4364 100644 --- a/commands/play.js +++ b/commands/play.js @@ -3,6 +3,7 @@ const urlRegex = /(?:\w+:)?\/\/(\S+)/; const searchRegex = /^(sc|yt)search:/; exports.run = async (message, args) => { + if (process.env.NODE_ENV === "production") return "Music commands are coming soon, but they aren't ready yet. Stay tuned to @esmBot_ on Twitter for updates!"; if (!args[0]) return `${message.author.mention}, you need to provide what you want to play!`; const query = args.join(" ").trim(); const search = urlRegex.test(query) ? query : (searchRegex.test(query) ? query : `ytsearch:${query}`); diff --git a/commands/qrread.js b/commands/qrread.js index 15267ff..74124e1 100644 --- a/commands/qrread.js +++ b/commands/qrread.js @@ -1,11 +1,13 @@ const jsqr = require("jsqr"); +const fetch = require("node-fetch"); const sharp = require("sharp"); exports.run = async (message) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image with a QR code to read!`; message.channel.sendTyping(); - const rawData = await sharp(image.path).ensureAlpha().raw().toBuffer({ resolveWithObject: true }); + 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); if (!qrBuffer) return `${message.author.mention}, I couldn't find a QR code!`; return `\`\`\`\n${qrBuffer.data}\n\`\`\``; diff --git a/commands/queue.js b/commands/queue.js index 6af2fc9..e72bd1a 100644 --- a/commands/queue.js +++ b/commands/queue.js @@ -1,6 +1,7 @@ const soundPlayer = require("../utils/soundplayer.js"); exports.run = async (message) => { + if (process.env.NODE_ENV === "production") return "Music commands are coming soon, but they aren't ready yet. Stay tuned to @esmBot_ on Twitter for updates!"; return await soundPlayer.queue(message); }; diff --git a/commands/skip.js b/commands/skip.js index 337e6a1..216df08 100644 --- a/commands/skip.js +++ b/commands/skip.js @@ -1,6 +1,7 @@ const soundPlayer = require("../utils/soundplayer.js"); exports.run = async (message) => { + if (process.env.NODE_ENV === "production") return "Music commands are coming soon, but they aren't ready yet. Stay tuned to @esmBot_ on Twitter for updates!"; return await soundPlayer.skip(message); }; diff --git a/commands/stop.js b/commands/stop.js index 22e2663..49228b1 100644 --- a/commands/stop.js +++ b/commands/stop.js @@ -1,6 +1,7 @@ const soundPlayer = require("../utils/soundplayer.js"); exports.run = async (message) => { + if (process.env.NODE_ENV === "production") return "Music commands are coming soon, but they aren't ready yet. Stay tuned to @esmBot_ on Twitter for updates!"; return await soundPlayer.stop(message); }; diff --git a/commands/yoda.js b/commands/yoda.js deleted file mode 100644 index d8bd2e9..0000000 --- a/commands/yoda.js +++ /dev/null @@ -1,14 +0,0 @@ -const fetch = require("node-fetch"); - -exports.run = async (message, args) => { - return `${message.author.mention}, this command is currently disabled due to various issues. We are looking for a fix.`; - /*if (args.length === 0) return `${message.author.mention}, you need to provide some text to translate to Yodish!`; - const request = await fetch(`https://yoda-api.appspot.com/api/v1/yodish?text=${encodeURIComponent(args.join(" "))}`); - const json = await request.json(); - return json.yodish;*/ -}; - -exports.aliases = ["yodish"]; -exports.category = 4; -exports.help = "Translates a message to Yodish (disabled)"; -exports.params = "[text]"; \ No newline at end of file diff --git a/events/messageCreate.js b/events/messageCreate.js index cda5c63..f5b7e81 100644 --- a/events/messageCreate.js +++ b/events/messageCreate.js @@ -24,15 +24,17 @@ module.exports = async (message) => { if (!valid) return; let prefixCandidate; - if (collections.prefixCache.has(message.channel.guild.id)) { - prefixCandidate = collections.prefixCache.get(message.channel.guild.id); - } else { - let guildDB = message.channel.guild ? await database.getGuild(message.channel.guild.id) : null; - if (message.channel.guild && !(guildDB && guildDB.disabled)) { - guildDB = await database.fixGuild(message.channel.guild); + if (message.channel.guild) { + if (collections.prefixCache.has(message.channel.guild.id)) { + prefixCandidate = collections.prefixCache.get(message.channel.guild.id); + } else { + let guildDB = message.channel.guild ? await database.getGuild(message.channel.guild.id) : null; + if (message.channel.guild && !(guildDB && guildDB.disabled)) { + guildDB = await database.fixGuild(message.channel.guild); + } + prefixCandidate = guildDB.prefix; + collections.prefixCache.set(message.channel.guild.id, guildDB.prefix); } - prefixCandidate = guildDB.prefix; - collections.prefixCache.set(message.channel.guild.id, guildDB.prefix); } // this line be like Pain. Pain. Pain. Pain. Pain. Pain. Pain. Pain. Pain. Pain. Pain. Pain. @@ -48,13 +50,15 @@ module.exports = async (message) => { const command = args.shift().toLowerCase(); // don't run if message is in a disabled channel - if (collections.disabledCache.has(message.channel.guild.id)) { - const disabled = collections.disabledCache.get(message.channel.guild.id); - if (disabled.includes(message.channel.id) && command != "channel") return; - } else if (message.channel.guild) { - const guildDB = await database.getGuild(message.channel.guild.id); - collections.disabledCache.set(message.channel.guild.id, guildDB.disabled); - if (guildDB.disabled.includes(message.channel.id) && command !== "channel") return; + if (message.channel.guild) { + if (collections.disabledCache.has(message.channel.guild.id)) { + const disabled = collections.disabledCache.get(message.channel.guild.id); + if (disabled.includes(message.channel.id) && command != "channel") return; + } else if (message.channel.guild) { + const guildDB = await database.getGuild(message.channel.guild.id); + collections.disabledCache.set(message.channel.guild.id, guildDB.disabled); + if (guildDB.disabled.includes(message.channel.id) && command !== "channel") return; + } } // check if command exists diff --git a/utils/image.js b/utils/image.js index d932231..ce8f63b 100644 --- a/utils/image.js +++ b/utils/image.js @@ -45,12 +45,14 @@ const getIdeal = () => { }); if (!serversLeft) { clearTimeout(timeout); - try { - const server = await chooseServer(idealServers); - resolve(server); - } catch (e) { - reject(e); - } + socket.close(async () => { + try { + const server = await chooseServer(idealServers); + resolve(server); + } catch (e) { + reject(e); + } + }); } } }); diff --git a/utils/imagedetect.js b/utils/imagedetect.js index 280d6f5..ed413f8 100644 --- a/utils/imagedetect.js +++ b/utils/imagedetect.js @@ -85,16 +85,10 @@ module.exports = async (cmdMessage) => { if (result !== false) return result; // if there aren't any in the current message then check if there's a reply if (cmdMessage.messageReference) { - const replyGuild = client.guilds.get(cmdMessage.messageReference.guildID); - if (replyGuild) { - const replyChannel = replyGuild.channels.get(cmdMessage.messageReference.channelID); - if (replyChannel) { - const replyMessage = replyChannel.messages.get(cmdMessage.messageReference.messageID); - if (replyMessage) { - const replyResult = await checkImages(replyMessage); - if (replyResult !== false) return replyResult; - } - } + const replyMessage = await client.getMessage(cmdMessage.messageReference.channelID, cmdMessage.messageReference.messageID); + if (replyMessage) { + const replyResult = await checkImages(replyMessage); + if (replyResult !== false) return replyResult; } } // if there aren't any replies then iterate over the last few messages in the channel diff --git a/utils/soundplayer.js b/utils/soundplayer.js index e860e31..8d6da8c 100644 --- a/utils/soundplayer.js +++ b/utils/soundplayer.js @@ -47,6 +47,7 @@ exports.connect = async () => { }; exports.play = async (sound, message, music = false) => { + if (!message.channel.guild) return `${message.author.mention}, this command only works in servers!`; if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; if (!message.channel.guild.members.get(client.user.id).permission.has("voiceConnect") || !message.channel.permissionsOf(client.user.id).has("voiceConnect")) return `${message.author.mention}, I can't join this voice channel!`; const voiceChannel = message.channel.guild.channels.get(message.member.voiceState.channelID); @@ -148,6 +149,7 @@ exports.nextSong = async (message, connection, track, info, music, voiceChannel, }; exports.stop = async (message) => { + if (!message.channel.guild) return `${message.author.mention}, this command only works in servers!`; if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return `${message.author.mention}, I'm not in a voice channel!`; if (this.players.get(message.channel.guild.id).host !== message.author.id) return `${message.author.mention}, only the current voice session host can stop the music!`; @@ -160,6 +162,7 @@ exports.stop = async (message) => { }; exports.skip = async (message) => { + if (!message.channel.guild) return `${message.author.mention}, this command only works in servers!`; if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return `${message.author.mention}, I'm not in a voice channel!`; const player = this.players.get(message.channel.guild.id); @@ -184,6 +187,7 @@ exports.skip = async (message) => { }; exports.pause = async (message) => { + if (!message.channel.guild) return `${message.author.mention}, this command only works in servers!`; if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return `${message.author.mention}, I'm not in a voice channel!`; if (this.players.get(message.channel.guild.id).host !== message.author.id) return `${message.author.mention}, only the current voice session host can pause/resume the music!`; @@ -193,6 +197,7 @@ exports.pause = async (message) => { }; exports.playing = async (message) => { + if (!message.channel.guild) return `${message.author.mention}, this command only works in servers!`; if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return `${message.author.mention}, I'm not in a voice channel!`; const player = this.players.get(message.channel.guild.id).player; @@ -227,6 +232,7 @@ exports.playing = async (message) => { }; exports.queue = async (message) => { + if (!message.channel.guild) return `${message.author.mention}, this command only works in servers!`; if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return `${message.author.mention}, I'm not in a voice channel!`; if (!message.channel.guild.members.get(client.user.id).permission.has("addReactions") && !message.channel.permissionsOf(client.user.id).has("addReactions")) return `${message.author.mention}, I don't have the \`Add Reactions\` permission!`; @@ -274,6 +280,7 @@ exports.queue = async (message) => { }; exports.loop = async (message) => { + if (!message.channel.guild) return `${message.author.mention}, this command only works in servers!`; if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return `${message.author.mention}, I'm not in a voice channel!`; if (this.players.get(message.channel.guild.id).host !== message.author.id) return `${message.author.mention}, only the current voice session host can loop the music!`;