Command Suggestion: /leavemsg

hihihilolHI

Member
Joined
Jan 12, 2012
Messages
440
Reaction score
532
Points
63
I was chatting with catgirl, and something she said gave me an idea for a command that could prove quite useful. My idea is a command called /leavemsg, for op+. Say player1 was reported in the reporting thread for block glitching. A staff would go on the server and type /leavemsg player1 Block glitching is illegal. The next time player1 joins the server, s/he will receive the message "Block glitching is illegal" after the typical stuff you see when you join. Instead of staff setting their pings for a certain name to come online, they could simply leave a message, which would be much more comvenient.
So...yeah, any comments/suggestions?
 

Conor

Member
Joined
Dec 12, 2012
Messages
34
Reaction score
103
Points
18
Website
cipbot.webs.com
I once made this for my own server a while back. The code isn't very pretty... but I may as well share it with you guys to save you some of your time if you consider making it. This is how /leavemessage could be configured in the server.

The command;

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace BlockTopiaTNT // Obviously change to namespace of your software.
{
    public class CmdLeaveMessage : Command
    {
        public override string name { get { return "leavemessage"; } }
        public override string shortcut { get { return "leavemsg"; } }
        public override string type { get { return "other"; } }
        public override bool museumUsable { get { return true; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Builder; } }
        public CmdLeaveMessage() { }
 
        public override void Help(Player p)
        {
            Player.SendMessage(p, "/leavemessage [player] [message] - leave a message that will be displayed to [player] when they next come online.");
        }
        public override void Use(Player p, string message)
        {
            if (message == "") { Help(p); return; }
 
            string Theplayer = "";
            string Themessage = "";
 
            try { Theplayer = message.Split(' ')[0]; }
            catch { }
            try { Themessage = message.Substring(message.Split(' ')[0].Length); }
            catch { }
 
            if (Themessage.Trim() == "") { Help(p); return; }
            if (Themessage.Length > 150) { Player.SendMessage(p, "Sorry, you cannot leave messages longer than 150 characters."); return; }
            Player who = Player.Find(Theplayer);
            if (who != null) { Player.SendMessage(p, "That player is already online! Just talk to them!"); return; }
 
            if (Player.GetGroup(Theplayer).Permission == LevelPermission.Guest || Player.GetGroup(Theplayer).Permission == LevelPermission.Banned)
            {
                Player.SendMessage(p, "You can only leave messages to players above the guest rank."); return;
            }
 
            if (check(Theplayer.ToLower(), p) == true)
            {
                Player.SendMessage(p, "You have already left this player a message!"); return;
            }
 
            File.AppendAllText("text/leftmessages.txt", Theplayer.ToLower().Trim() + ":" + p.name.ToLower() + ":" + Themessage + Environment.NewLine);
            Player.SendMessage(p, "You have succesfully left your message. " + Theplayer.ToLower() + " will receive it when they next log on.");
        }
 
        public bool check(string playerName, Player pl)
        {
            foreach (string line in File.ReadAllLines("text/leftmessages.txt"))
            {
                if (line != "")
                {
                    if (line.Substring(0, line.IndexOf(":")) == playerName.ToLower())
                    {
                        string check = line.Substring(line.IndexOf(":") + 1, (line.Substring(line.IndexOf(":") + 1)).IndexOf(":"));
                        if (check == pl.name.ToLower()) { return true; }
                    }
                }
            }
            return false;
        }
    }
}
Within the Player class;

Code:
public bool leftMessage()
        {
            foreach (string line in File.ReadAllLines("text/leftmessages.txt"))
            {
                if (!string.IsNullOrEmpty(line))
                {
                    string nameCheck = line.Substring(0, line.IndexOf(":"));
                    if (this.name.ToLower() == nameCheck) { return true; }
                }
            }
            return false;
        }
        public string getLeftMessage()
        {
            foreach (string line in File.ReadAllLines("text/leftmessages.txt"))
            {
                if (line != "")
                {
                    string checkName = line.Substring(0, line.IndexOf(":"));
                    if (checkName == this.name.ToLower())
                    {
                        return line.Substring(line.IndexOf(":") + 1);
                    }
                }
            }
            return "";
        }
        public void deleteMessage(string From)
        {
            List<string> toPaste = new List<string>();
            foreach (string line in File.ReadAllLines("text/leftmessages.txt"))
            {
                if (line != "")
                {
                    string[] par = line.Split(':');
                    if (this.name.ToLower() == par[0])
                    {
                        if (From.ToLower() != par[1])
                        {
                            toPaste.Add(line);
                        }
                    }
                    else
                    {
                        toPaste.Add(line);
                    }
                }
            }
            File.Delete("text/leftmessages.txt");
            File.WriteAllLines("text/leftmessages.txt", toPaste.ToArray());
        }

Within the HandleLogin method (Player.cs) at the relevant position;

Code:
                        if (this.leftMessage() == true)
                        {
                            for (int i = 0; i < this.totalMsgs(); i++)
                            {
                                string Msg = this.getLeftMessage();
                                string from = Msg.Substring(0, Msg.IndexOf(":"));
                                string msg = Msg.Substring(Msg.IndexOf(":") + 1);
 
                                this.SendMessage(c.maroon + "You have been left a message: " + c.lime + msg);
                                this.SendMessage(c.maroon + "Message From: " + c.gold + from);
 
                                this.deleteMessage(from);
                            }
                        }
And of course you would want to check for the leftmessages text file during server start-up.

Thanks,