Fixed some issues with the sound player, search images in original message first, add max limit to dice

This commit is contained in:
TheEssem 2020-08-16 11:48:37 -05:00
parent a82341ea4f
commit 95846d32d4
5 changed files with 55 additions and 70 deletions

View file

@ -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;
}
}
};

View file

@ -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);