Skip to content

Commit

Permalink
Util function for checking voice channel + format code
Browse files Browse the repository at this point in the history
Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com>
  • Loading branch information
TannerGabriel committed Jul 18, 2023
1 parent 8fd0ad9 commit 6eb556b
Show file tree
Hide file tree
Showing 20 changed files with 573 additions and 699 deletions.
14 changes: 7 additions & 7 deletions client/Client.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const {Client, Collection, GatewayIntentBits, Partials} = require('discord.js');

module.exports = class extends Client {
constructor(config) {
super({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates]
});
constructor(config) {
super({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates]
});

this.commands = new Collection();
this.commands = new Collection();

this.config = config;
}
this.config = config;
}
};
70 changes: 35 additions & 35 deletions commands/ban.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
const {ApplicationCommandOptionType } = require('discord.js');
const {ApplicationCommandOptionType} = require('discord.js');

module.exports = {
name: 'ban',
description: 'Ban a player',
options: [
{
name: 'user',
type: ApplicationCommandOptionType.User,
description: 'The user you want to ban',
required: true,
},
],
execute(interaction, client) {
const member = interaction.options.getUser('user');
name: 'ban',
description: 'Ban a player',
options: [
{
name: 'user',
type: ApplicationCommandOptionType.User,
description: 'The user you want to ban',
required: true,
},
],
execute(interaction, client) {
const member = interaction.options.getUser('user');

if (!member) {
return interaction.reply('You need to mention the member you want to ban him');
}
if (!member) {
return interaction.reply('You need to mention the member you want to ban him');
}

if (!interaction.member.permissions.has('BAN_MEMBERS')) {
return interaction.reply("I can't ban this user.");
}
if (!interaction.member.permissions.has('BAN_MEMBERS')) {
return interaction.reply("I can't ban this user.");
}

const userinfo = client.users.cache.getMember(member);
const userinfo = client.users.cache.getMember(member);

return interaction.guild.members
.ban(member)
.then(() => {
interaction.reply({
content: `${userinfo.username} was banned.`,
ephemeral: true,
});
})
.catch(error =>
interaction.reply({
content: `Sorry, an error occured.`,
ephemeral: true,
}),
);
},
return interaction.guild.members
.ban(member)
.then(() => {
interaction.reply({
content: `${userinfo.username} was banned.`,
ephemeral: true,
});
})
.catch(error =>
interaction.reply({
content: `Sorry, an error occured.`,
ephemeral: true,
}),
);
},
};
28 changes: 14 additions & 14 deletions commands/help.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const fs = require('fs');

module.exports = {
name: 'help',
description: 'List all available commands.',
execute(interaction) {
let str = '';
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
name: 'help',
description: 'List all available commands.',
execute(interaction) {
let str = '';
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
const command = require(`./${file}`);
str += `Name: ${command.name}, Description: ${command.description} \n`;
}
for (const file of commandFiles) {
const command = require(`./${file}`);
str += `Name: ${command.name}, Description: ${command.description} \n`;
}

return void interaction.reply({
content: str,
ephemeral: true,
});
},
return void interaction.reply({
content: str,
ephemeral: true,
});
},
};
113 changes: 51 additions & 62 deletions commands/loop.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,62 @@
const {GuildMember, ApplicationCommandOptionType} = require('discord.js');
const {QueueRepeatMode, useQueue} = require('discord-player');
const {isInVoiceChannel} = require("../utils/voicechannel");

