Page 1 of 1

Send message to all admins and ops?

PostPosted: 10 Jan 2014, 20:49
by joppiesaus
So, I am making a command that clears all of your player data, but it needs to be "approved" by a admin / op, how do I send I a message to every guy in that ranklist?
Thanks!

Re: Send message to all admins and ops?

PostPosted: 10 Jan 2014, 22:17
by dzienny
The code below shows how to get a list of players that are ranked Op. It then checks, if they are online. If yes it sends them a message. This code makes use of powerful LINQ.
Code: Select all
var operatorGroup = Group.groupList.Where(g => g.Permission == LevelPermission.Operator).First();
var operators = operatorGroup.playerList.All();
foreach (var op in operators)
{
    var opOnline = Player.Find(op);
    if (opOnline == null)
        continue;
    Player.SendMessage(opOnline, "Some message");
}


If you want to get a list of Op+ players, then replace the two first lines with this:
Code: Select all
var operatorPlusGroup = Group.groupList.Where(g => g.Permission >= LevelPermission.Operator);
var operatorsPlus = operatorPlusGroup.SelectMany(g => g.playerList.All());


Well, you can do the same without LINQ (below), but I recommend LINQ because it's more readable and concise.
Code: Select all
Group operatorGroup = null;
foreach (Group g in Group.groupList)
{
    if (g.Permission == LevelPermission.Operator)
    {
        operatorGroup = g;
        break;
    }
}
List<string> operators = operatorGroup.playerList.All();


*I haven't tested it, but it should work.

Re: Send message to all admins and ops?

PostPosted: 11 Jan 2014, 19:11
by joppiesaus
Thanks very much Dzienny! I'll post it soon! :D