From 83ebafeac6c605e63aa72f612273da59e79c144d Mon Sep 17 00:00:00 2001 From: LucasB25#7323 <50886682+LucasB25@users.noreply.github.com> Date: Wed, 8 May 2024 23:10:46 +0200 Subject: [PATCH] V1.3.6 --- .env.example | 6 +- package.json | 4 +- src/commands/{imagine => ai}/Imagine.ts | 2 +- src/commands/{imagine => general}/About.ts | 0 src/commands/{imagine => general}/Invite.ts | 0 src/commands/{imagine => general}/Ping.ts | 0 src/config.ts | 4 +- src/events/Ai/GeminiAssistance.ts | 70 +++++++++++++++++++++ src/events/client/MessageCreate.ts | 55 +--------------- src/structures/Client.ts | 4 +- 10 files changed, 82 insertions(+), 63 deletions(-) rename src/commands/{imagine => ai}/Imagine.ts (99%) rename src/commands/{imagine => general}/About.ts (100%) rename src/commands/{imagine => general}/Invite.ts (100%) rename src/commands/{imagine => general}/Ping.ts (100%) create mode 100644 src/events/Ai/GeminiAssistance.ts diff --git a/.env.example b/.env.example index 6c21a43..50124f1 100644 --- a/.env.example +++ b/.env.example @@ -2,8 +2,10 @@ TOKEN= #Discord Bot Token CLIENT_ID= #Discord Bot Client ID Activity=/imagine +#For REPLICATE REPLICATE_TOKEN= #Replicate Token from https://replicate.com/signin REPLICATE_MODEL=bytedance/sdxl-lightning-4step:727e49a643e999d602a896c774a0658ffefea21465756a6ce24b7ea4165eba6a -GOOGLE_KEY= #Google key from https://makersuite.google.com/ -GOOGLE_MODEL=gemini-1.5-pro-latest \ No newline at end of file +#For GEMINI +GEMINI_KEY= #GEMINI key from https://makersuite.google.com/ +GEMINI_MODEL=gemini-1.5-pro-latest \ No newline at end of file diff --git a/package.json b/package.json index 4b4bcaa..1de993f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "AikouBot", - "version": "1.3.5", + "version": "1.3.6", "description": "A simple AikouBot bot for discord", "type": "module", "main": "dist/index.js", @@ -23,7 +23,7 @@ }, "homepage": "https://github.com/LucasB25/AikouBot#readme", "devDependencies": { - "@types/node": "^20.12.10", + "@types/node": "^20.12.11", "@types/signale": "^1.4.7", "@typescript-eslint/eslint-plugin": "^7.8.0", "@typescript-eslint/parser": "^7.8.0", diff --git a/src/commands/imagine/Imagine.ts b/src/commands/ai/Imagine.ts similarity index 99% rename from src/commands/imagine/Imagine.ts rename to src/commands/ai/Imagine.ts index 6318093..85b2be9 100644 --- a/src/commands/imagine/Imagine.ts +++ b/src/commands/ai/Imagine.ts @@ -23,7 +23,7 @@ export default class Imagine extends Command { descriptionLocalizations: { fr: '📷 | Crée une image à partir d\'un prompt', }, - category: 'fun', + category: 'ai', cooldown: 3, permissions: { client: ['SendMessages', 'ViewChannel', 'EmbedLinks', 'AttachFiles'], diff --git a/src/commands/imagine/About.ts b/src/commands/general/About.ts similarity index 100% rename from src/commands/imagine/About.ts rename to src/commands/general/About.ts diff --git a/src/commands/imagine/Invite.ts b/src/commands/general/Invite.ts similarity index 100% rename from src/commands/imagine/Invite.ts rename to src/commands/general/Invite.ts diff --git a/src/commands/imagine/Ping.ts b/src/commands/general/Ping.ts similarity index 100% rename from src/commands/imagine/Ping.ts rename to src/commands/general/Ping.ts diff --git a/src/config.ts b/src/config.ts index 50540e2..9268e22 100644 --- a/src/config.ts +++ b/src/config.ts @@ -9,6 +9,6 @@ export default { replicateToken: process.env.REPLICATE_TOKEN, replicateModel: process.env.REPLICATE_MODEL as any, - googleKey: process.env.GOOGLE_KEY, - googleModel: process.env.GOOGLE_MODEL as any, + geminiKey: process.env.GEMINI_KEY, + geminiModel: process.env.GEMINI_MODEL as any, }; diff --git a/src/events/Ai/GeminiAssistance.ts b/src/events/Ai/GeminiAssistance.ts new file mode 100644 index 0000000..4e8a3dc --- /dev/null +++ b/src/events/Ai/GeminiAssistance.ts @@ -0,0 +1,70 @@ +import { GoogleGenerativeAI } from '@google/generative-ai'; +import { Message, TextChannel } from 'discord.js'; + +import { Bot, Event, EventsTypes } from '../../structures/index.js'; + +function truncateText(text: string, maxLength: number): string { + return text.length > maxLength ? text.substring(0, maxLength - 3) + '...' : text; +} + +export default class MessageCreate extends Event { + constructor(client: Bot, file: string) { + super(client, file, { + name: EventsTypes.MessageCreate, + }); + } + + public async run(message: Message): Promise { + if (message.channel instanceof TextChannel) { + if (message.content.endsWith('?')) { + try { + const threadName = truncateText(message.content, 100); + const thread = await message.startThread({ + name: threadName, + autoArchiveDuration: 60, + }); + + const generationConfig = { + maxOutputTokens: 1900, + temperature: 0.9, + topK: 1, + topP: 1, + }; + + const genAI = new GoogleGenerativeAI(this.client.config.geminiKey); + const model = genAI.getGenerativeModel({ + model: this.client.config.geminiModel, + generationConfig, + } as any); + + let chat = model.startChat({ + history: [ + { + role: 'user', + parts: [{ text: message.content }], + }, + ], + }); + + let result = await chat.sendMessage(message.content); + let response = result.response; + + let generatedText = response.text(); + + while (generatedText.length > 1500) { + await thread.send(generatedText.substring(0, 1500)); + generatedText = generatedText.substring(1500); + + result = await chat.sendMessage(generatedText); + response = result.response; + generatedText = response.text(); + } + + await thread.send(generatedText); + } catch (error) { + throw new Error(`An error occurred while generating the response: ${error}`); + } + } + } + } +} diff --git a/src/events/client/MessageCreate.ts b/src/events/client/MessageCreate.ts index 90e418a..0e46e63 100644 --- a/src/events/client/MessageCreate.ts +++ b/src/events/client/MessageCreate.ts @@ -1,12 +1,7 @@ -import { GoogleGenerativeAI } from '@google/generative-ai'; import { Message, TextChannel } from 'discord.js'; import { Bot, Event, EventsTypes } from '../../structures/index.js'; -function truncateText(text: string, maxLength: number): string { - return text.length > maxLength ? text.substring(0, maxLength - 3) + '...' : text; -} - export default class MessageCreate extends Event { constructor(client: Bot, file: string) { super(client, file, { @@ -16,55 +11,7 @@ export default class MessageCreate extends Event { public async run(message: Message): Promise { if (message.channel instanceof TextChannel) { - if (message.content.endsWith('?')) { - try { - const threadName = truncateText(message.content, 100); - const thread = await message.startThread({ - name: threadName, - autoArchiveDuration: 60, - }); - - const generationConfig = { - maxOutputTokens: 1900, - temperature: 0.9, - topK: 1, - topP: 1, - }; - - const genAI = new GoogleGenerativeAI(this.client.config.googleKey); - const model = genAI.getGenerativeModel({ - model: this.client.config.googleModel, - generationConfig, - } as any); - - let chat = model.startChat({ - history: [ - { - role: 'user', - parts: [{ text: message.content }], - }, - ], - }); - - let result = await chat.sendMessage(message.content); - let response = result.response; - - let generatedText = response.text(); - - while (generatedText.length > 1500) { - await thread.send(generatedText.substring(0, 1500)); - generatedText = generatedText.substring(1500); - - result = await chat.sendMessage(generatedText); - response = result.response; - generatedText = response.text(); - } - - await thread.send(generatedText); - } catch (error) { - throw new Error(`An error occurred while generating the response: ${error}`); - } - } + // CODE } } } diff --git a/src/structures/Client.ts b/src/structures/Client.ts index 77758ac..2ab1df2 100644 --- a/src/structures/Client.ts +++ b/src/structures/Client.ts @@ -45,8 +45,8 @@ export default class Bot extends Client { this.logger.warn('Replicate token is missing. Replicate will not be initialized.'); } - if (this.config.googleKey) { - this.genAI = new GoogleGenerativeAI(this.config.googleKey); + if (this.config.geminiKey) { + this.genAI = new GoogleGenerativeAI(this.config.geminiModel); this.logger.info('GoogleGenerativeAI is initialized.'); } else { this.logger.warn(