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(preconditions): add voice channel only precondition #462

Merged
merged 5 commits into from
Jun 26, 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: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { CorePrecondition as GuildOnly } from './preconditions/GuildOnly';
import { CorePrecondition as GuildPrivateThreadOnly } from './preconditions/GuildPrivateThreadOnly';
import { CorePrecondition as GuildPublicThreadOnly } from './preconditions/GuildPublicThreadOnly';
import { CorePrecondition as GuildTextOnly } from './preconditions/GuildTextOnly';
import { CorePrecondition as GuildVoiceOnly } from './preconditions/GuildVoiceOnly';
import { CorePrecondition as GuildThreadOnly } from './preconditions/GuildThreadOnly';
import { CorePrecondition as NSFW } from './preconditions/NSFW';
import { CorePrecondition as UserPermissions } from './preconditions/UserPermissions';
Expand Down Expand Up @@ -92,6 +93,7 @@ export const CorePreconditions = {
GuildPrivateThreadOnly,
GuildPublicThreadOnly,
GuildTextOnly,
GuildVoiceOnly,
GuildThreadOnly,
NSFW,
UserPermissions
Expand Down
1 change: 1 addition & 0 deletions src/lib/errors/Identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const enum Identifiers {
PreconditionGuildPrivateThreadOnly = 'preconditionGuildPrivateThreadOnly',
PreconditionGuildPublicThreadOnly = 'preconditionGuildPublicThreadOnly',
PreconditionGuildTextOnly = 'preconditionGuildTextOnly',
PreconditionGuildVoiceOnly = 'preconditionGuildVoiceOnly',
PreconditionNSFW = 'preconditionNsfw',
PreconditionClientPermissions = 'preconditionClientPermissions',
PreconditionClientPermissionsNoClient = 'preconditionClientPermissionsNoClient',
Expand Down
12 changes: 11 additions & 1 deletion src/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
return CommandPreConditions.DirectMessageOnly;
case 'GUILD_TEXT':
return CommandPreConditions.GuildTextOnly;
case 'GUILD_VOICE':
return CommandPreConditions.GuildVoiceOnly;
case 'GUILD_NEWS':
return CommandPreConditions.GuildNewsOnly;
case 'GUILD_NEWS_THREAD':
Expand Down Expand Up @@ -464,8 +466,9 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.

const dm = keys.has('DM');
const guildText = keys.has('GUILD_TEXT');
const guildVoice = keys.has('GUILD_VOICE');
const guildNews = keys.has('GUILD_NEWS');
const guild = guildText && guildNews;
const guild = guildText && guildNews && guildVoice;

// If runs everywhere, optimise to null:
if (dm && guild) return null;
Expand Down Expand Up @@ -499,6 +502,10 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
} else if (guildNewsThread) {
preconditions.append(CommandPreConditions.GuildNewsThreadOnly);
}

if (guildVoice) {
preconditions.append(CommandPreConditions.GuildVoiceOnly);
}
}

return preconditions;
Expand Down Expand Up @@ -559,6 +566,7 @@ export namespace AutocompleteCommand {
export type CommandOptionsRunType =
| 'DM'
| 'GUILD_TEXT'
| 'GUILD_VOICE'
| 'GUILD_NEWS'
| 'GUILD_NEWS_THREAD'
| 'GUILD_PUBLIC_THREAD'
Expand All @@ -572,6 +580,7 @@ export type CommandOptionsRunType =
export const enum CommandOptionsRunTypeEnum {
Dm = 'DM',
GuildText = 'GUILD_TEXT',
GuildVoice = 'GUILD_VOICE',
GuildNews = 'GUILD_NEWS',
GuildNewsThread = 'GUILD_NEWS_THREAD',
GuildPublicThread = 'GUILD_PUBLIC_THREAD',
Expand All @@ -592,6 +601,7 @@ export const enum CommandPreConditions {
GuildPrivateThreadOnly = 'GuildPrivateThreadOnly',
GuildPublicThreadOnly = 'GuildPublicThreadOnly',
GuildTextOnly = 'GuildTextOnly',
GuildVoiceOnly = 'GuildVoiceOnly',
GuildThreadOnly = 'GuildThreadOnly',
NotSafeForWork = 'NSFW',
ClientPermissions = 'ClientPermissions',
Expand Down
1 change: 1 addition & 0 deletions src/lib/structures/Precondition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export interface Preconditions {
GuildPrivateThreadOnly: never;
GuildPublicThreadOnly: never;
GuildTextOnly: never;
GuildVoiceOnly: never;
GuildThreadOnly: never;
NSFW: never;
ClientPermissions: {
Expand Down
37 changes: 37 additions & 0 deletions src/preconditions/GuildVoiceOnly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { CommandInteraction, ContextMenuInteraction, Message } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { AllFlowsPrecondition } from '../lib/structures/Precondition';
import { isVoiceChannel } from '@sapphire/discord.js-utilities';

export class CorePrecondition extends AllFlowsPrecondition {
public messageRun(message: Message) {
return isVoiceChannel(message.channel)
? this.ok()
: this.error({
identifier: Identifiers.PreconditionGuildVoiceOnly,
message: 'You can only run this message command in server voice channels.'
});
}

public async chatInputRun(interaction: CommandInteraction) {
const channel = await this.fetchChannelFromInteraction(interaction);

return isVoiceChannel(channel)
? this.ok()
: this.error({
identifier: Identifiers.PreconditionGuildVoiceOnly,
message: 'You can only run this chat input command in server voice channels.'
});
}

public async contextMenuRun(interaction: ContextMenuInteraction) {
const channel = await this.fetchChannelFromInteraction(interaction);

return isVoiceChannel(channel)
? this.ok()
: this.error({
identifier: Identifiers.PreconditionGuildVoiceOnly,
message: 'You can only run this context menu command in server voice channels.'
});
}
}