From 95846d32d49a1b1b829807f80ffa075149b877de Mon Sep 17 00:00:00 2001 From: TheEssem Date: Sun, 16 Aug 2020 11:48:37 -0500 Subject: [PATCH] Fixed some issues with the sound player, search images in original message first, add max limit to dice --- commands/dice.js | 2 +- events/ready.js | 18 --------- events/voiceChannelLeave.js | 4 +- utils/imagedetect.js | 73 +++++++++++++++++++------------------ utils/soundplayer.js | 28 +++++++------- 5 files changed, 55 insertions(+), 70 deletions(-) diff --git a/commands/dice.js b/commands/dice.js index d039506..31cbef3 100644 --- a/commands/dice.js +++ b/commands/dice.js @@ -1,7 +1,7 @@ const misc = require("../utils/misc.js"); exports.run = async (message, args) => { - if (args.length === 0 || !args[0].match(/^\d+$/)) { + if (args.length === 0 || !args[0].match(/^\d+$/) || args[0] > 1000000) { return `🎲 The dice landed on ${misc.random([...Array(6).keys()]) + 1}.`; } else { return `🎲 The dice landed on ${misc.random([...Array(parseInt(args[0])).keys()]) + 1}.`; diff --git a/events/ready.js b/events/ready.js index f9836f6..18b976d 100644 --- a/events/ready.js +++ b/events/ready.js @@ -1,4 +1,3 @@ -const cron = require("cron"); const client = require("../utils/client.js"); const database = require("../utils/database.js"); const collections = require("../utils/collections.js"); @@ -11,7 +10,6 @@ const helpGenerator = const twitter = process.env.TWITTER === "true" ? require("../utils/twitter.js") : null; const first = process.env.PMTWO === "true" ? process.env.NODE_APP_INSTANCE === "0" : true; -let run = false; // run when ready module.exports = async () => { @@ -44,21 +42,6 @@ module.exports = async () => { } } - if (!run && first) { - const job = new cron.CronJob("0 0 * * 0", async () => { - logger.log("Deleting stale guild entries in database..."); - const guildDB = await database.guilds.find({}); - for (const { id } of guildDB) { - if (!client.guilds.get(id)) { - await database.guilds.deleteMany({ id: id }); - logger.log(`Deleted entry for guild ID ${id}.`); - } - } - logger.log("Finished deleting stale entries."); - }); - job.start(); - } - const global = await database.global.findOne({}); if (!global) { const countObject = {}; @@ -146,5 +129,4 @@ module.exports = async () => { if (process.env.PMTWO === "true") process.send("ready"); logger.log(`Successfully started ${client.user.username}#${client.user.discriminator} with ${client.users.size} users in ${client.guilds.size} servers.`); - run = true; }; diff --git a/events/voiceChannelLeave.js b/events/voiceChannelLeave.js index c5de0b4..519d0f5 100644 --- a/events/voiceChannelLeave.js +++ b/events/voiceChannelLeave.js @@ -1,6 +1,7 @@ const soundPlayer = require("../utils/soundplayer.js"); const client = require("../utils/client.js"); const AwaitRejoin = require("../utils/awaitrejoin.js"); +const { random } = require("../utils/misc.js"); module.exports = async (member, oldChannel) => { const connection = soundPlayer.players.get(oldChannel.guild.id); @@ -29,11 +30,10 @@ module.exports = async (member, oldChannel) => { waitMessage.delete(); connection.player.stop(connection.originalChannel.guild.id); } else { - const randomMember = members.random(); + 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 }); waitMessage.edit(`🔊 ${randomMember.mention} is the new voice channel host.`); } - } }); } diff --git a/utils/imagedetect.js b/utils/imagedetect.js index 89f6e2d..2642f95 100644 --- a/utils/imagedetect.js +++ b/utils/imagedetect.js @@ -49,47 +49,48 @@ const typeCheck = async (image, image2, gifv = false) => { } }; +const checkImages = async (message) => { + let type; + // first check the embeds + if (message.embeds.length !== 0) { + // embeds can have 2 possible entries with images, we check the thumbnail first + if (message.embeds[0].type === "gifv") { + type = await typeCheck(message.embeds[0].video.url, message.embeds[0].video.url, true); + } else if (message.embeds[0].thumbnail) { + type = await typeCheck(message.embeds[0].thumbnail.proxy_url, message.embeds[0].thumbnail.url); + // if there isn't a thumbnail check the image area + } else if (message.embeds[0].image) { + type = await typeCheck(message.embeds[0].image.proxy_url, message.embeds[0].image.url); + } + // then check the attachments + } else if (message.attachments.length !== 0) { + // get type of file + type = await typeCheck(message.attachments[0].proxy_url, message.attachments[0].url); + // if there's nothing in the attachments check the urls in the message if there are any + } else if (urlRegex.test(message.content)) { + // get url + const url = message.content.match(urlRegex); + // get type of file + type = await typeCheck(url[0], url[0]); + } + // if the file is an image then return it + return type ? type : false; +}; + // this checks for the latest message containing an image and returns the url of the image module.exports = async (cmdMessage) => { - // we start by getting the messages + // we start by checking the current message for images + const result = await checkImages(cmdMessage); + if (result !== false) return result; + // if there aren't any then iterate over the last few messages in the channel const messages = await cmdMessage.channel.getMessages(); // iterate over each message for (const message of messages) { - // check the attachments first - if (message.embeds.length !== 0) { - // embeds can have 2 possible entries with images, we check the thumbnail first - if (message.embeds[0].type === "gifv") { - const type = await typeCheck(message.embeds[0].video.url, message.embeds[0].video.url, true); - if (type === false) continue; - return type; - } else if (message.embeds[0].thumbnail) { - const type = await typeCheck(message.embeds[0].thumbnail.proxy_url, message.embeds[0].thumbnail.url); - if (type === false) continue; - return type; - // if there isn't a thumbnail check the image area - } else if (message.embeds[0].image) { - const type = await typeCheck(message.embeds[0].image.proxy_url, message.embeds[0].image.url); - if (type === false) continue; - return type; - } - } else if (message.attachments.length !== 0) { - // get type of file - const type = await typeCheck(message.attachments[0].proxy_url, message.attachments[0].url); - // move to the next message if the file isn't an image - if (type === false) continue; - // if the file is an image then return it - return type; - // if there's nothing in the attachments check the urls in the message if there are any - } else if (urlRegex.test(message.content)) { - // get url - const url = message.content.match(urlRegex); - // get type of file - const type = await typeCheck(url[0], url[0]); - // move to the next message if the file isn't an image - if (type === false) continue; - // if the file is an image then return it - return type; - // if there's no urls then check the embeds + const result = await checkImages(message); + if (result === false) { + continue; + } else { + return result; } } }; diff --git a/utils/soundplayer.js b/utils/soundplayer.js index 074e2a0..9570ba9 100644 --- a/utils/soundplayer.js +++ b/utils/soundplayer.js @@ -70,7 +70,7 @@ exports.play = async (sound, message, music = false) => { } }; -exports.nextSong = async (message, connection, track, info, music, voiceChannel) => { +exports.nextSong = async (message, connection, track, info, music, voiceChannel, inQueue = false) => { const parts = Math.floor((0 / info.length) * 10); const playingMessage = await client.createMessage(message.channel.id, !music ? "🔊 Playing sound..." : { "embed": { @@ -99,14 +99,16 @@ exports.nextSong = async (message, connection, track, info, music, voiceChannel) }); await connection.play(track); this.players.set(voiceChannel.guild.id, { player: connection, type: music ? "music" : "sound", host: message.author.id, voiceChannel: voiceChannel, originalChannel: message.channel }); - connection.on("error", (error) => { - playingMessage.delete(); - this.manager.leave(voiceChannel.guild.id); - connection.destroy(); - this.players.delete(voiceChannel.guild.id); - queues.delete(voiceChannel.guild.id); - throw error; - }); + if (inQueue) { + connection.on("error", (error) => { + playingMessage.delete(); + this.manager.leave(voiceChannel.guild.id); + connection.destroy(); + this.players.delete(voiceChannel.guild.id); + queues.delete(voiceChannel.guild.id); + logger.error(error); + }); + } connection.once("end", async (data) => { if (data.reason === "REPLACED") return; const queue = queues.get(voiceChannel.guild.id); @@ -121,7 +123,7 @@ exports.nextSong = async (message, connection, track, info, music, voiceChannel) if (music) await client.createMessage(message.channel.id, "🔊 The current voice channel session has ended."); } else { const track = await fetch(`http://${connection.node.host}:${connection.node.port}/decodetrack?track=${encodeURIComponent(newQueue[0])}`, { headers: { Authorization: connection.node.password } }).then(res => res.json()); - this.nextSong(message, connection, newQueue[0], track, music, voiceChannel); + this.nextSong(message, connection, newQueue[0], track, music, voiceChannel, true); } }); }; @@ -143,13 +145,13 @@ exports.skip = async (message) => { if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return client.createMessage(message.channel.id, `${message.author.mention}, I'm not in a voice channel!`); const player = this.players.get(message.channel.guild.id); if (player.host !== message.author.id) { - const voteCount = skipVotes.has(message.guild.id) ? skipVotes.get(message.guild.id) : 0; + const voteCount = skipVotes.has(message.channel.guild.id) ? skipVotes.get(message.channel.guild.id) : 0; if (voteCount + 1 === 3) { player.player.stop(message.channel.guild.id); - skipVotes.set(message.guild.id, 0); + skipVotes.set(message.channel.guild.id, 0); } else { await client.createMessage(message.channel.id, `🔊 Voted to skip song (${voteCount + 1}/3 people have voted).`); - skipVotes.set(message.guild.id, voteCount + 1); + skipVotes.set(message.channel.guild.id, voteCount + 1); } } else { player.player.stop(message.channel.guild.id);