Discord DCC_GetRoleName
#7

Quote:
Originally Posted by Kwarde
View Post
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.
I have it like this :
pawn Code:
if(!strcmp(command, "!mycommand", true)) {
            for(new  i = 0; i < roleCount; i++) {
                DCC_GetGuildRole(g_Discord_Guild, i, rolename);
                if(rolename == g_Role_Level_1 || rolename == g_Role_Level_2 || rolename == g_Role_Level_3 || rolename == g_Role_Level_4) {
                    //My actions
                    break;
                } else return DCC_SendChannelMessage(g_Discord_Admin_CMD, "```ERROR: You are not a high enough level to use this command```");
            }
        }
or should i break it like :
pawn Code:
if(!strcmp(command, "!mycommand", true)) {
            for(new  i = 0; i < roleCount; i++) {
                DCC_GetGuildRole(g_Discord_Guild, i, rolename);
                if(rolename == g_Role_Level_1 || rolename == g_Role_Level_2 || rolename == g_Role_Level_3 || rolename == g_Role_Level_4) {
                    //My actions
                } else return DCC_SendChannelMessage(g_Discord_Admin_CMD, "```ERROR: You are not a high enough level to use this command```");
                break;
            }
        }
Reply


Messages In This Thread
Discord DCC_GetRoleName - by Filbert - 26.05.2020, 15:03
Re: Discord DCC_GetRoleName - by SharpenBlade - 26.05.2020, 15:10
Re: Discord DCC_GetRoleName - by Filbert - 26.05.2020, 15:42
Re: Discord DCC_GetRoleName - by Filbert - 26.05.2020, 20:01
Re: Discord DCC_GetRoleName - by Filbert - 27.05.2020, 04:27
Re: Discord DCC_GetRoleName - by Kwarde - 27.05.2020, 10:29
Re: Discord DCC_GetRoleName - by Filbert - 27.05.2020, 11:06
Re: Discord DCC_GetRoleName - by Kwarde - 27.05.2020, 21:14

Forum Jump:


Users browsing this thread: 2 Guest(s)