Precondition not throwing errors.

I've setup this precondition to make sure the user is in a voice channel, when the user is in a voice channel it runs perfectly fine. But if one of the conditions is not met the error is not thrown and the command does not respond.
import { Precondition } from "@sapphire/framework";
import { CommandInteraction, GuildMember, VoiceChannel } from "discord.js";
import SailorMoon from "../classes/Client";

export class InVoicePrecondition extends Precondition {

public override async chatInputRun(interaction: CommandInteraction) {
return this.checkVoiceChannel(interaction.member as GuildMember);
}

private async checkVoiceChannel(member: GuildMember) {
const voiceChannel = member.voice.channel as VoiceChannel;
console.log(voiceChannel)
if (!voiceChannel) {
return this.error({ message: 'You need to be in a voice channel to use this command.' });
}

if (!voiceChannel.joinable || !voiceChannel.speakable) {
return this.error({ message: 'I cannot join or speak in that voice channel.' });
}

const client = member.client as SailorMoon;
const player = client.lavalink.players.get(member.guild.id);
if (player && !player.paused && voiceChannel.id !== member.guild.members.me?.voice.channel?.id) {
return this.error({ message: 'You need to be in my voice channel.' });
}

return this.ok();
}
}

declare module '@sapphire/framework' {
interface Preconditions {
InVoice: never;
}
}
import { Precondition } from "@sapphire/framework";
import { CommandInteraction, GuildMember, VoiceChannel } from "discord.js";
import SailorMoon from "../classes/Client";

export class InVoicePrecondition extends Precondition {

public override async chatInputRun(interaction: CommandInteraction) {
return this.checkVoiceChannel(interaction.member as GuildMember);
}

private async checkVoiceChannel(member: GuildMember) {
const voiceChannel = member.voice.channel as VoiceChannel;
console.log(voiceChannel)
if (!voiceChannel) {
return this.error({ message: 'You need to be in a voice channel to use this command.' });
}

if (!voiceChannel.joinable || !voiceChannel.speakable) {
return this.error({ message: 'I cannot join or speak in that voice channel.' });
}

const client = member.client as SailorMoon;
const player = client.lavalink.players.get(member.guild.id);
if (player && !player.paused && voiceChannel.id !== member.guild.members.me?.voice.channel?.id) {
return this.error({ message: 'You need to be in my voice channel.' });
}

return this.ok();
}
}

declare module '@sapphire/framework' {
interface Preconditions {
InVoice: never;
}
}
Solution:
Sapphire Framework
Reporting precondition failure | Sapphire
When a precondition fails, it's usually important for the user to know why. For example, if they hit a cooldown or lack
Jump to solution
3 Replies
Solution
Favna
Favna6mo ago
Sapphire Framework
Reporting precondition failure | Sapphire
When a precondition fails, it's usually important for the user to know why. For example, if they hit a cooldown or lack
Jonny
JonnyOP6mo ago
Oh duh I forgot to implement that. Thanks.
Jonny
JonnyOP6mo ago
Slight problem, I added the listener that was on that page and its still not returning the error. src/preconditions/InVoice.ts
import { Precondition } from "@sapphire/framework";
import { CommandInteraction, GuildMember, VoiceChannel } from "discord.js";
import SailorMoon from "../classes/Client";

export class InVoicePrecondition extends Precondition {

public override async chatInputRun(interaction: CommandInteraction) {
const client = this.container.client as SailorMoon;
return this.checkVoiceChannel(interaction.member as GuildMember, client);
}

private async checkVoiceChannel(member: GuildMember, client: SailorMoon) {
const voiceChannel = member.voice.channel as VoiceChannel;
const player = client.lavalink.players.get(member.guild.id);

if (!voiceChannel) {
return this.error({ message: 'You need to be in a voice channel to use this command.' });
} else if (!voiceChannel.joinable || !voiceChannel.speakable) {
return this.error({ message: 'I cannot join or speak in that voice channel.' });
} else if (player && !player.paused && voiceChannel.id !== member.guild.members.me?.voice.channel?.id) {
return this.error({ message: 'You need to be in my voice channel.' });
} else return this.ok();
}
}

declare module '@sapphire/framework' {
interface Preconditions {
InVoice: never;
}
}
import { Precondition } from "@sapphire/framework";
import { CommandInteraction, GuildMember, VoiceChannel } from "discord.js";
import SailorMoon from "../classes/Client";

export class InVoicePrecondition extends Precondition {

public override async chatInputRun(interaction: CommandInteraction) {
const client = this.container.client as SailorMoon;
return this.checkVoiceChannel(interaction.member as GuildMember, client);
}

private async checkVoiceChannel(member: GuildMember, client: SailorMoon) {
const voiceChannel = member.voice.channel as VoiceChannel;
const player = client.lavalink.players.get(member.guild.id);

if (!voiceChannel) {
return this.error({ message: 'You need to be in a voice channel to use this command.' });
} else if (!voiceChannel.joinable || !voiceChannel.speakable) {
return this.error({ message: 'I cannot join or speak in that voice channel.' });
} else if (player && !player.paused && voiceChannel.id !== member.guild.members.me?.voice.channel?.id) {
return this.error({ message: 'You need to be in my voice channel.' });
} else return this.ok();
}
}

declare module '@sapphire/framework' {
interface Preconditions {
InVoice: never;
}
}
/src/listeners/ChatInputCommandDenied.ts
import { Events, Listener, type ChatInputCommandDeniedPayload, type UserError } from '@sapphire/framework';

export class ChatInputCommandDenied extends Listener<typeof Events.ChatInputCommandDenied> {
public run(error: UserError, { interaction }: ChatInputCommandDeniedPayload) {
const isSilent = Reflect.get(Object(error.context), 'silent');

if (interaction.deferred || interaction.replied) {
return interaction.editReply({ content: isSilent ? '\u200b' : error.message });
}

return interaction.reply({ content: isSilent ? '\u200b' : error.message, ephemeral: true });
}
}
import { Events, Listener, type ChatInputCommandDeniedPayload, type UserError } from '@sapphire/framework';

export class ChatInputCommandDenied extends Listener<typeof Events.ChatInputCommandDenied> {
public run(error: UserError, { interaction }: ChatInputCommandDeniedPayload) {
const isSilent = Reflect.get(Object(error.context), 'silent');

if (interaction.deferred || interaction.replied) {
return interaction.editReply({ content: isSilent ? '\u200b' : error.message });
}

return interaction.reply({ content: isSilent ? '\u200b' : error.message, ephemeral: true });
}
}
(ignore, i figured it out.) I was setting it up wrong
Want results from more Discord servers?
Add your server