From 0a2070f37f03c95a8348221a3b487419e6f044e9 Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Thu, 25 Jul 2024 21:53:17 -0500 Subject: [PATCH] add basic user installed app example --- examples/userInstalledApp.js | 76 ++++++++++++++++++++++++++++++++++++ lib/rest/OAuthHelper.ts | 3 ++ lib/types/oauth.d.ts | 10 ++++- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 examples/userInstalledApp.js diff --git a/examples/userInstalledApp.js b/examples/userInstalledApp.js new file mode 100644 index 00000000..b1853fca --- /dev/null +++ b/examples/userInstalledApp.js @@ -0,0 +1,76 @@ +// The application command management functions are on ClientApplication (client.application) & client.rest.applications +// https://docs.oceanic.ws/latest/classes/ClientApplication.html +// https://docs.oceanic.ws/latest/classes/REST_Applications.html +const { ApplicationCommandTypes, ApplicationIntegrationTypes, Client, InteractionContextTypes } = require("oceanic.js"); + +// This example is a slightly modified copy of the applicationCommand example + +const client = new Client({ + auth: "Bot [TOKEN]", + gateway: { + intents: 0 // No intents are needed if you are only using interactions + } +}); + +client.on("ready", async() => { + console.log("Ready as", client.user.tag); + + // https://docs.oceanic.ws/classes/ClientApplication.html#createGlobalCommand + // Create a single command + await client.application.createGlobalCommand({ + type: ApplicationCommandTypes.CHAT_INPUT, // CHAT_INPUT = slash commands - full list: https://docs.oceanic.ws/latest/enums/Constants.ApplicationCommandTypes.html + name: "global-command", + description: "A global command.", + // in which install types this command will be usable, defaults to only GUILD_INSTALL + // https://docs.oceanic.ws/latest/enums/Constants.ApplicationIntegrationTypes.html + integrationTypes: [ApplicationIntegrationTypes.USER_INSTALL], + // in which contexts this command will be usable, defaults to all + // https://docs.oceanic.ws/latest/enums/Constants.InteractionContextTypes.html + contexts: [InteractionContextTypes.BOT_DM, InteractionContextTypes.PRIVATE_CHANNEL] + }); + + // https://docs.oceanic.ws/latest/classes/ClientApplication.html#bulkEditGlobalCommands + // Instead of deleting individual commands or creating commands one at a time, you can create them in bulk. + await client.application.bulkEditGlobalCommands([ + { + type: ApplicationCommandTypes.USER, // This will display in the `Apps` context menu, when clicking on a user. + // These commands do not have options, and cannot have a description. They will have a `target` property when received + name: "User Info", + nameLocalizations: { + "es-ES": "InformaciĆ³n del usuario" + }, + // Same as above + integrationTypes: [ApplicationIntegrationTypes.USER_INSTALL], + contexts: [InteractionContextTypes.BOT_DM, InteractionContextTypes.PRIVATE_CHANNEL] + }, + { + type: ApplicationCommandTypes.MESSAGE, // This will display in the `Apps` context menu, when clicking on a message. + // Same as above + name: "Raw Json", + nameLocalizations: { + "es-ES": "json crudo" + }, + // Same as above + integrationTypes: [ApplicationIntegrationTypes.USER_INSTALL], + contexts: [InteractionContextTypes.BOT_DM, InteractionContextTypes.PRIVATE_CHANNEL] + } + ]); + + // https://docs.oceanic.ws/latest/classes/ClientApplication.html#getGlobalCommands + // if you need to fetch your commands + const commands = await client.application.getGlobalCommands(); + console.log(commands); // An array of ApplicationCommand classes + + for (const command of commands) { + // https://docs.oceanic.ws/latest/classes/ApplicationCommand.html#delete + await command.delete(); // DON'T DO THIS! This is just an example. Use `bulkEdit` with an empty array if you want to delete all commands + } +}); + +// An error handler +client.on("error", (error) => { + console.error("Something went wrong:", error); +}); + +// Connect to Discord +client.connect(); diff --git a/lib/rest/OAuthHelper.ts b/lib/rest/OAuthHelper.ts index 0d706a50..d52027d4 100644 --- a/lib/rest/OAuthHelper.ts +++ b/lib/rest/OAuthHelper.ts @@ -63,6 +63,9 @@ export default class OAuthHelper { if (options.state) { params.push(`state=${options.state}`); } + if (options.integrationType) { + params.push(`integration_type=${options.integrationType}`); + } return `${BASE_URL}${Routes.OAUTH_AUTHORIZE}?${params.join("&")}`; } diff --git a/lib/types/oauth.d.ts b/lib/types/oauth.d.ts index 1bdf96e7..309d7d6b 100644 --- a/lib/types/oauth.d.ts +++ b/lib/types/oauth.d.ts @@ -3,7 +3,13 @@ import type { RawUser } from "./users"; import type { OAuthWebhook } from "./webhooks"; import type { RawIntegration } from "./guilds"; import type { LocaleMap, RawPartialApplication } from "./applications"; -import type { ConnectionService, PermissionName, RoleConnectionMetadataTypes, ConnectionVisibilityTypes } from "../Constants"; +import type { + ConnectionService, + PermissionName, + RoleConnectionMetadataTypes, + ConnectionVisibilityTypes, + ApplicationIntegrationTypes +} from "../Constants"; import type PartialApplication from "../structures/PartialApplication"; import type User from "../structures/User"; import type Webhook from "../structures/Webhook"; @@ -62,6 +68,8 @@ export interface OAuthURLOptions { disableGuildSelect?: boolean; /** The id of the guild to preselect. */ guildID?: string; + /** The integration (install) type. */ + integrationType?: ApplicationIntegrationTypes; /** The permissions to request. */ permissions?: string; /** `consent` to show the prompt, `none` to not show the prompt if the user has already authorized previously. */