Skip to content

Commit

Permalink
fix getUserRoles crash
Browse files Browse the repository at this point in the history
Happened when roles array are not instantiated because object missing from cache
  • Loading branch information
ArthurBlanchon committed Apr 17, 2024
1 parent f3f2e44 commit b92ac30
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions discord/utils/getUserRoles.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import discordClient from "../../discord/index.js";

export default async function getUserRoles( guildId , userId ) {
export default async function getUserRoles(guildId, userId) {

// Check DiscordJS beginners guide => https://anidiots.guide/understanding/roles/

if ( !guildId || !userId ){
throw new Error(`Parameter required => guildId: ${ !!guildId ? '✅' : '❌' }, userId: ${ !!userId ? '✅' : '❌' }.`)
if (!guildId || !userId) {
throw new Error(`Parameter required => guildId: ${!!guildId ? '✅' : '❌'}, userId: ${!!userId ? '✅' : '❌'}.`)
}

const client = await discordClient()

const guild = client.guilds.cache.get( guildId );
const member = guild?.members.cache.get( userId)

let roles = member?.roles.cache ;
if ( !roles ){

const guild = client.guilds.cache.get(guildId);
const member = guild?.members.cache.get(userId)

let roles = member?.roles.cache;
let userRoles = [];
if (!roles) {
// get role by ID
//roles = guild.roles.cache.get( userId );
//roles = await member.roles.fetch( userId );
} else {
userRoles = roles.map(item => item.name);
}

const userRoles = roles.map( item => item.name );

// get role by name
//let myRole = message.guild.roles.cache.find(role => role.name === "Moderators");

Expand Down

0 comments on commit b92ac30

Please sign in to comment.