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.