Skip to content

Commit

Permalink
new version, updated serverless template
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Best-Codes committed Jan 13, 2025
1 parent 3f5db0b commit fea1a5a
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discraft",
"version": "1.6.5-beta.3",
"version": "1.6.5-beta.4",
"description": "Ultimate Discord bot framework",
"type": "module",
"packageManager": "[email protected]",
Expand Down
13 changes: 9 additions & 4 deletions package/src/cli/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,15 @@ async function init(options: InitOptions = {}) {
consola.info(
`Installing dependencies with ${kleur.cyan(packageManager)}...`,
);
await runSubprocess(packageManager, ["install"], {
cwd: projectDir,
});
consola.success("Dependencies installed successfully!");
try {
await runSubprocess(packageManager, ["install"], {
cwd: projectDir,
});
consola.success("Dependencies installed successfully!");
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any
} catch (error: any) {
consola.error("Could not install dependencies.");
}
}

const pmCommand =
Expand Down
3 changes: 3 additions & 0 deletions templates/vercel-ts-ai/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# You will need to add these secrets to the 'Environment Variables' section of your Vercel project
# https://vercel.com/docs/projects/environment-variables

# From `General Information > Public Key` | https://discord.com/developers/applications
DISCORD_PUBLIC_KEY=''
# From `General Information > App ID` | https://discord.com/developers/applications
Expand Down
56 changes: 41 additions & 15 deletions templates/vercel-ts-ai/commands/chat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { GoogleGenerativeAI } from "@google/generative-ai";
import { type SimplifiedInteraction } from "../utils/types";
import {
type APIApplicationCommandInteraction,
type APIApplicationCommandOption,
type APIChatInputApplicationCommandInteraction,
type APIInteractionResponse,
ApplicationCommandOptionType,
InteractionResponseType,
MessageFlags,
type RESTPostAPIApplicationCommandsJSONBody,
} from "discord-api-types/v10";

export default {
data: {
Expand All @@ -9,40 +18,56 @@ export default {
{
name: "prompt",
description: "The prompt for the AI",
type: 3,
type: ApplicationCommandOptionType.String,
required: true,
},
{
name: "image",
description: "Optional image to include in the prompt",
type: 11,
type: ApplicationCommandOptionType.Attachment,
required: false,
},
],
},

async execute(data: { interaction: SimplifiedInteraction }) {
} as RESTPostAPIApplicationCommandsJSONBody,
async execute(data: {
interaction: APIApplicationCommandInteraction;
}): Promise<APIInteractionResponse> {
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_AI_API_KEY || "");
const model = genAI.getGenerativeModel({
model: process.env.GOOGLE_AI_MODEL || "gemini-1.5-flash",
});
const interaction = data.interaction;
const promptOption = interaction.data.options?.find(

if (interaction.data.type !== 1) {
return {
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content:
"This command can only be used as a chat input (slash) command.",
flags: MessageFlags.Ephemeral,
},
};
}

const chatInteraction =
interaction as APIChatInputApplicationCommandInteraction;

const promptOption = chatInteraction.data.options?.find(
(option) => option.name === "prompt",
);
const imageOption = interaction.data.options?.find(
) as (APIApplicationCommandOption & { value: string }) | undefined;
const imageOption = chatInteraction.data.options?.find(
(option) => option.name === "image",
);
) as (APIApplicationCommandOption & { value: string }) | undefined;
const prompt = promptOption?.value || "";
const imageAttachment =
interaction.data.resolved?.attachments?.[imageOption?.value || ""];
chatInteraction.data.resolved?.attachments?.[imageOption?.value || ""];

if (prompt.length > 2000) {
return {
type: 4,
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Prompt must be less than 2000 characters.",
flags: 64,
flags: MessageFlags.Ephemeral,
},
};
}
Expand All @@ -68,17 +93,18 @@ export default {
const response = result.response.text();

return {
type: 4,
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: response,
},
};
} catch (error) {
console.error("Error during AI chat:", error);
return {
type: 4,
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "An error occurred while processing your request.",
flags: MessageFlags.Ephemeral,
},
};
}
Expand Down
16 changes: 11 additions & 5 deletions templates/vercel-ts-ai/commands/ping.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { type SimplifiedInteraction } from "../utils/types";
import {
type APIApplicationCommandInteraction,
type APIInteractionResponse,
type RESTPostAPIApplicationCommandsJSONBody,
InteractionResponseType,
} from "discord-api-types/v10";

export default {
data: {
name: "ping",
description: "Check if the bot is online",
},

} as RESTPostAPIApplicationCommandsJSONBody,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async execute(data: { interaction: SimplifiedInteraction }) {
async execute(data: {
interaction: APIApplicationCommandInteraction;
}): Promise<APIInteractionResponse> {
return {
type: 4,
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Pong from Vercel!",
},
Expand Down
24 changes: 22 additions & 2 deletions templates/vercel-ts-ai/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { VercelRequest, VercelResponse } from "@vercel/node";
import {
type APIInteractionResponse,
InteractionResponseType,
MessageFlags,
} from "discord-api-types/v10";
import { InteractionType, verifyKey } from "discord-interactions";
import getRawBody from "raw-body";
import commands from "./.discraft/commands/index";
Expand Down Expand Up @@ -84,8 +89,23 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
const command = (commands as any)[commandName];

if (command) {
const commandResponse = await command.execute({ interaction: message });
logger.debug("Command executed successfully", { commandName });
let commandResponse: APIInteractionResponse;
try {
commandResponse = await command.execute({ interaction: message });
logger.debug("Command executed successfully", { commandName });
} catch (error) {
logger.error("Error executing command", {
commandName,
error,
});
commandResponse = {
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "An error occurred while processing your request.",
flags: MessageFlags.Ephemeral,
},
};
}
return res.status(200).json(commandResponse);
}

Expand Down
4 changes: 1 addition & 3 deletions templates/vercel-ts-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"build": "discraft vercel build"
},
"devDependencies": {
"@types/express": "^5.0.0",
"@types/node": "^22.10.5",
"discraft": "latest",
"typescript": "^5.7.3",
Expand All @@ -20,9 +19,8 @@
"@google/generative-ai": "^0.21.0",
"@vercel/node": "^5.0.2",
"consola": "^3.3.3",
"discord-api-types": "^0.37.115",
"discord-interactions": "^4.1.0",
"discord.js": "^14.17.3",
"express": "^4.21.2",
"raw-body": "^3.0.0"
}
}
15 changes: 14 additions & 1 deletion templates/vercel-ts-ai/vercel.json
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
{ "version": 2, "rewrites": [{ "source": "/(.*)", "destination": "/api" }] }
{
"version": 2,
"rewrites": [
{
"source": "/(.*)",
"destination": "/api"
}
],
"functions": {
"api/index.js": {
"maxDuration": 60
}
}
}

0 comments on commit fea1a5a

Please sign in to comment.