#Issues with setPosition()

10 messages · Page 1 of 1 (latest)

sharp mural
#

Set position can cause discord api error even without inputting an invalid position.

The issue lies with https://github.com/discordjs/discord.js/blob/b2ec865765bf94181473864a627fb63ea8173fd3/packages/discord.js/src/util/Util.js#L324-L341

function moveElementInArray(array, element, newIndex, offset = false) {
  const index = array.indexOf(element);
  newIndex = (offset ? index : 0) + newIndex;
  if (newIndex > -1 && newIndex < array.length) {
    const removedElement = array.splice(index, 1)[0];
    array.splice(newIndex, 0, removedElement);
  }
  return array.indexOf(element);
}

The problem arises with index. Sometimes index will be -1 even if the role exists (possibly because object was updated). This in turn moves the last element (index -1) which is always either the bots role or a role with higher permission (which you can't move neither) throwing an api error.

GitHub

A powerful JavaScript library for interacting with the Discord API - discord.js/Util.js at b2ec865765bf94181473864a627fb63ea8173fd3 · discordjs/discord.js

#

Possible fix would be getting index by Array.prototype.findIndex() and matching with id

fresh torrent
#

Could you provide some reproduction steps?

split cobalt
#

If an object updates, all of its ref also gets updates. So I don't think it'll return -1.

sharp mural
fresh torrent
#

Ught, just provide a simple test code, can be?

#

If not, ok, can be a repo

sharp mural
#

but it just occurred to me it might be because im reassigning the roles

#

i didnt realise the object returned after edit is a cloned object