Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrowM committed Jun 24, 2024
1 parent 8acd1ce commit a8a7ae1
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 61 deletions.
84 changes: 42 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/buttons/buttons/my-positions.button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class MyPositionsButton extends EveryoneButton {
static readonly ID = "my_positions";

customId = MyPositionsButton.ID;
label = "My Positions";
label = "Positions";
style = ButtonStyle.Secondary;

async handle(inter: ButtonInteraction) {
Expand Down
1 change: 0 additions & 1 deletion src/commands/commands/queues.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export class QueuesCommand extends AdminCommand {
store: inter.store,
table: QUEUE_TABLE,
tableLabel: "Queues",
entryLabel: "properties:",
hiddenProperties: ["name"],
queueIdProperty: "id",
valueFormatters: {
Expand Down
12 changes: 11 additions & 1 deletion src/handlers/client.handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
type DMChannel,
type Guild,
type GuildMember,
type GuildMember, type Interaction,
type NonThreadGuildBasedChannel,
type PartialGuildMember,
Role,
Expand All @@ -17,6 +17,7 @@ import { MemberRemovalReason } from "../types/db.types.ts";
import { DisplayUtils } from "../utils/display.utils.ts";
import { MemberUtils } from "../utils/member.utils.ts";
import { QueueUtils } from "../utils/queue.utils.ts";
import { InteractionHandler } from "./interaction.handler.ts";

export namespace ClientHandler {
export function handleGuildDelete(guild: Guild) {
Expand All @@ -28,6 +29,15 @@ export namespace ClientHandler {
}
}

export async function handleInteraction(inter: Interaction) {
if (inter.guild) {
await new InteractionHandler(inter).handle();
}
else if ("reply" in inter) {
await inter.reply("This command can only be used in servers").catch(() => null);
}
}

export async function handleRoleDelete(role: Role) {
const store = new Store(role.guild);
await QueueUtils.updateQueues(
Expand Down
2 changes: 0 additions & 2 deletions src/handlers/interaction.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Color } from "../types/db.types.ts";
import type { Handler } from "../types/handler.types.ts";
import type { AnyInteraction } from "../types/interaction.types.ts";
import { CustomError } from "../utils/error.utils.ts";
import { InteractionUtils } from "../utils/interaction.utils.ts";
import { ERROR_HEADER_LINE } from "../utils/string.utils.ts";
import { AutocompleteHandler } from "./autocomplete.handler.ts";
import { ButtonHandler } from "./button.handler.ts";
Expand All @@ -16,7 +15,6 @@ export class InteractionHandler implements Handler {
private readonly inter: AnyInteraction;

constructor(inter: Interaction) {
InteractionUtils.verifyCommandIsFromGuild(inter);
this.inter = inter as any as AnyInteraction;
this.inter.store = new Store(this.inter.guild, this.inter);
}
Expand Down
3 changes: 1 addition & 2 deletions src/listeners/client.listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { Events } from "discord.js";

import { CLIENT } from "../client/client.ts";
import { ClientHandler } from "../handlers/client.handler.ts";
import { InteractionHandler } from "../handlers/interaction.handler.ts";

export namespace ClientListeners {
export function load() {
CLIENT.on(Events.GuildDelete, ClientHandler.handleGuildDelete);

CLIENT.on(Events.InteractionCreate, inter => new InteractionHandler(inter).handle());
CLIENT.on(Events.InteractionCreate, ClientHandler.handleInteraction);

CLIENT.on(Events.GuildRoleDelete, ClientHandler.handleRoleDelete);

Expand Down
9 changes: 9 additions & 0 deletions src/listeners/node.listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export namespace NodeListeners {

process.on("uncaughtException", async (error) => {
console.error(`Error occurred: ${error}`);
if (error.stack) {
console.error(error.stack);
}
// TODO - should we be exiting to cause a restart?
// await handleExitWithError(error);
});
Expand All @@ -34,6 +37,9 @@ export namespace NodeListeners {
// Optionally, log the stack trace of the promise rejection
promise.catch((error) => {
console.error(error);
if (error.stack) {
console.error(error.stack);
}
});
// TODO - should we be exiting to cause a restart?
// await handleExitWithError();
Expand All @@ -43,6 +49,9 @@ export namespace NodeListeners {
async function handleExitWithError(error?: Error | any) {
if (error) {
console.error(`Error occurred: ${error}`);
if (error.stack) {
console.error(error.stack);
}
}
try {
await flushPendingGuildUpdatesToDB();
Expand Down
7 changes: 0 additions & 7 deletions src/utils/interaction.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
EmbedBuilder,
type GuildTextBasedChannel,
inlineCode,
type Interaction,
type InteractionReplyOptions,
type Message,
PermissionsBitField,
Expand Down Expand Up @@ -79,12 +78,6 @@ export namespace InteractionUtils {
return confirmation?.customId === "confirm";
}

export function verifyCommandIsFromGuild(inter: Interaction) {
if (!inter.guild) {
throw new Error("This command can only be used in servers");
}
}

export async function verifyCanSendMessages(jsChannel: GuildTextBasedChannel) {
function throwPermissionError(permissionName: string) {
throw new CustomError({
Expand Down
1 change: 1 addition & 0 deletions src/utils/member.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export namespace MemberUtils {
}
else if (mentionable instanceof Role) {
const role = await store.jsRole(mentionable.id);
if (!role) continue;
for (const queue of queues.values()) {
for (const jsMember of role.members.values()) {
inserted.push(
Expand Down
15 changes: 10 additions & 5 deletions src/utils/string.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
userMention,
} from "discord.js";
import { SQLiteTable } from "drizzle-orm/sqlite-core";
import { compact, concat, groupBy, isNil } from "lodash-es";
import { compact, concat, groupBy, isEmpty, isNil } from "lodash-es";

import { type DbMember, type DbQueue, type DbSchedule } from "../db/schema.ts";
import type { Store } from "../db/store.ts";
Expand Down Expand Up @@ -161,7 +161,7 @@ export function describeTable<T extends object>(options: {
}

function formatEntryDescriptionLines(entry: T): string[] {
return Object.keys(entry)
return compact(Object.keys(entry)
.filter(property => !hiddenProperties.includes(property))
.map(property => {
const value = (entry as any)[property];
Expand All @@ -175,14 +175,19 @@ export function describeTable<T extends object>(options: {
const formattedValue = formatPropertyValue(entry, property, inlineCode) ?? "";
const formattedOverriddenDefaultValue = (property in table && !isDefaultValue) ? strikethrough(inlineCode(String(defaultValue))) : "";

return ` - ${formattedLabel} = ${formattedValue} ${formattedOverriddenDefaultValue}`.trimEnd();
});
return `${formattedLabel} = ${formattedValue} ${formattedOverriddenDefaultValue}`.trimEnd();
}));
}

function formatEntry(entry: T): string {
const label = entryLabelProperty ? formatPropertyValue(entry, entryLabelProperty, bold) : entryLabel;
const descriptionLines = formatEntryDescriptionLines(entry);
return compact(concat(`- ${label}`, descriptionLines)).join("\n");
if (isEmpty(label)) {
return descriptionLines.map(line => `- ${line}`).join("\n");
}
else {
return concat(`- ${label}`, descriptionLines.map(line => `- ${line}`)).join("\n");
}
}

const embeds = Object.entries(groupBy(entries, queueIdProperty)).map(([queueId, queueEntries]) => {
Expand Down

0 comments on commit a8a7ae1

Please sign in to comment.