Refactor "Staff Role" Check for Discord API

Status
Not open for further replies.

Foxy

AI
Member
I have some helper functions to check for staff roles. Currently the names are hardcoded, along with the guild ID. I'm trying to come up with a better way to check this.

JavaScript:
// isStaff()

// PARAMS: userId

// RETURN: Boolean value if the user is staff or not

const checkIfStaff = async userId => {

  const guildMember = await getGuildMember(userId);

  if (!guildMember.error) {

    const staffRoles = await getStaffRoles();

    const staff = staffRoles.some(r => {

      return guildMember.roles.includes(r.id);

    });

    if (staff) {

      return true;

    }

    return false;

  }

  return false;

};


// getStaffRoles()

// PARAMS: NA

// RETURN: Staff roles for the server.

// NOTE: Role names are hardcoded. Come up with a better way to check staff role IDs.

const getStaffRoles = async () => {

  const r = await fetch(`https://discordapp.com/api/guilds/${GUILD_ID}/roles`, {

    method: 'GET',

    headers: {

      Authorization: `Bot ${BOT_TOKEN}`

    }

  });

  const guildRoles = await r.json();

  let staffRoleFilter = ['Staff', 'Root', 'Server Admin'];

  const staffRoles = guildRoles.filter(role => {

    return staffRoleFilter.includes(role.name);

  });

  return staffRoles;

};

Ticket created by Slightly Nervous Doggo#2845
 
Last edited by a moderator:

Lyn__

OwO
Server Admin
Arctic
Red
Could connect to a HashiCorp vault server and pull the name and guild ID dynamically.

Another possible solution is storing it in a SQL database and pulling it that way. You could have multiple entries and have multiple staff roles
 

datallboy

datallboy
Server Admin
Fennec
Ruppell
Red
Member
I use the getStaffRoles for both the bot and website (to check if a user can access the staff/admin panel). With the bot I can check where the message (bot command) came from and call getStaffRoles dynamically.

With the admin panel though, I guess I have to store it somewhere in the database along with what roles are "staff". I currently just store the guildID in an environment variable, but this doesn't expand dynamically past 1 server. I never intend on it, but it's not "good practice" to hardcode per say.
 
Status
Not open for further replies.