Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commands: add chat command to chat with ChatGPT #1064

Merged
merged 4 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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