|
| 1 | +import { isArray, isEmpty, isNumber } from 'lodash'; |
| 2 | + |
| 3 | +import { |
| 4 | + ApplicationCommandOptionStructure, |
| 5 | + ApplicationCommandOptionTypes, |
| 6 | + ChannelTypes, |
| 7 | +} from '@src/interfaces'; |
| 8 | + |
| 9 | +import SlashCommandOption from '@common/SlashCommandOption'; |
| 10 | + |
| 11 | +/* |
| 12 | +|-------------------------------------------------------------------------- |
| 13 | +| SlashCommandBuilder::Options -> SlashCommandChannelOption |
| 14 | +|-------------------------------------------------------------------------- |
| 15 | +| |
| 16 | +| ... |
| 17 | +| |
| 18 | +*/ |
| 19 | + |
| 20 | +const allowTypes: Array<number> = [ |
| 21 | + ChannelTypes.GUILD_TEXT, |
| 22 | + ChannelTypes.DM, |
| 23 | + ChannelTypes.GUILD_VOICE, |
| 24 | + ChannelTypes.GROUP_DM, |
| 25 | + ChannelTypes.GUILD_CATEGORY, |
| 26 | + ChannelTypes.GUILD_NEWS, |
| 27 | + ChannelTypes.GUILD_STORE, |
| 28 | + ChannelTypes.GUILD_NEWS_THREAD, |
| 29 | + ChannelTypes.GUILD_PUBLIC_THREAD, |
| 30 | + ChannelTypes.GUILD_PRIVATE_THREAD, |
| 31 | + ChannelTypes.GUILD_STAGE_VOICE, |
| 32 | +]; |
| 33 | + |
| 34 | +export default class SlashCommandChannelOption extends SlashCommandOption { |
| 35 | + public override readonly type = ApplicationCommandOptionTypes.Channel; |
| 36 | + public readonly channelTypes: Array<ChannelTypes> = []; |
| 37 | + |
| 38 | + constructor() { |
| 39 | + super(ApplicationCommandOptionTypes.Channel); |
| 40 | + } |
| 41 | + |
| 42 | + public addFilterBy(value: Array<ChannelTypes>| ChannelTypes): this { |
| 43 | + let deny: boolean = true; |
| 44 | + |
| 45 | + if (isArray(value)) { |
| 46 | + deny = isEmpty(value); |
| 47 | + } else { |
| 48 | + deny = !isNumber(value); |
| 49 | + } |
| 50 | + |
| 51 | + if (deny) { |
| 52 | + throw new Error('The `value` parameter is not an array/ChannelType or is empty'); |
| 53 | + } |
| 54 | + |
| 55 | + if (isArray(value)) { |
| 56 | + for (const item of value) { |
| 57 | + if (!this.channelTypes.includes(item)) { |
| 58 | + if (!this.checkType(item)) { |
| 59 | + break; |
| 60 | + } |
| 61 | + |
| 62 | + this.channelTypes.push(item); |
| 63 | + } |
| 64 | + } |
| 65 | + } else if (!this.channelTypes.includes(value)) { |
| 66 | + if (!this.checkType(value)) { |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + this.channelTypes.push(value); |
| 71 | + } |
| 72 | + |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + public override toJSON(): ApplicationCommandOptionStructure { |
| 77 | + return { |
| 78 | + ...super.toJSON(), |
| 79 | + channel_types: this.channelTypes, |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + private checkType(type: ChannelTypes): boolean { |
| 84 | + let validate: boolean = allowTypes.includes(type); |
| 85 | + |
| 86 | + if (!validate) { |
| 87 | + throw new Error('The `value` parameter is not a ChannelType'); |
| 88 | + } |
| 89 | + |
| 90 | + return validate; |
| 91 | + } |
| 92 | +} |
0 commit comments