UNPKG

djskage

Version:

A Discord.js extension for utility commands

270 lines (269 loc) 12.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Mockinteraction = void 0; const discord_js_1 = require("discord.js"); const args_1 = require("./args"); class Mockinteraction { constructor(client, message, command) { const { args, userId, channelId, roleId, userIds, channelIds, roleIds, other, } = args_1.Args.parse(message.content, client, message.guild); const needed = []; command.options?.forEach((option) => needed.push({ type: option.type, name: option.name, value: null })); const final = []; let cur = 0; var full = `${command.name}`; if (command.options?.some((opt) => opt.type === discord_js_1.ApplicationCommandOptionType.Subcommand)) { const subcommandName = args[1]; const subcommand = command.options.find((opt) => opt.type === discord_js_1.ApplicationCommandOptionType.Subcommand && opt.name === subcommandName); if (!subcommand) { message.reply(`Subcommand "${subcommandName}" not found for command "${args[0]}".`); return; } needed.length = 0; subcommand.options?.forEach((option) => needed.push({ type: option.type, name: option.name, value: null })); full = `${full} > ${subcommand.name}`; final.push({ type: discord_js_1.ApplicationCommandOptionType.Subcommand, name: subcommandName, value: subcommandName, }); } let executingUser = message.author; let executingMember = message.member; if (userId) { executingUser = client.users.cache.get(userId) || executingUser; executingMember = message.guild?.members.cache.get(userId) || executingMember; } let executingChannel = message.channel; if (channelId) { executingChannel = message.guild?.channels.cache.get(channelId) || executingChannel; } needed.forEach((neededOption, index) => { if (!neededOption) return; if (neededOption.type === discord_js_1.ApplicationCommandOptionType.User && userIds.length > 0) { const userId = userIds.shift(); if (userId) { neededOption.value = client.users.cache.get(userId); } } else if (neededOption.type === discord_js_1.ApplicationCommandOptionType.Role && roleIds.length > 0) { const roleId = roleIds.shift(); if (roleId) { neededOption.value = message.guild?.roles.cache.get(roleId); } } else if (neededOption.type === discord_js_1.ApplicationCommandOptionType.Channel && channelIds.length > 0) { const channelId = channelIds.shift(); if (channelId) { neededOption.value = message.guild?.channels.cache.get(channelId); } } else if (neededOption.type === discord_js_1.ApplicationCommandOptionType.Mentionable && (userIds.length > 0 || roleIds.length > 0)) { const userId = userIds.shift(); if (userId) { neededOption.value = client.users.cache.get(userId); } else { const roleId = roleIds.shift(); if (roleId) { neededOption.value = message.guild?.roles.cache.get(roleId); } } } else if (neededOption.type === discord_js_1.ApplicationCommandOptionType.Attachment) { neededOption.value = message.attachments?.first(); } else if (neededOption.type === discord_js_1.ApplicationCommandOptionType.Boolean) { const bln = other.findIndex((arg) => arg.toLowerCase() === "true"); neededOption.value = other.splice(bln, 1)[0] === "true"; } else if (neededOption.type === discord_js_1.ApplicationCommandOptionType.Integer) { const bln = other.findIndex((arg) => !isNaN(Number(arg)) && Number.isInteger(Number(arg))); neededOption.value = parseInt(other.splice(bln, 1)[0]); } else if (neededOption.type === discord_js_1.ApplicationCommandOptionType.Number) { const bln = other.findIndex((arg) => !isNaN(Number(arg))); neededOption.value = other.splice(bln, 1)[0]; } else if (neededOption.type === discord_js_1.ApplicationCommandOptionType.String) { const bln = other.findIndex((arg) => isNaN(Number(arg))); neededOption.value = other.splice(bln, 1)[0]; } else { neededOption.value = other.shift(); } final.push(neededOption); cur++; }); this.id = message.id; this.applicationId = client.application?.id; this.type = discord_js_1.InteractionType.ApplicationCommand; this.channelId = executingChannel.id; this.guildId = message.guild?.id; this.user = executingUser; this.member = executingMember; this.options = { getString: (name) => final.find((opt) => opt.name === name && opt.type === discord_js_1.ApplicationCommandOptionType.String)?.value, getInteger: (name) => final.find((opt) => opt.name === name && opt.type === discord_js_1.ApplicationCommandOptionType.Integer)?.value, getNumber: (name) => final.find((opt) => opt.name === name && opt.type === discord_js_1.ApplicationCommandOptionType.Number)?.value, getBoolean: (name) => final.find((opt) => opt.name === name && opt.type === discord_js_1.ApplicationCommandOptionType.Boolean)?.value ?? null, getUser: (name) => final.find((opt) => opt.name === name && opt.type === discord_js_1.ApplicationCommandOptionType.User)?.value, getChannel: (name) => final.find((opt) => opt.name === name && opt.type === discord_js_1.ApplicationCommandOptionType.Channel)?.value, getRole: (name) => final.find((opt) => opt.name === name && opt.type === discord_js_1.ApplicationCommandOptionType.Role)?.value, getMentionable: (name) => final.find((opt) => opt.name === name && opt.type === discord_js_1.ApplicationCommandOptionType.Mentionable)?.value, getAttachment: (name) => final.find((opt) => opt.name === name && opt.type === discord_js_1.ApplicationCommandOptionType.Attachment)?.value, getSubcommand: (required = true) => { const sub = final.find((opt) => opt.type === discord_js_1.ApplicationCommandOptionType.Subcommand)?.value; if (required && !sub) throw new Error("Subcommand required"); return sub; }, }; this.command = command; this.commandGuildId = command.guildId; this.commandType = discord_js_1.ApplicationCommandType.ChatInput; this.commandId = command.id; this.commandName = command.name; this.fullCommandName = full; this.webhook = null; this.ephemeral = false; this.version = 1; this.locale = discord_js_1.Locale.EnglishUS; this.guildLocale = discord_js_1.Locale.EnglishUS; this.entitlements = new discord_js_1.Collection(); this.token = message.id; this.createdTimestamp = message.createdTimestamp; this.createdAt = new Date(message.createdTimestamp); this.channel = executingChannel; this.guild = message.guild; this.client = message.client; this.appPermissions = message.guild?.members.me?.permissions || new discord_js_1.PermissionsBitField(); this.memberPermissions = message.member?.permissions || new discord_js_1.PermissionsBitField(); this.replied = false; this.replmessage = null; this.context = discord_js_1.InteractionContextType.Guild; this._cacheType = null; this.deferReply = async () => { this.deferred = true; return Promise.resolve(); }; this.deferUpdate = async () => { this.deferred = true; return Promise.resolve(); }; this.reply = async (response) => { const replyMessage = await this.channel.send(response); this.replied = true; //@ts-ignore this.replmessage = replyMessage; return Promise.resolve(replyMessage); }; this.followUp = async (response) => { let d; d = await this.channel.send(response); return Promise.resolve(d); }; this.editReply = async (response) => { let d; //@ts-ignore if (this.replmessage) { //@ts-ignore d = await this.replmessage.edit(response); } else { d = await this.channel.send(response); } return Promise.resolve(d); }; this.deleteReply = async () => { let d; //@ts-ignore if (this.replmessage) { //@ts-ignore d = await this.replmessage.delete(); } return Promise.resolve(d); }; this.fetchReply = async () => { //@ts-ignore return this.replmessage || null; }; this.showModal = (modal, options) => { const btn = new discord_js_1.ButtonBuilder() .setCustomId(modal.data.custom_id || "modal_button") .setLabel("View Modal") .setStyle(discord_js_1.ButtonStyle.Secondary); const row = new discord_js_1.ActionRowBuilder().addComponents(btn); return this.reply({ embeds: [], components: [row.toJSON()], }).then((msg) => { msg .createMessageComponentCollector({ filter: (i) => i.user.id === this.user.id, time: 15000, }) .on("collect", (i) => { i.showModal(modal); }); }); }; this.awaitModalSubmit = () => { return new Promise((resolve, reject) => { const collector = this.channel.createMessageComponentCollector({ filter: (i) => i.user.id === this.user.id, time: 15000, }); collector.on("collect", (i) => { if (i.isModalSubmit()) { resolve(i); } }); collector.on("end", () => { reject(new Error("No modal submitted")); }); }); }; this.transformOption = () => false; this.sendPremiumRequired = () => false; this.authorizingIntegrationOwners = () => new Map(); this.isCommand = () => true; this.isAutocomplete = () => false; this.isButton = () => false; this.isChatInputCommand = () => true; this.isMessageComponent = () => false; this.isModalSubmit = () => false; this.isUserContextMenuCommand = () => false; this.isMessageContextMenuCommand = () => false; this.isStringSelectMenu = () => false; this.isAnySelectMenu = () => false; this.isRoleSelectMenu = () => false; this.isChannelSelectMenu = () => false; this.isMentionableSelectMenu = () => false; this.isContextMenuCommand = () => false; this.isUserSelectMenu = () => false; this.isSelectMenu = () => false; this.isRepliable = () => false; this.toJSON = () => JSON.stringify(this); this.inGuild = () => message.inGuild(); this.inRawGuild = () => client.guilds.cache.get(message.guildId) === undefined; this.inCachedGuild = () => client.guilds.cache.get(message.guildId) !== undefined; } } exports.Mockinteraction = Mockinteraction;