View Single Post
Old 27/05/2020, 10:29 AM   #7
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,862
Reputation: 1693
Default Re: Discord DCC_GetRoleName

pawn Code:
DCC_GetGuildMemberRoleCount(guild, author, roleCount);
if(channel == g_Discord_Admin_CMD) {
    if(!strcmp(command, "!mycommand", true)) {
        //Loop through all roles for this user
        for(new  i = 0; i < roleCount; i++) {
            DCC_GetGuildRole(guild, i, role);
            //you can compare 'role' directly to 'g_Role_Level_X'
            if(role == g_Role_Level_1 || role == g_Role_Level_2 || role == g_Role_Level_3 || role == g_Role_Level_4) {
                    //My actions
            //if you don't break here the command may execute many times. for example if user has both g_Role_Level_1 and g_Role_Level_3
            break;
            } else return DCC_SendChannelMessage(g_Discord_Admin_CMD, "```ERROR: You are not a high enough level to use this command```");
        }
    }

    //other commands
}
Let's say there are 5 roles. Level 0 up to 4. Someone with level 0 uses this.
pawn Code:
for (new i; i < roleCount; i++)
{
    //Loop i=0:
    Get user role (role = 0)
    Role is not 1,2,3 or 4.
    Send message: ERROR: You are not a high enough level to use this command.
    Break entire function (not just the loop), return return value of DCC_SendChannelMessage();
}
Now let's say they have level 2.
pawn Code:
for (new i; i < roleCount; i++)
{
    //Loop i=0:
    Get user role (role = 0) //Assuming this role is read before any other role.
    Role is not 1,2,3 or 4
    Send message: ERROR: You are not a high enough level to use this command.
    Break entire function (not just the loop), return return value of DCC_SendChannelMessage();
}

You might wanna use DCC_HasGuildMemberRole(DCC_Guild:guild, DCC_User:user, DCC_Role:role, &bool:has_role); instead. This piece of code (as you can see) is
1- Inefficient because it could loop through all the roles every time someone uses that commands.
2- As seen in a above example, if their first read role is not level 1-4, it would send the error aswell.


As of the unreachable code; make sure you have no code after using return/break/continue in the same level.
__________________
When the opportunity presents itself to flip-a da table, uh, you flip-a da table.

Discord: Kwarde#8009
Kwarde is offline   Reply With Quote