Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
feat: help and chatbot commands
Browse files Browse the repository at this point in the history
  • Loading branch information
janleigh committed Jul 6, 2023
1 parent 8ff4867 commit 3399a12
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 13 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"husky": "husky install .github/husky"
},
"devDependencies": {
"@types/node-fetch": "^2.6.4",
"husky": "^8.0.3",
"lint-staged": "^13.2.0",
"prettier": "^2.8.2",
Expand All @@ -39,7 +40,8 @@
"discord.js": "^14.7.1",
"dotenv": "^16.0.3",
"eslint": "latest",
"eslint-plugin-prettier": "^4.2.1"
"eslint-plugin-prettier": "^4.2.1",
"node-fetch": "2"
},
"lint-staged": {
"src/**/*.ts": [
Expand Down
71 changes: 71 additions & 0 deletions src/commands/core/HelpCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* eslint-disable indent */
import { ChatInputCommand, Command, RegisterBehavior } from "@sapphire/framework";
import { BaseEmbedBuilder, githubBtn, inviteBtn } from "../../libraries/structures/components";
import { parseEmojiByID } from "../../libraries/utils/common/parsers";
import { ComponentType } from "discord.js";
import { resolveKey } from "@sapphire/plugin-i18next";
import { LanguageKeys } from "../../libraries/language";

export class HelpCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
name: "help",
fullCategory: ["Core"]
});
}

public override registerApplicationCommands(registry: ChatInputCommand.Registry) {
registry.registerChatInputCommand(
(builder) =>
builder.setName("help").setDescription("Display the help menu or help for a specific command."),
{ behaviorWhenNotIdentical: RegisterBehavior.Overwrite }
);
}

public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
const embed = new BaseEmbedBuilder();
const transparent = parseEmojiByID("1126301870210695239");

embed.setAuthor({
name: (this.container.client.user?.username ?? "") + " Help Menu",
iconURL: this.container.client.user?.displayAvatarURL({ size: 1024 })
});

// TODO: Make this configurable.
embed.addFields(
{
name: "— **CORE**",
value: `
${transparent} </help:1126303427203448938> - ${await resolveKey(
interaction,
LanguageKeys.Commands.Help.HELP_HELPCMD_DESCRIPTION
)}
${transparent} </ping:1125599325431533578> - ${await resolveKey(
interaction,
LanguageKeys.Commands.Help.HELP_PINGCMD_DESCRIPTION
)}
`
},
{
name: "— **ENTERTAINMENT**",
value: `
${transparent} </chat:1126124768576417892> - ${await resolveKey(
interaction,
LanguageKeys.Commands.Help.HELP_CHATCMD_DESCRIPTION
)}
`
}
);

return interaction.reply({
embeds: [embed],
components: [
{
type: ComponentType.ActionRow,
components: [inviteBtn, githubBtn]
}
]
});
}
}
1 change: 1 addition & 0 deletions src/commands/core/PingCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class PingCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
name: "ping",
fullCategory: ["Core"]
});
}
Expand Down
45 changes: 34 additions & 11 deletions src/commands/developer/EvaluateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,39 @@ export class EvalCommand extends Command {
embed.isErrorEmbed().setDescription("An error occurred. Check the console for more details.");
}

return interaction.reply({
content: content,
embeds: embed.data.description ? [embed] : [],
ephemeral: interaction.options.getBoolean("silent") ?? false,
components: [
{
type: ComponentType.ActionRow,
components: [deleteBtn]
}
]
});
if (content.length < 2000) {
return interaction.reply({
content: content,
embeds: embed.data.description ? [embed] : [],
ephemeral: interaction.options.getBoolean("silent") ?? false,
components: [
{
type: ComponentType.ActionRow,
components: [deleteBtn]
}
]
});
} else {
this.container.logger.info("!! EVAL COMPLETE !!");
console.log(content.replaceAll("```xl", "").replaceAll("```", ""));

embed
.isErrorEmbed()
.setDescription(
"The output was too long to be sent as a message. Output has been logged to the console."
);

return interaction.reply({
content: "",
embeds: embed.data.description ? [embed] : [],
ephemeral: interaction.options.getBoolean("silent") ?? false,
components: [
{
type: ComponentType.ActionRow,
components: [deleteBtn]
}
]
});
}
}
}
56 changes: 56 additions & 0 deletions src/commands/entertainment/ChatCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { ChatInputCommand, Command, RegisterBehavior } from "@sapphire/framework";
import { fetchResponseFromAI } from "../../libraries/utils/common/fetch";
import { BaseEmbedBuilder } from "../../libraries/structures/components";
import { resolveKey } from "@sapphire/plugin-i18next";
import { LanguageKeys } from "../../libraries/language";

