Skip to content

Commit

Permalink
add basic user installed app example
Browse files Browse the repository at this point in the history
  • Loading branch information
DonovanDMC committed Jul 26, 2024
1 parent b146b3c commit 0a2070f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
76 changes: 76 additions & 0 deletions examples/userInstalledApp.js
Original file line number Diff line number Diff line change
@@ -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();
3 changes: 3 additions & 0 deletions lib/rest/OAuthHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("&")}`;
}

Expand Down
10 changes: 9 additions & 1 deletion lib/types/oauth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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. */
Expand Down

0 comments on commit 0a2070f

Please sign in to comment.