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

v10.14 #1066

Merged
merged 13 commits into from
Dec 3, 2023
2 changes: 1 addition & 1 deletion commands.json

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bastion",
"version": "10.13.0",
"version": "10.14.0",
"description": "Get an enhanced Discord experience!",
"type": "module",
"homepage": "https://bastion.traction.one",
Expand All @@ -22,12 +22,12 @@
"@types/express": "^4.17.21",
"@types/gamedig": "^4.0.5",
"@types/http-errors": "^2.0.4",
"@types/jsdom": "^21.1.5",
"@types/node": "^20.9.2",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.11.0",
"eslint": "^8.54.0",
"typescript": "^5.2.2"
"@types/jsdom": "^21.1.6",
"@types/node": "^20.10.2",
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"eslint": "^8.55.0",
"typescript": "^5.3.2"
},
"dependencies": {
"@bastion/tesseract": "^5.1.0",
Expand All @@ -36,13 +36,14 @@
"discord-rpc": "^4.0.1",
"dotenv": "^16.3.1",
"emoji-regex": "^10.3.0",
"gamedig": "^4.1.0",
"jsdom": "^22.1.0",
"gamedig": "^4.2.0",
"jsdom": "^23.0.1",
"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",
"undici": "^5.28.2",
"ytdl-core": "^4.11.5",
"ytpl": "^2.3.0"
},
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 };
74 changes: 74 additions & 0 deletions src/commands/image/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*!
* @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 ImageGenerateCommand extends Command {
constructor() {
super({
name: "generate",
description: "Generate an image with DALL-E from OpenAI.",
owner: true,
options: [
{
type: ApplicationCommandOptionType.String,
name: "prompt",
description: "A description of the desired image.",
required: true,
},
{
type: ApplicationCommandOptionType.String,
name: "size",
description: "The size of the generated image.",
choices: [
{
name: "Square",
value: "1024x1024",
},
{
name: "Portrait",
value: "1024x1792",
},
{
name: "Landscape",
value: "1792x1024",
},
],
},
],
});
}

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

const prompt = interaction.options.getString("prompt");
const size = interaction.options.getString("size") as "1024x1024" | "1024x1792" | "1792x1024" || "1024x1024";

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

const response = await openai.images.generate({
model: "dall-e-3",
prompt: prompt,
response_format: "url",
size: size,
user: interaction.member.id,
});

await interaction.editReply({
content: response.data[0].revised_prompt,
files: [{
attachment: response.data[0].url,
}],
});
}
}

export { ImageGenerateCommand as Command };
8 changes: 5 additions & 3 deletions src/schedulers/liveStreams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import GuildModel from "../models/Guild.js";
import memcache from "../utils/memcache.js";
import * as requests from "../utils/requests.js";
import { COLORS } from "../utils/constants.js";
import { TWITCH_CHANNEL } from "../utils/regex.js";
import Settings from "../utils/settings.js";
import { TwitchStream } from "../types.js";

Expand Down Expand Up @@ -40,15 +41,16 @@ class LiveStreamNotificationScheduler extends Scheduler {

for (const guild of guildDocuments) {
// twitch streams
if (guild.twitchNotificationChannel && this.client.guilds.cache.get(guild.id).channels.cache.has(guild.twitchNotificationChannel) && guild.twitchNotificationUsers?.length) {
const twitchNotificationUsers = guild.twitchNotificationUsers.filter(u => TWITCH_CHANNEL.test(u));
if (guild.twitchNotificationChannel && this.client.guilds.cache.get(guild.id).channels.cache.has(guild.twitchNotificationChannel) && twitchNotificationUsers?.length) {
// get current live streams
const { body, statusCode } = await requests.get("https://api.twitch.tv/helix/streams/?user_login=" + guild.twitchNotificationUsers.join("&user_login="), {
const { body, statusCode } = await requests.get("https://api.twitch.tv/helix/streams/?user_login=" + twitchNotificationUsers.join("&user_login="), {
"authorization": "Bearer " + (this.client.settings as Settings).get("twitch").accessToken,
"client-id": (this.client.settings as Settings).get("twitch").clientId,
});

if (statusCode >= 400) {
Logger.error(await body.json());
return Logger.error(await body.json());
}

const streams: TwitchStream[] = (await body.json())?.["data"] || [];
Expand Down
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
Loading