Skip to content

Commit

Permalink
Properly encode audit log/ban reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
abalabahaha committed Apr 1, 2021
1 parent 692febf commit 6771b3f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class Client extends EventEmitter {
* @arg {Object} [options.rest] Options for the REST request handler
* @arg {Object} [options.rest.agent] A HTTPS Agent used to proxy requests
* @arg {String} [options.rest.baseURL] The base URL to use for API requests. Defaults to `/api/v${REST_VERSION}`
* @arg {Boolean} [options.rest.decodeReasons=true] [DEPRECATED] Whether reasons should be decoded with `decodeURIComponent()` when making REST requests. This is true by default to mirror pre-0.15.0 behavior (where reasons were expected to be URI-encoded), and should be set to false once your bot code stops. Reasons will no longer be decoded in the future
* @arg {Boolean} [options.rest.disableLatencyCompensation=false] Whether to disable the built-in latency compensator or not
* @arg {String} [options.rest.domain="discord.com"] The domain to use for API requests
* @arg {Number} [options.rest.latencyThreshold=30000] The average request latency at which Eris will start emitting latency errors
Expand Down
15 changes: 14 additions & 1 deletion lib/rest/RequestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class RequestHandler {
this.options = options = Object.assign({
agent: client.options.agent || null,
baseURL: Endpoints.BASE_URL,
decodeReasons: true,
disableLatencyCompensation: false,
domain: "discord.com",
latencyThreshold: client.options.latencyThreshold || 30000,
Expand Down Expand Up @@ -92,9 +93,21 @@ class RequestHandler {
headers.Authorization = this._client._token;
}
if(body && body.reason) { // Audit log reason sniping
headers["X-Audit-Log-Reason"] = body.reason;
let unencodedReason = body.reason;
if(this.options.decodeReasons) {
try {
if(unencodedReason.includes("%") && !unencodedReason.includes(" ")) {
unencodedReason = decodeURIComponent(unencodedReason);
}
} catch(err) {
this._client.emit("error", err);
}
}
headers["X-Audit-Log-Reason"] = encodeURIComponent(unencodedReason);
if((method !== "PUT" || !url.includes("/bans")) && (method !== "POST" || !url.includes("/prune"))) {
delete body.reason;
} else {
body.reason = unencodedReason;
}
}
if(file) {
Expand Down

0 comments on commit 6771b3f

Please sign in to comment.