Compare commits
3 commits
9b2b141af6
...
df6aabd2bd
Author | SHA1 | Date | |
---|---|---|---|
df6aabd2bd | |||
7adaebe7b5 | |||
b572ed3111 |
1 changed files with 29 additions and 5 deletions
26
index.ts
26
index.ts
|
@ -16,7 +16,7 @@ import {
|
|||
} from "openai";
|
||||
|
||||
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({
|
||||
intents: [
|
||||
|
@ -43,6 +43,7 @@ client.on("messageCreate", async (message) => {
|
|||
if (message.channel.type != ChannelType.GuildText) return;
|
||||
if (message.content == "die") process.exit(1);
|
||||
|
||||
|
||||
// "adapted" from https://github.com/ryanccn/blahaj/blob/main/src/chat.ts
|
||||
|
||||
await message.channel.sendTyping();
|
||||
|
@ -51,11 +52,18 @@ client.on("messageCreate", async (message) => {
|
|||
message.channel.sendTyping();
|
||||
}, 5000);
|
||||
|
||||
const recieved = Date.now();
|
||||
|
||||
await new Promise<void>((resolve) => setTimeout(() => resolve(), 1500));
|
||||
|
||||
try {
|
||||
const msgs = await message.channel.messages.fetch({
|
||||
after: SnowflakeUtil.generate({
|
||||
timestamp: Date.now() - 5 * 60 * 1000,
|
||||
}).toString(),
|
||||
before: SnowflakeUtil.generate({
|
||||
timestamp: recieved + 500,
|
||||
}).toString(),
|
||||
});
|
||||
|
||||
const context = [
|
||||
|
@ -89,15 +97,31 @@ client.on("messageCreate", async (message) => {
|
|||
const responseMessage = response.data.choices[0].message;
|
||||
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
|
||||
.createModeration({ input: responseMessage.content })
|
||||
.then(({ data }) => !data.results[0].flagged);
|
||||
|
||||
if (isAppropriate) {
|
||||
try {
|
||||
await lastMessage.reply({
|
||||
content: responseMessage.content,
|
||||
allowedMentions: { parse: ["users"] },
|
||||
});
|
||||
} catch {
|
||||
await message.channel.send({
|
||||
content: responseMessage.content,
|
||||
allowedMentions: { parse: ["users"] },
|
||||
});
|
||||
}
|
||||
} else {
|
||||
await message.channel.send({
|
||||
embeds: [
|
||||
|
|
Loading…
Reference in a new issue