forward DCC_OnMessageCreate(DCC_Message:message);
public DCC_OnMessageCreate(DCC_Message:message)
{
new realMsg[100];
DCC_GetMessageContent(message, realMsg, 100);
new bool:IsBot;
new DCC_Channel:channel;
DCC_GetMessageChannel(message, channel);
new DCC_User:author;
DCC_GetMessageAuthor(message, author);
//==================I'm confused with this==================================
new DCC_Role:role, rolename[128];//<<<<<<<<<<<<<<<<<<<<
DCC_GetRoleName(role, rolename, 128);//<<<<<<<<<<<<<<<<<<<<
//==========================================================================
DCC_IsUserBot(author, IsBot);
if(IsBot) return 1;
new discordstr[256];
new command[32], params[128];
DCC_GetMessageContent(message, discordstr);
sscanf(discordstr, "s[32]s[128]", command, params);
if(channel == g_Discord_Admin_CMD)
{
if(!strcmp(command, "!mycommand", true)) {
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```");
}
}
return 1;
}
error 033: array must be indexed (variable "rolename")
if(!strcmp(rolename, g_Role_Level_1, false) |
From what I see, your variable name rolename is an array. So the check should be like this:
|
new DCC_Role:role, rolename[128];//<<<<<<<<<<<<<<<<<<<<
DCC_GetRoleName(role, rolename, 128);//<<<<<<<<<<<<<<<<<<<<
new rolename[128];
DCC_GetRoleName(author, rolename, 128);
if(!strcmp(rolename, g_Role_Level_1, false)) {
//My action
}
error 035: argument type mismatch (argument 2)
new DCC_Role:g_Role_Level_1;
new DCC_Role:g_Role_Level_2;
new DCC_Role:g_Role_Level_3;
new DCC_Role:g_Role_Level_4;
new DCC_Role:g_Role_Level_5;
new DCC_Role:g_Role_Level_6;
new DCC_Role:g_Role_Level_7;
new DCC_Role:g_Role_Level_8;
g_Role_Level_1 = DCC_FindRoleById("myroleid");
g_Role_Level_2 = DCC_FindRoleById("myroleid");
g_Role_Level_3 = DCC_FindRoleById("myroleid");
g_Role_Level_4 = DCC_FindRoleById("myroleid");
g_Role_Level_5 = DCC_FindRoleById("myroleid");
g_Role_Level_6 = DCC_FindRoleById("myroleid");
g_Role_Level_7 = DCC_FindRoleById("myroleid");
g_Role_Level_8 = DCC_FindRoleById("myroleid");
pawn Code:
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 discord-connector.inc is an invalid role pawn Code:
pawn Code:
pawn Code:
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:
Finally the code would look something like this: pawn Code:
|
warning 225: unreachable 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
}
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();
}
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();
}
pawn Code:
pawn Code:
pawn Code:
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. |
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```");
}
}
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;
}
}
main()
{
for (new i; i <= 2; i++)
{
if (!i) //Aka "if (i == 0)". !variable equals variable == 0. This statement will only return true if i is 0 (-1 would also return true)
continue; //If i is 0, cancel the current loop and go to the next loop.
printf("This is message number %d!", i);
}
}
>> Start loop with i, run it as long as i is 2 or less, increase i with every loop [i = 0] if (!i) is true: continue to the next loop, increase i with 1 (i++) [i = 1] if (!i) is false: do nothing (thus proceeding to the rest of the code) print: This is message number 1! Increase i with 1 (i++) [i = 2] if (!i) is false: do nothing (thus proceeding to the rest of the code) print: This is message number 2! Increase i with 1 (i++) [i = 3]: Aborting loop
main()
{
for (new i; i <= 2; i++)
{
if (!i) //Aka "if (i == 0)". !variable equals variable == 0. This statement will only return true if i is 0 (-1 would also return true)
continue; //If i is 0, cancel the current loop and go to the next loop.
printf("This is message number %d!", i);
break;
print("This code is unreachable!");
}
}
>> Start loop with i, run it as long as i is 2 or less, increase i with every loop [i = 0] if (!i) is true: continue to the next loop, increase i with 1 (i++) [i = 1] if (!i) is false: do nothing (thus proceeding to the rest of the code) print: This is message number 1! >BREAK<; Aborting entire loop
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```");
}
}
command equals "!mycommand" >> Start loop with i, run it as long as i is less than variable roleCount. Increase i with every loop [i = 0] DCC_GetGuildRole on index 0 (i), save outcome to variable rolename rolename = g_Role_Level_0 (User) rolename does not equal g_Role_Level_1, g_Role_Level2, g_Role_Level3 or g_Role_Level 4; running the code after else: >>return<< DCC_SendChannelMessage... return: break the loop, return the return value of DCC_SendChannelMessage and break the entire function
MyCommand()
{
//This space is the highest level inside MyCommand()
new i;
while (i < 50)
{
//Lower level [1]
i++;
if (i == 20)
{
//Lower level [2]
}
else continue; //NOTE: If this was "else return" like you did the print() function below (outside of the loop) would never run!
//Code placed here would never run anyway
}
//Even more code here, back to highest level of MyCommand()
print("This will not print if else continue is else return");
}