@ayanaware/bentocord
Version:
Bentocord is a Bento plugin designed to rapidly build fully functional Discord Bots.
55 lines • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChannelOptionResolver = void 0;
const eris_1 = require("eris");
const OptionType_1 = require("../constants/OptionType");
class ChannelOptionResolver {
constructor() {
this.option = OptionType_1.OptionType.CHANNEL;
this.convert = eris_1.Constants.ApplicationCommandOptionTypes.CHANNEL;
}
async reduce(ctx, option, channel) {
return { display: `#${channel.name}`, extra: channel.id };
}
async resolve(ctx, option, input) {
const guild = ctx.guild;
if (!guild)
return null;
const channels = ctx.guild.channels;
const channelTypes = option.channelTypes ?? [];
// filter matching channelType
const find = Array.from(channels.filter(c => this.checkChannel(input, c)).filter(c => channelTypes.length === 0 || channelTypes.includes(c.type)).values());
if (find.length > 0)
return find;
return Array.from(channels.filter(c => channelTypes.length === 0 || channelTypes.includes(c.type)).values());
}
checkChannel(input, channel) {
if (channel.id === input)
return true;
// handle mention
const id = /^<#(\d{17,19})>$/i.exec(input);
if (id && channel.id === id[1])
return true;
// handle name
input = input.replace(/^#/, '');
return channel.name.toLocaleLowerCase().includes(input.toLocaleLowerCase());
}
async help(ctx, option, data) {
if ('channelTypes' in option) {
// find channel names
const names = [];
for (const channelType of option.channelTypes) {
for (const [name, id] of Object.entries(eris_1.Constants.ChannelTypes)) {
if (id !== channelType)
continue;
names.push(name);
break;
}
}
data.set(await ctx.formatTranslation('BENTOCORD_WORD_CHANNELTYPES', {}, 'Channel Types'), names.map(n => `\`${n}\``).join(' '));
}
return data;
}
}
exports.ChannelOptionResolver = ChannelOptionResolver;
//# sourceMappingURL=ChannelOption.js.map