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

Feat(CommandCheck): Added check for same VC #32

Merged
merged 1 commit into from
Aug 30, 2021
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
9 changes: 8 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ abstract class SlashCommand {
* @default false
*/
public readonly needsVoiceChannel: boolean;
/**
* If the command needs the user to be in the same voice channel as the bot
* @default false
*/
public readonly needsSameVoiceChannel: boolean;
/**
* If the command is a global command
*
Expand All @@ -54,13 +59,15 @@ abstract class SlashCommand {
defer = true,
ephemeral = true,
guildOnly = false,
needsVoiceChannel = false
needsVoiceChannel = false,
needsSameVoiceChannel = false
} = {}){
this.data = data;
this.defer = defer;
this.ephemeral = ephemeral;
this.guildOnly = guildOnly;
this.needsVoiceChannel = needsVoiceChannel;
this.needsSameVoiceChannel = needsSameVoiceChannel;
}

/**
Expand Down
14 changes: 2 additions & 12 deletions src/commands/disconnect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SlashCommandBuilder } from "@discordjs/builders";
import { CommandInteraction, GuildMember, MessageEmbed } from "discord.js";
import { CommandInteraction, MessageEmbed } from "discord.js";
import { CustomClient } from "../client";
import { SlashCommand } from "../command";

Expand All @@ -11,7 +11,7 @@ module.exports = class extends SlashCommand {
{
ephemeral: false,
guildOnly: true,
needsVoiceChannel: true
needsSameVoiceChannel: true
});
}

Expand All @@ -20,16 +20,6 @@ module.exports = class extends SlashCommand {
{guild} = interaction,
{channel} = guild.me.voice;

if(!(interaction.member as GuildMember).voice.channel.equals(channel)) return interaction.editReply({
embeds: [
new MessageEmbed({
color: "RED",
title: "Can't disconnect",
description: "You need to be in the same voice channel to disconnect"
})
]
});

client.player.deleteQueue(guild);

interaction.editReply({
Expand Down
15 changes: 2 additions & 13 deletions src/commands/nowPlaying.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SlashCommandBuilder } from "@discordjs/builders";
import { stripIndents } from "common-tags";
import { CommandInteraction, GuildMember, MessageEmbed } from "discord.js";
import { CommandInteraction, MessageEmbed } from "discord.js";
import { CustomClient } from "../client";
import { SlashCommand } from "../command";

Expand All @@ -11,7 +11,7 @@ module.exports = class extends SlashCommand {
.setDescription("show the current playing song"),
{
guildOnly: true,
needsVoiceChannel: true
needsSameVoiceChannel: true
});
}

Expand All @@ -30,17 +30,6 @@ module.exports = class extends SlashCommand {
]
});

if(!(interaction.member as GuildMember).voice.channel.equals(interaction.guild.me.voice.channel))
return interaction.editReply({
embeds: [
new MessageEmbed({
color: "RED",
title: "Not in voice channel",
description: "You need to be in the same voice channel to show the current song"
})
]
});

interaction.editReply({
embeds: [
new MessageEmbed({
Expand Down
38 changes: 35 additions & 3 deletions src/events/client/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CommandInteraction, GuildMember, Interaction, MessageEmbed } from "discord.js";
import { CommandInteraction, GuildMember, Interaction, MessageEmbed, MessageEmbedOptions } from "discord.js";
import { CustomClient } from "../../client";
import { SlashCommand } from "../../command";
import { CustomConsole } from "../../console";
import { ClientEvent } from "../../event";

Expand All @@ -8,6 +9,21 @@ module.exports = class extends ClientEvent {
super("interactionCreate");
}

checks: {
fn: (interaction: CommandInteraction, command: SlashCommand) => boolean;
reply: Omit<MessageEmbedOptions, "color">;
}[] = [
{
fn(interaction, command){
return command.guildOnly && !interaction.inGuild();
},
reply: {
title: "Guild Only",
description: "This command can only be used in a guild"
}
}
];

async run(interaction: Interaction){
if(!interaction.isCommand()) return;

Expand All @@ -16,7 +32,7 @@ module.exports = class extends ClientEvent {

if(!command) return;

//#region checks
//#region misc checks
if(command.guildOnly && !interaction.inGuild())
return (interaction as CommandInteraction).reply({
embeds: [
Expand All @@ -28,8 +44,12 @@ module.exports = class extends ClientEvent {
],
ephemeral: true
});
//#endregion

if(command.needsVoiceChannel && !(interaction.member as GuildMember).voice.channel)
//#region voice channel checks
const voiceChannel = (interaction.member as GuildMember).voice.channel;

if(command.needsVoiceChannel && !voiceChannel)
return interaction.reply({
embeds: [
new MessageEmbed({
Expand All @@ -40,6 +60,18 @@ module.exports = class extends ClientEvent {
],
ephemeral: true
});

if(command.needsSameVoiceChannel && !interaction.guild.me.voice.channel?.equals(voiceChannel))
return interaction.reply({
embeds: [
new MessageEmbed({
color: "RED",
title: "Not in voice channel",
description: "You need to be in the same voice channel as me to use this command"
})
],
ephemeral: true
});
//#endregion

if(command.defer) await interaction.deferReply({ephemeral: command.ephemeral});
Expand Down