Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added some checks on creating 247 player and on destroy reconnect #306

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/events/Lavalink/nodeConnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ module.exports = async (client, node) => {
}));

for (const obj of ModifyArray) {
AutoConnect(obj, client);
await AutoConnect(obj, client);
}
};
31 changes: 28 additions & 3 deletions src/events/Lavalink/playerDestroy.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
const { ActionRowBuilder, ButtonBuilder, EmbedBuilder, ButtonStyle } = require("discord.js");
const { Player } = require("erela.js");
const Model = require("../../schema/247");
const db = require("../../schema/setup");

const MusicBot = require("../../structures/Client");
const { AutoConnect } = require("../../utils/functions");

/**
*
* @param {MusicBot} client
* @param {Player} player
* @returns
*/
module.exports = async (client, player) => {

client.logger.log(`Player has been destroyed in ${player.guild}`, "log");

const twentyFourSeven = await Model.findOne({ Guild: player.guild, TextChannel: player.textChannel, 247: true });

if (twentyFourSeven) {
const obj = {
guild: player.guild,
voicechannel: twentyFourSeven.VoiceChannel,
textchannel: player.textChannel,
status: true,
}

await AutoConnect(obj, client);

return;
}

let guild = client.guilds.cache.get(player.guild);
if (!guild) return;
const data = await db.findOne({ Guild: guild.id });
Expand All @@ -23,8 +48,8 @@ module.exports = async (client, player) => {

if (!message) return;
let disabled = true;
if(player && player.queue && player.queue.current) disabled = false;
if (player && player.queue && player.queue.current) disabled = false;

let embed1 = new EmbedBuilder().setColor(client.embedColor).setTitle(`Nothing playing right now in this server!`).setDescription(`[Invite](${client.config.links.invite}) - [Support Server](${client.config.links.support})`).setImage(client.config.links.img);

let pausebut = new ButtonBuilder().setCustomId(`pause_but_${guild.id}`).setEmoji({ name: "⏯️" }).setStyle(ButtonStyle.Secondary).setDisabled(false);
Expand Down
68 changes: 48 additions & 20 deletions src/utils/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
ButtonStyle,
} = require("discord.js");
const { Player } = require("erela.js");
const Model = require("../schema/247");
const db = require("../schema/setup");
const MusicBot = require("../structures/Client");
const { convertTime } = require("./convert");
Expand All @@ -27,8 +28,16 @@ const { convertTime } = require("./convert");
* @returns {boolean}
*/

function AutoConnect(data, client) {
async function AutoConnect(data, client) {
const { status, guild, voicechannel, textchannel } = data;
const obtainedBool = await check(client, guild, textchannel, voicechannel);

if (!obtainedBool) {
const data = await Model.findOne({ Guild: guild, VoiceChannel: voicechannel, TextChannel: textchannel });
await data.delete();
return
}

/**
* @type {Player}
*/
Expand All @@ -47,6 +56,28 @@ function AutoConnect(data, client) {
return (player.twentyFourSeven = status);
}

/**
* @param {MusicBot} client
* @param {string} guild
* @param {string} textchannel
* @param {string} voicechannel
* @returns {boolean}
*/
async function check(client, guild, textchannel, voicechannel) {
let isGuild;
let isChannel;
const resolveGuild = await client.guilds.fetch(guild).catch(() => isGuild = false);

if (isGuild === false) return false;

await resolveGuild.channels.fetch(textchannel).catch(() => isChannel = false);
await resolveGuild.channels.fetch(voicechannel).catch(() => isChannel = false);

if (isChannel === false) return false;

return true;
}

/**
*
* @param {TextChannel} channel
Expand All @@ -61,7 +92,7 @@ async function oops(channel, args) {
embeds: [embed1],
});

setTimeout(async () => await m.delete().catch(() => {}), 12000);
setTimeout(async () => await m.delete().catch(() => { }), 12000);
} catch (e) {
return console.error(e);
}
Expand All @@ -83,8 +114,7 @@ function neb(embed, player, client) {

return embed
.setDescription(
`[${player.queue.current.title}](${
player.queue.current.uri
`[${player.queue.current.title}](${player.queue.current.uri
}) • \`[${convertTime(player.queue.current.duration)}]\``
)
.setImage(icon)
Expand Down Expand Up @@ -115,7 +145,7 @@ async function playerhandler(query, player, message) {
message: d.Message,
cache: true,
});
} catch (e) {}
} catch (e) { }

if (!message.guild.members.me.voice.channel || player.state !== "CONNECTED")
player.connect();
Expand Down Expand Up @@ -149,12 +179,12 @@ async function playerhandler(query, player, message) {
],
})
.then((a) =>
setTimeout(async () => await a.delete().catch(() => {}), 5000)
setTimeout(async () => await a.delete().catch(() => { }), 5000)
)
.catch(() => {});
.catch(() => { });

neb(n, player);
if (m) await m.edit({ embeds: [n] }).catch(() => {});
if (m) await m.edit({ embeds: [n] }).catch(() => { });
} else if (s.loadType === "SEARCH_RESULT") {
if (player.state !== "CONNECTED") player.connect();
if (player) player.queue.add(s.tracks[0]);
Expand All @@ -178,12 +208,12 @@ async function playerhandler(query, player, message) {
],
})
.then((a) =>
setTimeout(async () => await a.delete().catch(() => {}), 5000)
setTimeout(async () => await a.delete().catch(() => { }), 5000)
)
.catch(() => {});
.catch(() => { });

neb(n, player);
if (m) await m.edit({ embeds: [n], files: [] }).catch(() => {});
if (m) await m.edit({ embeds: [n], files: [] }).catch(() => { });
} else if (s.loadType === "TRACK_LOADED") {
if (player.state !== "CONNECTED") player.connect();
if (player) player.queue.add(s.tracks[0]);
Expand All @@ -207,12 +237,12 @@ async function playerhandler(query, player, message) {
],
})
.then((a) =>
setTimeout(async () => await a.delete().catch(() => {}), 5000)
setTimeout(async () => await a.delete().catch(() => { }), 5000)
)
.catch(() => {});
.catch(() => { });

neb(n, player);
if (m) await m.edit({ embeds: [n] }).catch(() => {});
if (m) await m.edit({ embeds: [n] }).catch(() => { });
} else return await oops(message.channel, `No results found for ${query}`);
}

Expand Down Expand Up @@ -308,15 +338,13 @@ async function trackStartEventHandler(msgId, channel, player, track, client) {
let embed2 = new EmbedBuilder()
.setColor(message.client.embedColor)
.setDescription(
`${emojiplay} **Started Playing** - [${track.title}](${
track.uri
`${emojiplay} **Started Playing** - [${track.title}](${track.uri
}) - \`[${convertTime(track.duration)}]\``
)
.setImage(icon)
.setFooter({
text: `Requested by ${
player.queue?.current.requester?.username ?? "Autoplay"
}`,
text: `Requested by ${player.queue?.current.requester?.username ?? "Autoplay"
}`,
iconURL: player.queue.current.requester.displayAvatarURL({
dynamic: true,
}),
Expand Down Expand Up @@ -355,7 +383,7 @@ async function buttonReply(int, args, client) {

setTimeout(async () => {
if (int && !int.ephemeral) {
await int.deleteReply().catch(() => {});
await int.deleteReply().catch(() => { });
}
}, 2000);
}
Expand Down