Skip to content

Commit

Permalink
commands: add chat command to chat with ChatGPT (#1064)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtraction authored Dec 2, 2023
2 parents a1558a4 + 4850e10 commit fa80d6f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"jsdom": "^22.1.0",
"libsodium-wrappers": "^0.7.13",
"mathjs": "^12.1.0",
"openai": "^4.20.1",
"play-dl": "^1.9.7",
"r6api.js": "^4.4.1",
"undici": "^5.27.2",
Expand Down
10 changes: 10 additions & 0 deletions settings.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ auth: ""
coinMarketCapApiKey: ""
# Required for `apod` command.
nasaApiKey: "DEMO_KEY"
# Required for `chat` command to use the OpenAI's ChatGPT APIs.
# API pricing depends on these values.
# For more details, check https://openai.com/pricing
openai:
apiKey: ""
# If you want to use GPT-4, set `model` to `gpt-4`.
model: "gpt-3.5-turbo"
# Change the `maxTokens` value to set the length of ChatGPT's responses.
# https://platform.openai.com/tokenizer
maxTokens: 100
# Required for `weather` command.
openWeatherMapApiKey: ""
# Required for `movie` and `tv` commands.
Expand Down
55 changes: 55 additions & 0 deletions src/commands/chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*!
* @author TRACTION (iamtraction)
* @copyright 2023
*/
import { ApplicationCommandOptionType, ChatInputCommandInteraction } from "discord.js";
import { Client, Command } from "@bastion/tesseract";
import OpenAI from "openai";

import Settings from "../utils/settings.js";

class ChatCommand extends Command {
constructor() {
super({
name: "chat",
description: "Ask questions or chat with ChatGPT from OpenAI.",
owner: true,
options: [
{
type: ApplicationCommandOptionType.String,
name: "message",
description: "Your message.",
required: true,
},
],
});
}

public async exec(interaction: ChatInputCommandInteraction<"cached">): Promise<void> {
await interaction.deferReply();

const message = interaction.options.getString("message");

const openai = new OpenAI({
apiKey: ((interaction.client as Client).settings as Settings).get("openai").apiKey,
});

const response = await openai.chat.completions.create({
model: ((interaction.client as Client).settings as Settings).get("openai").model || "gpt-3.5-turbo",
messages: [
{
role: "user",
content: message,
},
],
max_tokens: ((interaction.client as Client).settings as Settings).get("openai").maxTokens || 100,
user: interaction.member.id,
});

await interaction.editReply({
content: response.choices[0].message.content,
});
}
}

export { ChatCommand as Command };
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export namespace bastion {
auth?: string;
coinMarketCapApiKey?: string;
nasaApiKey?: string;
openai?: {
apiKey?: string;
model?: string;
maxTokens?: number;
};
openWeatherMapApiKey?: string;
tmdbApiKey?: string;
trackerNetworkApiKey?: string;
Expand Down

0 comments on commit fa80d6f

Please sign in to comment.