Skip to content

Commit

Permalink
V1.3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasB25 committed May 8, 2024
1 parent 827e8a9 commit 83ebafe
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 63 deletions.
6 changes: 4 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
#For GEMINI
GEMINI_KEY= #GEMINI key from https://makersuite.google.com/
GEMINI_MODEL=gemini-1.5-pro-latest
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
70 changes: 70 additions & 0 deletions src/events/Ai/GeminiAssistance.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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}`);
}
}
}
}
}
55 changes: 1 addition & 54 deletions src/events/client/MessageCreate.ts
Original file line number Diff line number Diff line change
@@ -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, {
Expand All @@ -16,55 +11,7 @@ export default class MessageCreate extends Event {

public async run(message: Message): Promise<void> {
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
}
}
}
4 changes: 2 additions & 2 deletions src/structures/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 83ebafe

Please sign in to comment.