Compare commits
No commits in common. "df6aabd2bd8a2bcbc1a6d8cac23c299e6b5dff7b" and "9b2b141af66d917f4744713e5503b0d7bd55f83b" have entirely different histories.
df6aabd2bd
...
9b2b141af6
1 changed files with 5 additions and 29 deletions
34
index.ts
34
index.ts
|
@ -16,7 +16,7 @@ import {
|
|||
} from "openai";
|
||||
|
||||
const SYSTEM_MESSAGE =
|
||||
"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.";
|
||||
"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.";
|
||||
|
||||
const client = new Client({
|
||||
intents: [
|
||||
|
@ -43,7 +43,6 @@ 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();
|
||||
|
@ -52,18 +51,11 @@ 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 = [
|
||||
|
@ -97,31 +89,15 @@ 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"] },
|
||||
});
|
||||
}
|
||||
await message.channel.send({
|
||||
content: responseMessage.content,
|
||||
allowedMentions: { parse: ["users"] },
|
||||
});
|
||||
} else {
|
||||
await message.channel.send({
|
||||
embeds: [
|
||||
|
|
Loading…
Reference in a new issue