Initial commit
This commit is contained in:
commit
c18b8d83ef
9 changed files with 2191 additions and 0 deletions
43
src/toOpenAIMessages.ts
Normal file
43
src/toOpenAIMessages.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue