44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
|
import { ChatCompletionRequestMessage as OpenAIMessage } from "openai";
|
||
|
import { Collection, Message as DiscordMessage } from "discord.js";
|
||
|
|
||
|
import config from "./config.json";
|
||
|
|
||
|
function formatMessage(message: DiscordMessage): string {
|
||
|
let rvalue: string = message.cleanContent;
|
||
|
|
||
|
for (const attachment of message.attachments) {
|
||
|
rvalue += ` [Attachment: ${attachment[1].name}`;
|
||
|
rvalue += attachment[1].description ? ` - ${attachment[1].description}` : "";
|
||
|
rvalue += "]";
|
||
|
}
|
||
|
|
||
|
for (const embed of message.embeds) {
|
||
|
rvalue += ` [Embed:`;
|
||
|
rvalue += embed.title ? ` ${embed.title}` : "";
|
||
|
rvalue += embed.author ? ` by ${embed.author}` : "";
|
||
|
rvalue += embed.title || embed.author ? " -": "";
|
||
|
rvalue += embed.description ? ` ${embed.description}` : "";
|
||
|
rvalue += "]";
|
||
|
}
|
||
|
|
||
|
return rvalue;
|
||
|
}
|
||
|
|
||
|
export default function toOpenAIMessages(messages: Collection<string, DiscordMessage>): OpenAIMessage[] {
|
||
|
const rvalue: OpenAIMessage[] = [];
|
||
|
|
||
|
rvalue.push({ role: "system", content: config.systemPrompt});
|
||
|
|
||
|
messages
|
||
|
.sort((a, b) => a.createdTimestamp - b.createdTimestamp)
|
||
|
.each(message => {
|
||
|
rvalue.push({
|
||
|
role: message.author.id == message.client.user.id ? "assistant" : "user",
|
||
|
content: formatMessage(message),
|
||
|
name: message.member?.displayName,
|
||
|
});
|
||
|
});
|
||
|
|
||
|
return rvalue;
|
||
|
}
|