Skip to content

Instantly share code, notes, and snippets.

@yuta0801
Created May 2, 2020 20:00
Show Gist options
  • Save yuta0801/3d834df6c51592911137e44971682554 to your computer and use it in GitHub Desktop.
Save yuta0801/3d834df6c51592911137e44971682554 to your computer and use it in GitHub Desktop.
Complement stack trace of DiscordAPIError in Discord.js
const Discord = require('discord.js')
const client = new Discord.Client()
{
const prototype = Object.getPrototypeOf(client.rest)
const original = Object.getOwnPropertyDescriptor(prototype, 'api').get
Object.defineProperty(prototype, 'api', {
get() {
const builder = original.apply(this)
return new Proxy(builder, {
get(target, name) {
const result = Reflect.get(target, name)
if (result.inspect) return new Proxy(result, this)
return (...args) => result(...args).catch(err => {
Error.captureStackTrace(err)
throw err
})
},
apply(target, _, args) {
return new Proxy(Reflect.apply(target, _, args), this)
}
})
}
})
}
client.on('message', () => {
handler().catch(err => console.error(err))
})
client.login()
const handler = async () => {
if (true) await command()
}
const command = async () => {
await client.channels.cache.get('706188750354645073').delete()
}
DiscordAPIError: Missing Permissions
at /index.js:16:19
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async command (/index.js:39:3)
at async handler (/index.js:35:13) {
method: 'delete',
path: '/channels/706188750354645073',
code: 50013,
httpStatus: 403
}
@yuta0801
Copy link
Author

yuta0801 commented May 2, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment