-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
166 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,71 @@ | ||
import { safeParse } from '../json-utils'; | ||
import Logger from '../logger'; | ||
import { OpenAiApiService } from '../open-ai-api-service'; | ||
import { PromptBuilder } from '../prompt-builder'; | ||
import { banTool } from '../tools/ban-tool'; | ||
import { doNothingTool } from '../tools/do-nothing'; | ||
import { Client, Message } from 'discord.js'; | ||
|
||
export const moderator = { | ||
execute: async (client: Client, message: Message) => {}, | ||
execute: async (client: Client, message: Message) => { | ||
const isNukisMessage = message.author.id === client.user?.id; | ||
|
||
if (isNukisMessage) { | ||
return; | ||
} | ||
const prompt = new PromptBuilder() | ||
.withModeratorPrompt() | ||
.withCreatorInfo() | ||
.build(); | ||
|
||
const serverNickname = message.guild?.members.cache.get( | ||
message.author.id, | ||
)?.displayName; | ||
|
||
Logger.info('Querying OpenAI API for moderation...'); | ||
const openAiService = new OpenAiApiService(); | ||
const response = await openAiService.getClient().chat.completions.create({ | ||
max_tokens: 256, | ||
model: 'gpt-4o-2024-08-06', | ||
messages: [ | ||
{ role: 'system', content: prompt }, | ||
{ | ||
role: 'user', | ||
content: `[id:${message.author.id}]:${message.content}`, | ||
name: serverNickname, | ||
}, | ||
], | ||
tool_choice: 'required', | ||
tools: [banTool.definition(), doNothingTool.definition()], | ||
parallel_tool_calls: false, | ||
}); | ||
|
||
const choice = response.choices[0]; | ||
const toolCall = choice.message?.tool_calls?.[0]; | ||
|
||
if (toolCall) { | ||
const toolCallBody = safeParse(toolCall?.function.arguments!); | ||
const toolCallName = toolCall?.function.name; | ||
|
||
if (toolCallName === banTool.name) { | ||
Logger.info('Nuki used the banhammer!'); | ||
Logger.info(`Banhammer arguments: ${JSON.stringify(toolCallBody)}`, { | ||
message: message.content, | ||
}); | ||
|
||
// await message.member?.ban({ | ||
// deleteMessageSeconds: toolCallBody.deleteMessageSeconds, | ||
// }); | ||
|
||
if (toolCallBody.message) { | ||
await message.channel.send(toolCallBody.message); | ||
} | ||
} else { | ||
Logger.info(`Tool call name: ${toolCallName}`); | ||
Logger.info(`Tool call arguments: ${JSON.stringify(toolCallBody)}`); | ||
} | ||
} else { | ||
Logger.info('No tool call was made.'); | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const safeParse = (json: string): any => { | ||
try { | ||
return JSON.parse(json); | ||
} catch (e) { | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Tool } from '.'; | ||
|
||
export const banTool: Tool = { | ||
name: 'ban', | ||
definition: () => ({ | ||
function: { | ||
name: 'ban', | ||
description: | ||
'Confirm that you want to issue a ban for the above message. Only ban people in extremely clear cases, such as sending scam links or outright spam. Delete the last messages when banning someone. NEVER try banning the server admins, moderators, or your creator. If you\'re not sure, use the "do nothing" tool instead.', | ||
parameters: { | ||
type: 'object', | ||
properties: { | ||
deleteMessageSeconds: { | ||
type: 'number', | ||
description: | ||
"Also delete the user's messages from the past x seconds.", | ||
}, | ||
reason: { | ||
type: 'string', | ||
description: | ||
'The reason for the ban. Moderators will see this, so put something useful here.', | ||
}, | ||
message: { | ||
type: 'string', | ||
description: | ||
'Message for the users of the server, informing them that you banned the user.', | ||
}, | ||
}, | ||
required: ['deleteMessageSeconds', 'reason', 'message'], | ||
}, | ||
}, | ||
type: 'function', | ||
}), | ||
execute: async () => {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Tool } from '.'; | ||
|
||
export const doNothingTool: Tool = { | ||
name: 'do-nothing', | ||
definition: () => ({ | ||
function: { | ||
name: 'do-nothing', | ||
description: | ||
"Do nothing. If you deem the message to be fine, use this tool. If you're not sure about the message, leave it to the human moderators, and do nothing.", | ||
parameters: { | ||
type: 'object', | ||
properties: { | ||
reason: { | ||
type: 'string', | ||
description: 'The reason for doing nothing about this message.', | ||
}, | ||
}, | ||
}, | ||
}, | ||
type: 'function', | ||
}), | ||
execute: async () => {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Client } from 'discord.js'; | ||
import { ChatCompletionTool } from 'openai/resources'; | ||
|
||
export type Tool = { | ||
name: string; | ||
definition: () => ChatCompletionTool; | ||
execute: (client: Client) => Promise<void>; | ||
}; |