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

Commit

Permalink
feat: context menus and private commands
Browse files Browse the repository at this point in the history
Co-authored-by: Marcos Susaña <[email protected]>
  • Loading branch information
MrlolDev and socram03 committed Dec 17, 2023
1 parent 5f837a2 commit 7fc8452
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 31 deletions.
14 changes: 8 additions & 6 deletions src/bot/commands/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ async function buildInfo(
let loadingIndicatorIndex = (await getSettingsValue(user, "general:loadingIndicator")) as number | string;
if (
!loadingIndicatorIndex ||
loadingIndicatorIndex == "default" ||
(typeof loadingIndicatorIndex == "string" && parseInt(loadingIndicatorIndex) >= LOADING_INDICATORS.length)
loadingIndicatorIndex === "default" ||
(typeof loadingIndicatorIndex === "string" && parseInt(loadingIndicatorIndex) >= LOADING_INDICATORS.length)
) {
loadingIndicatorIndex = (await getDefaultValues("general:loadingIndicator")) as number;
}
Expand Down Expand Up @@ -170,8 +170,9 @@ async function buildInfo(
lastUpdate = Date.now();
if (characters.length > 2000) {
await edit({
content: `<${loadingIndicator.emoji.animated ? "a" : ""}:${loadingIndicator.emoji.name}:${loadingIndicator.emoji.id
}>`,
content: `<${loadingIndicator.emoji.animated ? "a" : ""}:${loadingIndicator.emoji.name}:${
loadingIndicator.emoji.id
}>`,
// send result as a file
files: [
{
Expand All @@ -182,8 +183,9 @@ async function buildInfo(
});
} else {
await edit({
content: `${data.result}<${loadingIndicator.emoji.animated ? "a" : ""}:${loadingIndicator.emoji.name}:${loadingIndicator.emoji.id
}>`,
content: `${data.result}<${loadingIndicator.emoji.animated ? "a" : ""}:${loadingIndicator.emoji.name}:${
loadingIndicator.emoji.id
}>`,
});
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/bot/commands/grant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { NoCooldown, createCommand } from "../config/setup.js";

export default createCommand({
body: {
name: "grant",
description: "View information & statistics about the bot",
options: [
{
name: "plan",
type: "SubCommand",
description: "Customize the bot for yourself",
},
{
name: "subscription",
type: "SubCommand",
description: "Customize the bot for the entire server",
},
],
},
cooldown: NoCooldown,
pr: true,
interaction: async ({ interaction }) => {
await interaction.edit({});
},
});
7 changes: 4 additions & 3 deletions src/bot/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Bot from "./bot.js";
import Imagine from "./imagine.js";
import Chat from "./chat.js";
import grant from "./grant.js";
import Imagine from "./imagine.js";
import Premium from "./premium.js";
import Settings from "./settings.js";
import Reset from "./reset.js";
import Settings from "./settings.js";

export const commands = [Bot, Chat, Imagine, Premium, Settings, Reset];
export const commands = [Bot, Chat, Imagine, Premium, Settings, Reset, grant];
17 changes: 17 additions & 0 deletions src/bot/commands/translate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BigString, Bot, ButtonComponent, CreateMessageOptions, MessageComponentTypes } from "@discordeno/bot";
import config from "../../config.js";
import { NoCooldown, buttonInfo, createCommand } from "../config/setup.js";
import { gatewayConfig } from "../index.js";
import { resetConversation } from "../utils/conversations.js";
import { getDefaultValues, getSettingsValue } from "../utils/settings.js";

export default createCommand({
body: {
name: "translate",
type: "Message",
description: "Translate a message",
},
cooldown: NoCooldown,
isPrivate: true,
interaction: async ({ interaction, env }) => {},
});
6 changes: 5 additions & 1 deletion src/bot/config/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export interface CreateCommand extends Omit<Command, "body"> {

export function createCommand({ body, ...base }: CreateCommand): Command {
const { options, ...info } = body;
const payload: Command = { ...base, body: { ...info, type: ApplicationCommandTypes[body.type!] ?? ApplicationCommandTypes.ChatInput } };
const payload: Command = {
...base,
body: { ...info, type: ApplicationCommandTypes[body.type!] ?? ApplicationCommandTypes.ChatInput },
pr: !!base.pr,
};
if (options) payload.body.options = optionsTranformer(options);
return payload;
}
Expand Down
9 changes: 6 additions & 3 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { commands as cmds } from "./commands/index.js";
import { events } from "./events/index.js";
import { handleGatewayMessage } from "./gateway.js";
import type { ButtonResponse, Command } from "./types/index.js";
import { connection, env, redis } from "./utils/db.js";
import { oldSettingsMigration, oldSettingsMigrationBulk } from "./utils/settings.js";
import { connection, redis } from "./utils/db.js";

export const logger = createLogger({ name: "[BOT]" });
let routingKey = "gateway";
if (config.bot.dev) {
Expand Down Expand Up @@ -51,12 +51,15 @@ bot.rest = createRestManager({

export const gatewayConfig = await bot.rest.getGatewayBot();

const applicationCommands: CreateApplicationCommand[] = cmds.map((cmd) => cmd.body);
const privateCommands: CreateApplicationCommand[] = cmds.filter((cmd) => cmd.pr).map((cmd) => cmd.body);
const applicationCommands: CreateApplicationCommand[] = cmds.filter((cmd) => !cmd.pr).map((cmd) => cmd.body);
export const commands = new Map<string, Command>(cmds.map((cmd) => [cmd.body.name, cmd]));
export const buttons = new Map<string, ButtonResponse>(Buttons.map((b) => [b.id, b]));

await bot.rest.upsertGlobalApplicationCommands(applicationCommands).catch((err) => logger.warn(err));

await bot.rest.upsertGuildApplicationCommands(config.bot.privateGuild, privateCommands);

logger.info(`${commands.size} commands deployed`);

bot.transformers.desiredProperties.interaction.data = true;
Expand Down
1 change: 1 addition & 0 deletions src/bot/types/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Command {
body: CreateSlashApplicationCommand;
cooldown: CommandCooldown;
isPrivate?: boolean;
pr?: boolean;
}

export interface CommandContext {
Expand Down
31 changes: 13 additions & 18 deletions src/bot/utils/premium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const buttons: Array<DiscordButtonComponent> = [
},
];
export const requiredPremium = {
content: "",
embeds: [
{
title: "This is a premium feature",
Expand Down Expand Up @@ -69,19 +70,17 @@ export async function generatePremiumEmbed(premiumInfo: {
for (const expense of user.plan.expenses) {
if (expensesFields.length >= 10) break;
expensesFields.push({
name: `${expense.type.slice(0, 1).toUpperCase()}${expense.type.slice(1)} - using \`${
expense.data.model
}\` - $${expense.used.toFixed(5)}`,
name: `${expense.type.slice(0, 1).toUpperCase()}${expense.type.slice(1)} - using \`${expense.data.model
}\` - $${expense.used.toFixed(5)}`,
value: `<t:${Math.floor(expense.time / 1000)}:R>`,
});
}
} else if (premiumInfo.premiumSelection.location === "guild" && guild?.plan) {
for (const expense of guild.plan.expenses) {
if (expensesFields.length >= 10) break;
expensesFields.push({
name: `${expense.type.slice(0, 1).toUpperCase()}${expense.type.slice(1)} - using \`${
expense.data.model
}\` - $${expense.used.toFixed(5)}`,
name: `${expense.type.slice(0, 1).toUpperCase()}${expense.type.slice(1)} - using \`${expense.data.model
}\` - $${expense.used.toFixed(5)}`,
value: `<t:${Math.floor(expense.time / 1000)}>`,
});
}
Expand All @@ -98,19 +97,17 @@ export async function generatePremiumEmbed(premiumInfo: {
for (const charge of user.plan.history) {
if (chargesFields.length >= 10) break;
chargesFields.push({
name: `${charge.type.slice(0, 1).toUpperCase()}${charge.type.slice(1)} ${
charge.gateway ? `- using \`${charge.gateway}\`` : ""
}`,
name: `${charge.type.slice(0, 1).toUpperCase()}${charge.type.slice(1)} ${charge.gateway ? `- using \`${charge.gateway}\`` : ""
}`,
value: `$${charge.amount.toFixed(2)} - <t:${Math.floor(charge.time / 1000)}>`,
});
}
} else if (premiumInfo.premiumSelection.location === "guild" && guild?.plan) {
for (const charge of guild.plan.history) {
if (chargesFields.length >= 10) break;
chargesFields.push({
name: `${charge.type.slice(0, 1).toUpperCase()}${charge.type.slice(1)} ${
charge.gateway ? `- using \`${charge.gateway}\`` : ""
}`,
name: `${charge.type.slice(0, 1).toUpperCase()}${charge.type.slice(1)} ${charge.gateway ? `- using \`${charge.gateway}\`` : ""
}`,
value: `$${charge.amount.toFixed(2)} - <t:${Math.floor(charge.time / 1000)}>`,
});
}
Expand All @@ -126,13 +123,11 @@ export async function generatePremiumEmbed(premiumInfo: {
// LAST EMBED
let description = "";
if (premiumInfo.premiumSelection.location === "user" && user.plan) {
description = `**$${user.plan?.used.toFixed(2)}**\`${generateProgressBar(user.plan.total, user.plan.used)}\`**$${
user.plan?.total
}**`;
description = `**$${user.plan?.used.toFixed(2)}**\`${generateProgressBar(user.plan.total, user.plan.used)}\`**$${user.plan?.total
}**`;
} else if (premiumInfo.premiumSelection.location === "guild" && guild?.plan) {
description = `**$${guild.plan?.used.toFixed(2)}**\`${generateProgressBar(guild.plan.total, guild.plan.used)}\`**$${
guild.plan?.total
}**`;
description = `**$${guild.plan?.used.toFixed(2)}**\`${generateProgressBar(guild.plan.total, guild.plan.used)}\`**$${guild.plan?.total
}**`;
}
embeds.push({
title: "Your pay-as-you-go plan 📊",
Expand Down
1 change: 1 addition & 0 deletions src/config.example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default {
intents: Intents.DirectMessages | Intents.GuildMessages,
permissions: 0,
dev: true,
privateGuild: "",
status: [
{
since: null,
Expand Down

0 comments on commit 7fc8452

Please sign in to comment.