aboutsummaryrefslogtreecommitdiff
path: root/bot/src/events/MessageCreate.js
blob: e7af3a8a3c79210b69f79b86a89fa9dd6621d5ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { Events } from 'discord.js';
import { ollama } from '../utils/ollama.js';
import { ollamaGlobal, ollamaModel } from '../storage/consts.js';
import { guildSettings } from '../models/guild-settings.js';

export default {
    name: Events.MessageCreate,
    async execute(msg) {
        if (!msg.client.application?.owner) await msg.client.application?.fetch();
        if (msg.author.bot) return;
        if (!msg.guild) return;

        const guildSetting = await guildSettings.findOne({ where: { guildID: msg.guild.id } });

        const args = msg.content.slice(`<@${msg.client.user.id}>`.length).trim();

        if (msg.mentions.has(msg.client.user)) {
            if (!guildSetting.ollamaEnabled) return;
            if (!ollamaGlobal) return msg.reply('Sorry, this feature has been turned off.');
            if (!args) return msg.reply('Sorry? What was that?');

            try {
                const response = await ollama.chat({
                    model: ollamaModel,
                    messages: [{ role: 'user', content: args }],
                });

                let content = response.message.content;
                content = content.replace(/<think>.*?<\/think>/g, '');

                if (content.length > 2000) {
                    const chunks = content.match(/[\s\S]{1,2000}/g) || [];
                    for (const chunk of chunks) {
                        await msg.reply({ content: chunk });
                    }
                } else {
                    await msg.reply({ content });
                }

            } catch (err) {
                console.error(err);
                await msg.reply(`Something went wrong. [Submit an issue at the AleeBot repository.](<https://github.com/Alee14/AleeBot/issues>)\nMessage:\n\`\`\`${err.stack}\`\`\``);
            }
        }
    }
};