View Single Post
Old 27/05/2020, 04:27 AM   #6
Huge Clucker
Join Date: May 2016
Posts: 251
Reputation: 1
Default Re: Discord DCC_GetRoleName

Originally Posted by killermvc View Post
pawn Code:
new DCC_Role:role, rolename[128];//<<<<<<<<<<<<<<<<<<<<
    DCC_GetRoleName(role, rolename, 128);//<<<<<<<<<<<<<<<<<<<<
No this isn't correct.

DCC_GetRoleName will copy the name of the specified role (the variable named 'role') to the array you pass it (rolename in this case) howewer, as you just created the variable 'role' it currently holds the value 'DCC_Role:0', which if you check is an invalid role
pawn Code:
You need to retrieve the role(s) of the author of the message, for this, discord-connector has the function
pawn Code:
'native DCC_GetGuildMemberRole(DCC_Guild:guild, DCC_User:user, offset, &DCC_Role:role);'
A bot (and a user) can be in many guilds (discord servers) so you need to specify for which server you want to get that user's roles. To retrieve your guild you may use:
pawn Code:
native DCC_Guild:DCC_FindGuildById(const guild_id[]);
This will return the guild that you should use to call 'DCC_GetGuildMemberRole'
Now, a user may have many roles not just one, so you need to retrieves all of the roles and compare each with the allowed roles for the command.
This is what the third parameter of 'DCC_GetGuildMemberRole' ('offset') is for. If offset is 0 you will get the first role of this user, if is 1 you will get the second role, etc. To know how many roles the user has use:
pawn Code:
native DCC_GetGuildMemberRoleCount(DCC_Guild:guild, DCC_User:user, &count);
Then you just need to loop through each of the user's role
Finally the code would look something like this:
pawn Code:
//optionally you may prefer to make 'DCC_Guild:guild' global and call 'DCC_FindGuildById' in OnGamemodeInit
//so that it only gets called once and not each time someone uses a command
    DCC_Guild:guild = DCC_Guild:DCC_FindGuildById("yourGuildId"),

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
            } else return DCC_SendChannelMessage(g_Discord_Admin_CMD, "```ERROR: You are not a high enough level to use this command```");

    //other commands
O ma gad.. i just jumped out of my bed knowing that someone just replied my topic xd... Thanks bud gonna test it

EDIT: I got
warning 225: unreachable code
for every breaks.. how to fix that?

Last edited by Filbert; 27/05/2020 at 05:22 AM.
Filbert is offline   Reply With Quote