export class ChatCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
name: "chat",
fullCategory: ["Entertainment"]
});
}

public override registerApplicationCommands(registry: ChatInputCommand.Registry) {
registry.registerChatInputCommand(
(builder) =>
builder
.setName("chat")
.setDescription("Chat to Akari.")
.addStringOption((option) =>
option.setName("message").setDescription("The message you want to chat.").setRequired(true)
)
.addBooleanOption((option) =>
option
.setName("silent")
.setDescription("Whether to send the reply public or not.")
.setRequired(false)
),
{ behaviorWhenNotIdentical: RegisterBehavior.Overwrite }
);
}

public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
const message = interaction.options.getString("message");
const silent = interaction.options.getBoolean("silent") ?? false;
const embed = new BaseEmbedBuilder();
const response: { role: string; content: string } = await fetchResponseFromAI(
String(message),
interaction.user.id
);

await interaction.deferReply({ ephemeral: silent });

if (response.content) {
await new Promise((resolve) => setTimeout(resolve, 2000));
return interaction.editReply({ content: response.content });
}

embed.isErrorEmbed().setDescription(await resolveKey(interaction, LanguageKeys.Commands.Chat.CHAT_FAILED));
return interaction.editReply({
embeds: [embed]
});
}
}
3 changes: 3 additions & 0 deletions src/languages/en-US/commands/chat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CHAT_FAILED": "An error occurred while fetching response from AI. Please try again."
}
5 changes: 5 additions & 0 deletions src/languages/en-US/commands/help.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"HELP_HELPCMD_DESCRIPTION": "Display the help menu or help for a specific command.",
"HELP_PINGCMD_DESCRIPTION": "Check if the bot is alive.",
"HELP_CHATCMD_DESCRIPTION": "Chat to Akari."
}
1 change: 1 addition & 0 deletions src/libraries/language/keys/commands/chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CHAT_FAILED = "commands/chat:CHAT_FAILED";
3 changes: 3 additions & 0 deletions src/libraries/language/keys/commands/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const HELP_HELPCMD_DESCRIPTION = "commands/help:HELP_HELPCMD_DESCRIPTION";
export const HELP_PINGCMD_DESCRIPTION = "commands/help:HELP_PINGCMD_DESCRIPTION";
export const HELP_CHATCMD_DESCRIPTION = "commands/help:HELP_CHATCMD_DESCRIPTION";
2 changes: 2 additions & 0 deletions src/libraries/language/keys/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * as Chat from "./chat";
export * as Help from "./help";
export * as Ping from "./ping";
2 changes: 2 additions & 0 deletions src/libraries/structures/components/BaseEmbedBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export class BaseEmbedBuilder extends EmbedBuilder {
*/
public constructor() {
super();

this.setColor("#fdd59a");
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/libraries/structures/components/Buttons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,17 @@ export const deleteBtn = new ButtonBuilder()
.setCustomId("evalDelete")
.setLabel("Delete Output")
.setStyle(ButtonStyle.Danger);

export const inviteBtn = new ButtonBuilder()
.setLabel("Invite Akari")
.setEmoji("1126314576544280686")
.setURL(
"https://discord.com/oauth2/authorize?client_id=1125402976845049937&permissions=1237423877622&scope=bot%20applications.commands"
)
.setStyle(ButtonStyle.Link);

export const githubBtn = new ButtonBuilder()
.setLabel("GitHub")
.setEmoji("1126316165208211476")
.setURL("https://github.com/janleigh/akari")
.setStyle(ButtonStyle.Link);
7 changes: 7 additions & 0 deletions src/libraries/utils/common/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fetch from "node-fetch";

// Closed source ;) No stealing
export const fetchResponseFromAI = async (message: string, uid: string) => {
const response = await fetch(`http://localhost:8080/response?msg=${message}&uid=${uid}`);
return response.json();
};
3 changes: 2 additions & 1 deletion src/libraries/utils/common/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFileSync } from "fs";
import { container } from "@sapphire/pieces";
import { GuildEmoji } from "discord.js";

export const parseBarredUserIDByFile = (inputFile: string): string[] => {
const barredUsers: string[] = [];
Expand All @@ -13,6 +14,6 @@ export const parseBarredUserIDByFile = (inputFile: string): string[] => {
return barredUsers;
};

export const parseEmojiByID = (emojiID: string) => {
export const parseEmojiByID = (emojiID: string): GuildEmoji | undefined => {
return container.client.emojis.cache.get(emojiID);
};
1 change: 1 addition & 0 deletions src/libraries/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * as fetch from "./common/fetch";
export * as parsers from "./common/parsers";
export * as text from "./common/text";
Loading

0 comments on commit 3399a12

Please sign in to comment.