diff --git a/bun.lockb b/bun.lockb index e628973..53338fa 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index aed6a91..569d77e 100644 --- a/package.json +++ b/package.json @@ -1,22 +1,23 @@ { - "name": "bot-template", - "module": "src/index.ts", - "devDependencies": { - "@discordeno/types": "^19.0.0-next.d81b28a", - "@types/bun": "latest" - }, - "peerDependencies": { - "typescript": "^5.0.0" - }, - "type": "module", - "dependencies": { - "@discordeno/bot": "^19.0.0-next.d81b28a", - "@discordeno/rest": "^19.0.0-next.d81b28a" - }, - "scripts": { - "dev": "bun run --watch src/index.ts", - "bot:list": "bun run bin/list.js", - "bot:delete": "bun run bin/delete.js", - "bot:update": "bun run bin/update.js" - } + "name": "bot-template", + "module": "src/index.ts", + "devDependencies": { + "@discordeno/types": "^19.0.0-next.d81b28a", + "@types/bun": "latest" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "type": "module", + "dependencies": { + "@discordeno/bot": "^19.0.0-next.d81b28a", + "@discordeno/rest": "^19.0.0-next.d81b28a", + "ollama": "^0.5.0" + }, + "scripts": { + "dev": "bun run --watch src/index.ts", + "bot:list": "bun run bin/list.js", + "bot:delete": "bun run bin/delete.js", + "bot:update": "bun run bin/update.js" + } } diff --git a/src/interactions/ai.ts b/src/interactions/ai.ts new file mode 100644 index 0000000..0c618f5 --- /dev/null +++ b/src/interactions/ai.ts @@ -0,0 +1,40 @@ +import type { Command } from "../lib/types/command"; +import { type Interaction } from "@discordeno/bot"; +import SlashCommand from "../lib/classes/SlashCommand"; +import { Ollama } from "ollama"; + +const ollama = new Ollama({ host: "http://host.docker.internal:11434" }); + +export default class extends SlashCommand implements Command { + name = "ai"; + description = "Send a message to llama3."; + options = [{ type: 3, name: "input", description: "Text input to send to llama3.", required: true }]; + + constructor() { + super(); + } + + async run(interaction: Interaction) { + await interaction.defer(); + + const input = interaction.data?.options?.find((_) => _.name === "input")?.value as string; + let fullMessage = ""; + let index = 0; + + const response = await ollama.chat({ + model: "terrye", + messages: [{ role: "user", content: input }], + stream: true, + }); + + for await (const part of response) { + fullMessage += part.message.content; + index++; + + if (index % 10 === 0) await interaction.edit(fullMessage); + if (part.done) await interaction.edit(fullMessage); + } + + return false; + } +} diff --git a/src/interactions/default.ts b/src/interactions/default.ts index 32f8862..dcec7af 100644 --- a/src/interactions/default.ts +++ b/src/interactions/default.ts @@ -1,14 +1,6 @@ import type { Command } from "../lib/types/command"; -import { - MessageComponentTypes, - TextStyles, - type Interaction, - type InteractionResponse, - InteractionResponseTypes, -} from "@discordeno/bot"; +import { InteractionResponseTypes } from "@discordeno/bot"; import SlashCommand from "../lib/classes/SlashCommand"; -import Modal from "../lib/classes/Modal"; -import { collectModal } from "../lib/handlers/ModalHandler"; export default class extends SlashCommand implements Command { name = "default"; @@ -19,7 +11,7 @@ export default class extends SlashCommand implements Command { super(); } - async run(interaction: Interaction) { + async run() { return { type: InteractionResponseTypes.ChannelMessageWithSource, data: { content: "my name is terry and im going to destroy thgis world" }, diff --git a/src/lib/handlers/CommandHandler.ts b/src/lib/handlers/CommandHandler.ts index c4f36c2..fa57fd1 100644 --- a/src/lib/handlers/CommandHandler.ts +++ b/src/lib/handlers/CommandHandler.ts @@ -17,9 +17,10 @@ export function handle(interaction: Interaction) { resolved .run(interaction) - .then((response: InteractionResponse) => - REST.sendInteractionResponse(interaction.id, interaction.token, response) - ) + .then((response: InteractionResponse | boolean) => { + if (!response) return; + REST.sendInteractionResponse(interaction.id, interaction.token, response as InteractionResponse); + }) .catch((e: Error) => { console.log("CommandHandler.ts -> Exception"); console.log(e); diff --git a/src/lib/types/command.d.ts b/src/lib/types/command.d.ts index 3112e00..57e3dcf 100644 --- a/src/lib/types/command.d.ts +++ b/src/lib/types/command.d.ts @@ -1,6 +1,6 @@ import { type Interaction, type InteractionCallbackData, type InteractionResponse } from "@discordeno/bot"; interface Command { - run(interaction: Interaction): Promise; + run(interaction: Interaction): Promise; autocomplete?(interaction: Interaction): Promise; }