Compare commits

...

3 commits

Author SHA1 Message Date
df6aabd2bd
respect your developers smolhaj 2023-03-10 22:14:31 +09:00
7adaebe7b5
even 2023-03-10 22:12:02 +09:00
b572ed3111
better 2023-03-10 22:11:49 +09:00

View file

@ -16,7 +16,7 @@ import {
} from "openai"; } from "openai";
const SYSTEM_MESSAGE = const SYSTEM_MESSAGE =
"You are Smolhaj, a Discord bot. You are helpful and friendly, and answers concisely. Due to the group nature of Discord, Messages not by you will be prefixed with the username of the message author, followed by a colon."; "You are Smolhaj, a Discord bot made by skyevg. You are helpful and friendly, and answers concisely. Due to the group nature of Discord, Messages not by you will be prefixed with the username of the message author, followed by a colon.";
const client = new Client({ const client = new Client({
intents: [ intents: [
@ -43,6 +43,7 @@ client.on("messageCreate", async (message) => {
if (message.channel.type != ChannelType.GuildText) return; if (message.channel.type != ChannelType.GuildText) return;
if (message.content == "die") process.exit(1); if (message.content == "die") process.exit(1);
// "adapted" from https://github.com/ryanccn/blahaj/blob/main/src/chat.ts // "adapted" from https://github.com/ryanccn/blahaj/blob/main/src/chat.ts
await message.channel.sendTyping(); await message.channel.sendTyping();
@ -51,11 +52,18 @@ client.on("messageCreate", async (message) => {
message.channel.sendTyping(); message.channel.sendTyping();
}, 5000); }, 5000);
const recieved = Date.now();
await new Promise<void>((resolve) => setTimeout(() => resolve(), 1500));
try { try {
const msgs = await message.channel.messages.fetch({ const msgs = await message.channel.messages.fetch({
after: SnowflakeUtil.generate({ after: SnowflakeUtil.generate({
timestamp: Date.now() - 5 * 60 * 1000, timestamp: Date.now() - 5 * 60 * 1000,
}).toString(), }).toString(),
before: SnowflakeUtil.generate({
timestamp: recieved + 500,
}).toString(),
}); });
const context = [ const context = [
@ -89,15 +97,31 @@ client.on("messageCreate", async (message) => {
const responseMessage = response.data.choices[0].message; const responseMessage = response.data.choices[0].message;
if (!responseMessage) return; if (!responseMessage) return;
const lastMessage = msgs
.filter((msg) => {
if (msg.webhookId && !msg.content.startsWith("\\")) return true;
if (msg.author.bot && msg.author !== msg.author.client.user)
return false;
if (msg.content.startsWith("\\")) return false;
return true;
}).first() ?? message;
const isAppropriate = await openai const isAppropriate = await openai
.createModeration({ input: responseMessage.content }) .createModeration({ input: responseMessage.content })
.then(({ data }) => !data.results[0].flagged); .then(({ data }) => !data.results[0].flagged);
if (isAppropriate) { if (isAppropriate) {
await message.channel.send({ try {
content: responseMessage.content, await lastMessage.reply({
allowedMentions: { parse: ["users"] }, content: responseMessage.content,
}); allowedMentions: { parse: ["users"] },
});
} catch {
await message.channel.send({
content: responseMessage.content,
allowedMentions: { parse: ["users"] },
});
}
} else { } else {
await message.channel.send({ await message.channel.send({
embeds: [ embeds: [