discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

Creating channel permissions for a newly created role works only some of the time #4657

Closed martgaedtgens closed 4 years ago

martgaedtgens commented 4 years ago

I have a command that creates a new role that can be assigned to users you want to mute in the server. Now I want this role to automatically have the permissions "send_messages" disabled in every text channel. The code that I have now does work but it's not consistent. It doesn't throw any errors but the permission for the role doesn't show up in the channel's permissions tab.

image

image

It always shows that the overwrites were created but then in the channel it isn't always the case. I'm not sure what could be causing it to only work some of the time.

almostSouji commented 4 years ago

For then-callbacks to actually work as expected you have to provide (at least one) callable function to it as argument. Currently the console.log("created...") will resolve to undefined immediately (the return value of console.log) and before the promise has a chance to resolve or reject.

Feel free to check out some resources on the topic below:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promiseshttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/thenhttps://discordjs.guide/additional-info/async-await.htmlhttps://javascript.info/async-await

martgaedtgens commented 4 years ago

I've changed the then-callback to have a callable function as argument. However, the results haven't changed; the promise channel.createOverwrite does not give an error but the overwrites still do not always show up in the channels. Sometimes it works and the overwrites show in all 3 channels, sometimes it's none, and other times its only 1 or 2 but not all 3.

I feel like the problem lies with the promise and not so much with the callback as it is just simple logging of success or failure.

almostSouji commented 4 years ago

I feel like the problem lies with the promise and not so much with the callback as it is just simple logging of success or failure.

The proper handling of the then callback ensures that each promise resolves properly, enables you to log the results and make sure everything is in order and as you expect (+ allows points of debugging if that is not the case).

In the opening post you provide the logs from the (presumable) then callback as evidence that the call goes through. However this is not the case. Since the console.log gets evaluated and executed prior to the call being completed it prevents this functionality.

If the promise actually resolves there is no reason why an update would not apply on discords end, as the promise only resolves after the PUT call has gone through and accordingly rejects if anything stops it from being applied (for example due to missing permissions.)

The only case where "nothing" happening is a possible outcome is really if the API call is delayed due to rate limit back offs (which will only occur if you execute this a lot of times in a short time frame - maybe due to testing the command right now)

martgaedtgens commented 4 years ago

I think I got it to work, it might've been an API issue after all. After making some changes it doesn't seem to malfunction anymore. Thank you so much for your help, I appreciate it a lot!