This commit is contained in:
cirroskais 2024-04-20 00:02:06 -04:00
parent 14867491af
commit 05f3b48f11
No known key found for this signature in database
GPG key ID: 5FC73EBF2678E33D
6 changed files with 68 additions and 34 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -1,22 +1,23 @@
{ {
"name": "bot-template", "name": "bot-template",
"module": "src/index.ts", "module": "src/index.ts",
"devDependencies": { "devDependencies": {
"@discordeno/types": "^19.0.0-next.d81b28a", "@discordeno/types": "^19.0.0-next.d81b28a",
"@types/bun": "latest" "@types/bun": "latest"
}, },
"peerDependencies": { "peerDependencies": {
"typescript": "^5.0.0" "typescript": "^5.0.0"
}, },
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@discordeno/bot": "^19.0.0-next.d81b28a", "@discordeno/bot": "^19.0.0-next.d81b28a",
"@discordeno/rest": "^19.0.0-next.d81b28a" "@discordeno/rest": "^19.0.0-next.d81b28a",
}, "ollama": "^0.5.0"
"scripts": { },
"dev": "bun run --watch src/index.ts", "scripts": {
"bot:list": "bun run bin/list.js", "dev": "bun run --watch src/index.ts",
"bot:delete": "bun run bin/delete.js", "bot:list": "bun run bin/list.js",
"bot:update": "bun run bin/update.js" "bot:delete": "bun run bin/delete.js",
} "bot:update": "bun run bin/update.js"
}
} }

40
src/interactions/ai.ts Normal file
View file

@ -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;
}
}

View file

@ -1,14 +1,6 @@
import type { Command } from "../lib/types/command"; import type { Command } from "../lib/types/command";
import { import { InteractionResponseTypes } from "@discordeno/bot";
MessageComponentTypes,
TextStyles,
type Interaction,
type InteractionResponse,
InteractionResponseTypes,
} from "@discordeno/bot";
import SlashCommand from "../lib/classes/SlashCommand"; 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 { export default class extends SlashCommand implements Command {
name = "default"; name = "default";
@ -19,7 +11,7 @@ export default class extends SlashCommand implements Command {
super(); super();
} }
async run(interaction: Interaction) { async run() {
return { return {
type: InteractionResponseTypes.ChannelMessageWithSource, type: InteractionResponseTypes.ChannelMessageWithSource,
data: { content: "my name is terry and im going to destroy thgis world" }, data: { content: "my name is terry and im going to destroy thgis world" },

View file

@ -17,9 +17,10 @@ export function handle(interaction: Interaction) {
resolved resolved
.run(interaction) .run(interaction)
.then((response: InteractionResponse) => .then((response: InteractionResponse | boolean) => {
REST.sendInteractionResponse(interaction.id, interaction.token, response) if (!response) return;
) REST.sendInteractionResponse(interaction.id, interaction.token, response as InteractionResponse);
})
.catch((e: Error) => { .catch((e: Error) => {
console.log("CommandHandler.ts -> Exception"); console.log("CommandHandler.ts -> Exception");
console.log(e); console.log(e);

View file

@ -1,6 +1,6 @@
import { type Interaction, type InteractionCallbackData, type InteractionResponse } from "@discordeno/bot"; import { type Interaction, type InteractionCallbackData, type InteractionResponse } from "@discordeno/bot";
interface Command { interface Command {
run(interaction: Interaction): Promise<InteractionResponse>; run(interaction: Interaction): Promise<InteractionResponse | boolean>;
autocomplete?(interaction: Interaction): Promise<InteractionCallbackData>; autocomplete?(interaction: Interaction): Promise<InteractionCallbackData>;
} }