Skip to content

Commit

Permalink
Add option to enable/disable Guilded official markdown
Browse files Browse the repository at this point in the history
WS & HTTP req (temporary) header
  • Loading branch information
pakkographic committed Apr 27, 2024
1 parent 3cc28e3 commit cefd8a7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/gateway/WSManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class WSManager extends TypedEmitter<WebsocketEvents> {
#connectTimeout: NodeJS.Timeout | null;
compression: boolean;
#sharedZLib!: Pako.Inflate | Inflate;
isOfficialMarkdownEnabled: boolean;
constructor(client: Client, params: WSManagerParams) {
super();
Object.defineProperties(this, {
Expand Down Expand Up @@ -108,6 +109,7 @@ export class WSManager extends TypedEmitter<WebsocketEvents> {
this.#connectTimeout = null;

this.compression = params.compression ?? false; // not enabled, guilded doesn't support compression for now.
this.isOfficialMarkdownEnabled = client.params.isOfficialMarkdownEnabled ?? params.isOfficialMarkdownEnabled ?? true;
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
Expand Down Expand Up @@ -146,6 +148,7 @@ export class WSManager extends TypedEmitter<WebsocketEvents> {
}

const wsoptions = { headers: { Authorization: `Bearer ${this.params.token}` }, protocol: "HTTPS" };
Object.assign(wsoptions.headers, { "x-guilded-bot-api-use-official-markdown": this.isOfficialMarkdownEnabled }); // temporary header
if (this.replayEventsCondition) Object.assign(wsoptions.headers, { "guilded-last-message-id": this.lastMessageID });
this.ws = new WebSocket(this.proxyURL, wsoptions);

Expand Down Expand Up @@ -431,4 +434,6 @@ export interface WSManagerParams {
client: Client;
/** Compression */
compression?: boolean;
/** Enable official Guilded markdown header to opt-in. */
isOfficialMarkdownEnabled?: boolean;
}
1 change: 1 addition & 0 deletions lib/rest/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class RequestHandler {
const controller = new AbortController();

headers["User-Agent"] = `TouchGuild ${pkgconfig.branch} (${pkgconfig.version}) Node.JS ${pkgconfig.NodeJSVersion}`;
headers["x-guilded-bot-api-use-official-markdown"] = String(this.#manager.client.params.isOfficialMarkdownEnabled ?? true); // temporary header

if (typeof options.auth === "string"){
const tokenArgs = options.auth.split(" ");
Expand Down
8 changes: 8 additions & 0 deletions lib/types/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ export interface ClientOptions {
*/
waitForCaching?: boolean;

/**
* Fixes & improves Guilded API markdown and makes it Commonmark compliant.
*
* Enabled by default, can be disabled to use old version of the Guilded markdown. (if facing issues for example)
*/
isOfficialMarkdownEnabled?: boolean;

/** Set your own limit to how much messages, threads, comments, events.. will be stored in cache before deletion. */
collectionLimits?: {
messages?: number;
threads?: number;
Expand Down

0 comments on commit cefd8a7

Please sign in to comment.