Skip to content

Commit

Permalink
send pac3 server errors to the pac discord as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Techbot121 authored and Meta Construct committed Dec 11, 2023
1 parent 91f0fc2 commit d97b1c8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/services/gamebridge/GameServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default class GameServer {
discord: DiscordClient;
discordWH: WebhookClient;
discordEWH: WebhookClient;
discordPEWH: WebhookClient;
gamemode: {
folderName: string;
name: string;
Expand Down Expand Up @@ -67,6 +68,9 @@ export default class GameServer {
this.discordEWH = new WebhookClient({
url: bridge.config.errorWebhookUrl,
});
this.discordPEWH = new WebhookClient({
url: bridge.config.pacErrorWebhookUrl,
});

this.discord.run(this.config.discordToken);

Expand Down
26 changes: 19 additions & 7 deletions app/services/gamebridge/payloads/ErrorPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as requestSchema from "./structures/ErrorRequest.json";
import * as responseSchema from "./structures/ErrorResponse.json";
import { APIEmbed } from "discord.js";
import { ErrorRequest, ErrorResponse } from "./structures";
import { GMOD_PATH_MATCH, getOrFetchGmodFile } from "@/utils";
import { GameServer } from "..";
import { getOrFetchGmodFile } from "@/utils";
import Payload from "./Payload";
import dayjs from "dayjs";

Expand All @@ -17,19 +17,23 @@ export default class ErrorPayload extends Payload {

static async handle(payload: ErrorRequest, server: GameServer): Promise<void> {
super.handle(payload, server);
const { discordEWH } = server;
const { discordEWH, discordPEWH } = server;

const { hook_error } = payload.data;

if (hook_error.name.includes("@repl_") || this.lastError === hook_error) return;

const webhook = discordEWH;
const pacWebhook = discordPEWH;

const lines = hook_error.errormsg.split(/\r?\n/);
const err = lines[0];
const [path, linenr] = err.split(":", 2);
const stack = lines.splice(2).map((l, i) => `${i + 1}. ${l}`);
const file = await getOrFetchGmodFile(path + ":" + linenr);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [, fpath, addon, filename, ext, linenos, linenoe] =
new RegExp(GMOD_PATH_MATCH).exec(<string>path) || [];
const embeds: APIEmbed[] = [];
const embed = {
title: hook_error.name,
Expand Down Expand Up @@ -61,11 +65,19 @@ export default class ErrorPayload extends Payload {
embeds.push(embed);
}
this.lastError = hook_error;
webhook.send({
allowedMentions: { parse: [] },
content: `**${hook_error.identifier} Hook Failed!\n${err}**`,
embeds: embeds,
});
if (addon === "pac3") {
pacWebhook.send({
allowedMentions: { parse: [] },
content: `**${hook_error.identifier} Hook Failed!\n${err}**`,
embeds: embeds,
});
} else {
webhook.send({
allowedMentions: { parse: [] },
content: `**${hook_error.identifier} Hook Failed!\n${err}**`,
embeds: embeds,
});
}
}

static async send(payload: ErrorResponse, server: GameServer): Promise<void> {
Expand Down
5 changes: 3 additions & 2 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ export const AddonURIS = {
};

const LOOKUP_PATH = webappconfig.lookupPath;
const PATH_MATCH =
export const GMOD_PATH_MATCH =
/^(?<path>(?:lua|gamemodes)\/(?<addon>[-_.A-Za-z0-9]+?|)?(?:\/.*)?\/(?<filename>[-_.A-Za-z0-9]+)\.(?<ext>[a-z]*))?(?::-?(?<linenos>\d+)-?(?<linenoe>\d+)?)?$/g;

export const getOrFetchGmodFile = async (path: PathLike) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [, fpath, addon, filename, ext, linenos, linenoe] =
new RegExp(PATH_MATCH).exec(<string>path) || [];
new RegExp(GMOD_PATH_MATCH).exec(<string>path) || [];
const fullpath = LOOKUP_PATH + fpath;

if (await exists(fullpath)) {
Expand Down

0 comments on commit d97b1c8

Please sign in to comment.