2023-03-14 21:16:54 +01:00
|
|
|
import DiscordApi from "discord.js";
|
|
|
|
import { Configuration as OpenAIApiConfiguration, OpenAIApi } from "openai";
|
2023-05-02 17:55:48 +02:00
|
|
|
import { PrismaClient } from "@prisma/client";
|
2023-03-14 21:16:54 +01:00
|
|
|
|
2023-03-24 15:44:22 +01:00
|
|
|
import config from "./config";
|
2023-03-14 21:16:54 +01:00
|
|
|
import toOpenAIMessages from "./toOpenAIMessages";
|
2023-03-14 23:44:43 +01:00
|
|
|
import Moderation from "./moderation";
|
2023-03-14 21:16:54 +01:00
|
|
|
|
|
|
|
const discord = new DiscordApi.Client({
|
|
|
|
intents: [
|
|
|
|
DiscordApi.GatewayIntentBits.Guilds,
|
|
|
|
DiscordApi.GatewayIntentBits.GuildMessages,
|
|
|
|
DiscordApi.GatewayIntentBits.MessageContent,
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2023-03-14 23:44:43 +01:00
|
|
|
export const openai = new OpenAIApi(new OpenAIApiConfiguration({
|
2023-03-14 21:16:54 +01:00
|
|
|
apiKey: config.tokens.OpenAI
|
|
|
|
}));
|
|
|
|
|
2023-05-02 17:55:48 +02:00
|
|
|
export const database = new PrismaClient();
|
|
|
|
|
2023-03-14 21:16:54 +01:00
|
|
|
discord.on("ready", async event => {
|
|
|
|
console.log(`Connected to Discord as ${event.user.tag} (${event.user.id})`);
|
|
|
|
});
|
|
|
|
|
2023-03-25 11:24:43 +01:00
|
|
|
const channelsRunning: DiscordApi.Collection<string, DiscordApi.Message[]> = new DiscordApi.Collection();
|
|
|
|
|
2023-03-14 21:16:54 +01:00
|
|
|
discord.on("messageCreate", async message => {
|
|
|
|
if (message.author.bot) return;
|
|
|
|
if (!message.mentions.has(message.client.user)) return;
|
|
|
|
|
2023-05-02 20:41:59 +02:00
|
|
|
const userLimits = await database.limits.findUnique({
|
|
|
|
where: {
|
|
|
|
user: BigInt(message.author.id)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let limit = 25;
|
|
|
|
|
|
|
|
if (userLimits?.limit) {
|
|
|
|
limit = userLimits.limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!userLimits?.vip) {
|
|
|
|
const usedLimit = await database.usage.count({
|
|
|
|
select: { _all: true },
|
|
|
|
where: {
|
|
|
|
user: BigInt(message.author.id),
|
|
|
|
timestamp: {
|
|
|
|
gte: new Date(message.createdTimestamp - 1000*60*60*24 /*24 hours */)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (usedLimit._all >= limit) {
|
|
|
|
message.react("🛑");
|
|
|
|
message.author.dmChannel?.send({
|
|
|
|
embeds: [{
|
|
|
|
color: 0xff0000,
|
|
|
|
description: `You've used up your message limit for today, ${limit} requrests in last 24 hours`
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-25 11:24:43 +01:00
|
|
|
const messagesForChannel = channelsRunning.ensure(message.channelId, () => {return [] as DiscordApi.Message[];} );
|
|
|
|
const shouldStart = messagesForChannel.length == 0;
|
|
|
|
messagesForChannel.push(message);
|
|
|
|
if (shouldStart)
|
|
|
|
onMessage(message.channelId);
|
|
|
|
});
|
|
|
|
|
|
|
|
async function onMessage(channel: string) {
|
|
|
|
const channelQueue = channelsRunning.get(channel) as DiscordApi.Message[];
|
|
|
|
const message = channelQueue.at(0) as DiscordApi.Message;
|
|
|
|
|
2023-03-14 21:16:54 +01:00
|
|
|
try {
|
2023-03-24 16:47:26 +01:00
|
|
|
let messages: DiscordApi.Collection<string, DiscordApi.Message> = await message.channel.messages.fetch({ limit: config.limits.messages, cache: false });
|
2023-03-14 21:16:54 +01:00
|
|
|
|
2023-03-24 16:47:26 +01:00
|
|
|
messages = messages.filter(m => message.createdTimestamp - m.createdTimestamp < config.limits.time );
|
2023-03-14 21:16:54 +01:00
|
|
|
|
2023-03-14 23:44:43 +01:00
|
|
|
messages.forEach(m => Moderation.checkMessage(m));
|
|
|
|
|
2023-03-14 21:16:54 +01:00
|
|
|
message.channel.sendTyping();
|
|
|
|
const answer = await openai.createChatCompletion({
|
2023-03-24 15:44:22 +01:00
|
|
|
...config.chatCompletionConfig,
|
2023-03-14 21:16:54 +01:00
|
|
|
messages: toOpenAIMessages(messages),
|
|
|
|
});
|
|
|
|
|
|
|
|
const usage = answer.data.usage;
|
|
|
|
if (usage != undefined) {
|
|
|
|
const channelName: string = message.inGuild() ? `${message.channel.name} (${message.guild.name})` : `@${message.author.tag}`;
|
|
|
|
console.log(`Used ${usage.total_tokens} (${usage.prompt_tokens} + ${usage.completion_tokens}) tokens for ${message.author.tag} (${message.author.id}) in #${channelName}`);
|
2023-05-02 17:55:48 +02:00
|
|
|
|
|
|
|
database.usage.create({
|
|
|
|
data: {
|
|
|
|
timestamp: message.createdAt,
|
|
|
|
user: BigInt(message.author.id),
|
|
|
|
channel: BigInt(message.channelId),
|
|
|
|
guild: message.guildId ? BigInt(message.guildId) : null,
|
|
|
|
usageReguest: usage.prompt_tokens,
|
|
|
|
usageResponse: usage.completion_tokens
|
|
|
|
}
|
|
|
|
}).catch((e => {
|
|
|
|
console.error("Failed to push to a database");
|
|
|
|
console.error(e);
|
|
|
|
}));
|
2023-03-14 21:16:54 +01:00
|
|
|
}
|
2023-03-14 23:44:43 +01:00
|
|
|
|
2023-03-19 02:26:28 +01:00
|
|
|
const answerContent = answer.data.choices[0].message?.content;
|
|
|
|
|
|
|
|
if (answerContent != undefined && answerContent != "") {
|
|
|
|
const response = message.reply({
|
|
|
|
content: answerContent,
|
|
|
|
allowedMentions: {
|
|
|
|
repliedUser: false,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Moderation.checkMessage(await response);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
message.react("😶");
|
|
|
|
}
|
2023-03-14 21:16:54 +01:00
|
|
|
} catch (e) {
|
2023-03-22 06:40:16 +01:00
|
|
|
console.error(`Error ocurred while handling chat completion request (${(e as object).constructor.name}):`);
|
2023-03-14 21:16:54 +01:00
|
|
|
console.error(e);
|
|
|
|
|
|
|
|
message.reply({
|
|
|
|
embeds: [{
|
|
|
|
color: 0xff0000,
|
2023-03-22 06:40:16 +01:00
|
|
|
description: "Something bad happened! :frowning:"
|
2023-03-14 21:16:54 +01:00
|
|
|
}],
|
|
|
|
allowedMentions: {
|
|
|
|
repliedUser: false,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-03-25 11:24:43 +01:00
|
|
|
|
|
|
|
channelQueue.shift();
|
|
|
|
if (channelQueue.length == 0)
|
|
|
|
channelsRunning.delete(channel);
|
|
|
|
else
|
|
|
|
onMessage(channel);
|
|
|
|
}
|
2023-03-14 21:16:54 +01:00
|
|
|
|
|
|
|
discord.login(config.tokens.Discord);
|