module.exports = {
name: 'loop',
description: 'Sets loop mode',
options: [
{
name: 'mode',
type: ApplicationCommandOptionType.Integer,
description: 'Loop type',
required: true,
choices: [
name: 'loop',
description: 'Sets loop mode',
options: [
{
name: 'Off',
value: QueueRepeatMode.OFF,
name: 'mode',
type: ApplicationCommandOptionType.Integer,
description: 'Loop type',
required: true,
choices: [
{
name: 'Off',
value: QueueRepeatMode.OFF,
},
{
name: 'Track',
value: QueueRepeatMode.TRACK,
},
{
name: 'Queue',
value: QueueRepeatMode.QUEUE,
},
{
name: 'Autoplay',
value: QueueRepeatMode.AUTOPLAY,
},
],
},
{
name: 'Track',
value: QueueRepeatMode.TRACK,
},
{
name: 'Queue',
value: QueueRepeatMode.QUEUE,
},
{
name: 'Autoplay',
value: QueueRepeatMode.AUTOPLAY,
},
],
},
],
async execute(interaction) {
try {
if (!(interaction.member instanceof GuildMember) || !interaction.member.voice.channel) {
return void interaction.reply({
content: 'You are not in a voice channel!',
ephemeral: true,
});
}
],
async execute(interaction) {
try {
const inVoiceChannel = isInVoiceChannel(interaction)
if (!inVoiceChannel) {
return
}

if (
interaction.guild.members.me.voice.channelId &&
interaction.member.voice.channelId !== interaction.guild.members.me.voice.channelId
) {
return void interaction.reply({
content: 'You are not in my voice channel!',
ephemeral: true,
});
}
await interaction.deferReply();

await interaction.deferReply();
const queue = useQueue(interaction.guild.id)
if (!queue || !queue.currentTrack) {
return void interaction.followUp({content: '❌ | No music is being played!'});
}

const queue = useQueue(interaction.guild.id)
if (!queue || !queue.currentTrack) {
return void interaction.followUp({content: '❌ | No music is being played!'});
}
const loopMode = interaction.options.getInteger('mode');
queue.setRepeatMode(loopMode);
const mode = loopMode === QueueRepeatMode.TRACK ? '🔂' : loopMode === QueueRepeatMode.QUEUE ? '🔁' : '▶';

const loopMode = interaction.options.getInteger('mode');
queue.setRepeatMode(loopMode);
const mode = loopMode === QueueRepeatMode.TRACK ? '🔂' : loopMode === QueueRepeatMode.QUEUE ? '🔁' : '▶';

return void interaction.followUp({
content: `${mode} | Updated loop mode!`,
});
} catch (error) {
console.log(error);
interaction.followUp({
content: 'There was an error trying to execute that command: ' + error.message,
});
}
},
return void interaction.followUp({
content: `${mode} | Updated loop mode!`,
});
} catch (error) {
console.log(error);
interaction.followUp({
content: 'There was an error trying to execute that command: ' + error.message,
});
}
},
};
97 changes: 43 additions & 54 deletions commands/move.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,52 @@
const {GuildMember, ApplicationCommandOptionType } = require('discord.js');
const {GuildMember, ApplicationCommandOptionType} = require('discord.js');
const {useQueue} = require("discord-player");
const {isInVoiceChannel} = require("../utils/voicechannel");

module.exports = {
name: 'move',
description: 'move song position in the queue!',
options: [
{
name: 'track',
type: ApplicationCommandOptionType.Integer,
description: 'The track number you want to move',
required: true,
},
{
name: 'position',
type: ApplicationCommandOptionType.Integer,
description: 'The position to move it to',
required: true,
},
],
async execute(interaction) {
if (!(interaction.member instanceof GuildMember) || !interaction.member.voice.channel) {
return void interaction.reply({
content: 'You are not in a voice channel!',
ephemeral: true,
});
}
name: 'move',
description: 'move song position in the queue!',
options: [
{
name: 'track',
type: ApplicationCommandOptionType.Integer,
description: 'The track number you want to move',
required: true,
},
{
name: 'position',
type: ApplicationCommandOptionType.Integer,
description: 'The position to move it to',
required: true,
},
],
async execute(interaction) {
const inVoiceChannel = isInVoiceChannel(interaction)
if (!inVoiceChannel) {
return
}

if (
interaction.guild.members.me.voice.channelId &&
interaction.member.voice.channelId !== interaction.guild.members.me.voice.channelId
) {
return void interaction.reply({
content: 'You are not in my voice channel!',
ephemeral: true,
});
}
await interaction.deferReply();
const queue = useQueue(interaction.guild.id)

await interaction.deferReply();
const queue = useQueue(interaction.guild.id)
if (!queue || !queue.currentTrack)
return void interaction.followUp({content: '❌ | No music is being played!'});

if (!queue || !queue.currentTrack)
return void interaction.followUp({content: '❌ | No music is being played!'});
const queueNumbers = [interaction.options.getInteger('track') - 1, interaction.options.getInteger('position') - 1];

const queueNumbers = [interaction.options.getInteger('track') - 1, interaction.options.getInteger('position') - 1];
if (queueNumbers[0] > queue.tracks.size || queueNumbers[1] > queue.tracks.size)
return void interaction.followUp({content: '❌ | Track number greater than queue depth!'});

if (queueNumbers[0] > queue.tracks.size || queueNumbers[1] > queue.tracks.size)
return void interaction.followUp({content: '❌ | Track number greater than queue depth!'});

try {
const track = queue.node.remove(queueNumbers[0]);
queue.node.insert(track, queueNumbers[1]);
return void interaction.followUp({
content: `✅ | Moved **${track}**!`,
});
} catch (error) {
console.log(error);
return void interaction.followUp({
content: '❌ | Something went wrong!',
});
}
},
try {
const track = queue.node.remove(queueNumbers[0]);
queue.node.insert(track, queueNumbers[1]);
return void interaction.followUp({
content: `✅ | Moved **${track}**!`,
});
} catch (error) {
console.log(error);
return void interaction.followUp({
content: '❌ | Something went wrong!',
});
}
},
};
Loading

0 comments on commit 6eb556b

Please sign in to comment.