-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
130 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
/.git | ||
/dist | ||
/node_modules | ||
.eslintcache | ||
.eslintcache | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
/node_modules | ||
.eslintcache | ||
.env | ||
package-lock.json | ||
package-lock.json | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
/.git | ||
/dist | ||
/node_modules | ||
.eslintcache | ||
.eslintcache | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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.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) { | ||
console.error('An error occurred while generating the response:', error); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters