Skip to content

Commit

Permalink
Update discord_app version (#17)
Browse files Browse the repository at this point in the history
* wip

* move schema to main.ts file
  • Loading branch information
EthanThatOneKid authored Apr 16, 2024
1 parent 52a8bd8 commit 979aaa4
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 374 deletions.
44 changes: 0 additions & 44 deletions app_schema.ts
Original file line number Diff line number Diff line change
@@ -1,44 +0,0 @@
import * as discord from "discord-api-types";
import type { AppSchema } from "discord-app";

export const appSchema = {
chatInput: {
name: "shorter",
description: "Manage shortlinks.",
subcommands: {
add: {
description: "Add a shortlink.",
options: {
alias: {
type: discord.ApplicationCommandOptionType.String,
description: "The alias of the shortlink",
required: true,
},
destination: {
type: discord.ApplicationCommandOptionType.String,
description: "The destination of the shortlink",
required: true,
},
force: {
type: discord.ApplicationCommandOptionType.Boolean,
description: "Whether to overwrite an existing shortlink",
},
ttl: {
type: discord.ApplicationCommandOptionType.String,
description: "The time-to-live of the shortlink",
},
},
},
remove: {
description: "Remove a shortlink.",
options: {
alias: {
type: discord.ApplicationCommandOptionType.String,
description: "The alias of the shortlink",
required: true,
},
},
},
},
},
} as const satisfies AppSchema;
10 changes: 4 additions & 6 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
{
"tasks": {
"start": "deno run -A --unstable-kv main.ts",
"start": "deno run -A --unstable-kv --env main.ts",
"ngrok": "ngrok http 8080"
},
"imports": {
"discord-api-types": "https://deno.land/x/[email protected]/v10.ts",
"@discord-applications/app": "jsr:@discord-applications/app@^0.0.4",
"codemod": "https://deno.land/x/[email protected]/github/mod.ts",
"durationjs": "https://deno.land/x/[email protected]/mod.ts",
"discord-app": "https://deno.land/x/[email protected]/mod.ts",
"tweetnacl": "npm:[email protected]",
"@std/dotenv": "jsr:@std/dotenv@^0.222.1",
"shorter/": "./"
"shorter/": "./",
"tweetnacl": "npm:[email protected]"
}
}
184 changes: 22 additions & 162 deletions deno.lock

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { load } from "@std/dotenv";

await load({ export: true });

/**
* PORT is the port to listen on.
*/
Expand Down
81 changes: 0 additions & 81 deletions lib/discord/discord_api_client.ts

This file was deleted.

48 changes: 0 additions & 48 deletions lib/discord/discord_api_client_interface.ts

This file was deleted.

2 changes: 0 additions & 2 deletions lib/discord/mod.ts

This file was deleted.

98 changes: 71 additions & 27 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import * as discord from "discord-api-types";
import { Duration } from "durationjs";
import { createApp } from "discord-app";
import type {
APIInteractionResponseDeferredChannelMessageWithSource,
AppSchema,
} from "@discord-applications/app";
import {
ApplicationCommandOptionType,
createApp,
DiscordAPI,
InteractionResponseType,
MessageFlags,
} from "@discord-applications/app";
import type { ShorterOptions } from "shorter/lib/shorter/mod.ts";
import { shorter } from "shorter/lib/shorter/mod.ts";
import { DiscordAPIClient } from "shorter/lib/discord/mod.ts";
import {
addTTLMessage,
makeTTLMessageListener,
Expand All @@ -16,14 +24,59 @@ import {
GITHUB_TOKEN,
PORT,
} from "shorter/env.ts";
import { appSchema } from "./app_schema.ts";

const INVITE_URL =
`https://discord.com/api/oauth2/authorize?client_id=${DISCORD_CLIENT_ID}&scope=applications.commands`;
const APPLICATION_URL =
`https://discord.com/developers/applications/${DISCORD_CLIENT_ID}/bot`;

const discordAPI = new DiscordAPIClient();
const discordAPI = new DiscordAPI({
applicationID: DISCORD_CLIENT_ID,
token: DISCORD_TOKEN,
publicKey: DISCORD_PUBLIC_KEY,
});

export const shorterSchema = {
chatInput: {
name: "Shorter",
description: "Manage shortlinks.",
subcommands: {
add: {
description: "Add a shortlink.",
options: {
alias: {
type: ApplicationCommandOptionType.String,
description: "The alias of the shortlink",
required: true,
},
destination: {
type: ApplicationCommandOptionType.String,
description: "The destination of the shortlink",
required: true,
},
force: {
type: ApplicationCommandOptionType.Boolean,
description: "Whether to overwrite an existing shortlink",
},
ttl: {
type: ApplicationCommandOptionType.String,
description: "The time-to-live of the shortlink",
},
},
},
remove: {
description: "Remove a shortlink.",
options: {
alias: {
type: ApplicationCommandOptionType.String,
description: "The alias of the shortlink",
required: true,
},
},
},
},
},
} as const satisfies AppSchema;

if (import.meta.main) {
await main();
Expand All @@ -41,12 +94,13 @@ export async function main() {
});

// Create the Discord application.
const handleInteraction = await createApp(
const shorterApp = await createApp(
{
schema: shorterSchema,
applicationID: DISCORD_CLIENT_ID,
publicKey: DISCORD_PUBLIC_KEY,
register: { token: DISCORD_TOKEN },
schema: appSchema,
token: DISCORD_TOKEN,
register: true,
},
{
add(interaction) {
Expand All @@ -58,9 +112,9 @@ export async function main() {
!interaction.member.roles.some((role) => DISCORD_ROLE_ID === role)
) {
return {
type: discord.InteractionResponseType.ChannelMessageWithSource,
type: InteractionResponseType.ChannelMessageWithSource,
data: {
flags: discord.MessageFlags.Ephemeral,
flags: MessageFlags.Ephemeral,
content: "You do not have permission to use this command.",
},
};
Expand Down Expand Up @@ -101,8 +155,6 @@ export async function main() {

// Send the success message.
await discordAPI.editOriginalInteractionResponse({
botID: DISCORD_CLIENT_ID,
botToken: DISCORD_TOKEN,
interactionToken: interaction.token,
content,
});
Expand All @@ -124,8 +176,6 @@ export async function main() {
.catch((error) => {
if (error instanceof Error) {
discordAPI.editOriginalInteractionResponse({
botID: DISCORD_CLIENT_ID,
botToken: DISCORD_TOKEN,
interactionToken: interaction.token,
content: `Error: ${error.message}`,
});
Expand All @@ -136,9 +186,8 @@ export async function main() {

// Acknowledge the interaction.
return {
type:
discord.InteractionResponseType.DeferredChannelMessageWithSource,
} satisfies discord.APIInteractionResponseDeferredChannelMessageWithSource;
type: InteractionResponseType.DeferredChannelMessageWithSource,
} satisfies APIInteractionResponseDeferredChannelMessageWithSource;
},
remove(interaction) {
if (!interaction.member?.user) {
Expand All @@ -149,9 +198,9 @@ export async function main() {
!interaction.member.roles.some((role) => DISCORD_ROLE_ID === role)
) {
return {
type: discord.InteractionResponseType.ChannelMessageWithSource,
type: InteractionResponseType.ChannelMessageWithSource,
data: {
flags: discord.MessageFlags.Ephemeral,
flags: MessageFlags.Ephemeral,
content: "You do not have permission to use this command.",
},
};
Expand All @@ -172,8 +221,6 @@ export async function main() {
.then(async (result) => {
// Send the success message.
await discordAPI.editOriginalInteractionResponse({
botID: DISCORD_CLIENT_ID,
botToken: DISCORD_TOKEN,
interactionToken: interaction.token,
content:
`Removed \`${interaction.data.parsedOptions.alias}\` in commit [${result.message}](https://acmcsuf.com/code/commit/${result.sha}).`,
Expand All @@ -182,8 +229,6 @@ export async function main() {
.catch((error) => {
if (error instanceof Error) {
discordAPI.editOriginalInteractionResponse({
botID: DISCORD_CLIENT_ID,
botToken: DISCORD_TOKEN,
interactionToken: interaction.token,
content: `Error: ${error.message}`,
});
Expand All @@ -194,9 +239,8 @@ export async function main() {

// Acknowledge the interaction.
return {
type:
discord.InteractionResponseType.DeferredChannelMessageWithSource,
} satisfies discord.APIInteractionResponseDeferredChannelMessageWithSource;
type: InteractionResponseType.DeferredChannelMessageWithSource,
} satisfies APIInteractionResponseDeferredChannelMessageWithSource;
},
},
);
Expand All @@ -213,7 +257,7 @@ export async function main() {
console.log("Discord application information:", APPLICATION_URL);
},
},
handleInteraction,
shorterApp,
);
}

Expand Down

1 comment on commit 979aaa4

@deno-deploy
Copy link

@deno-deploy deno-deploy bot commented on 979aaa4 Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to deploy:

UNCAUGHT_EXCEPTION

Error: Failed to register command: 400 Bad Request
    at registerApplicationCommand (https://jsr.io/@discord-applications/app/0.0.4/discord_api.ts:111:11)
    at eventLoopTick (ext:core/01_core.js:153:7)
    at async createApp (https://jsr.io/@discord-applications/app/0.0.4/app_handler.ts:414:5)
    at async main (file:///src/main.ts:97:22)
    at async file:///src/main.ts:82:3

Please sign in to comment.