[Server Scripting] Mailbox

Discussion in 'Server and Client Scripting' started by Kase, Oct 4, 2014.

  1. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    Work Need to be done:
    -Messages to be split in to 2 categories (Read & Unread) (Optional)
    -Messages older 1 week to be deleted. (Important)
    -Store IP of the recipient. Also update the IP when they login. (Important)
    Known errors:
    -When using "/readmail", it shows mail but also says "(17:19:34) ±CommandBot: The command readmail doesn't exist". (Fixed)
    Plans:
    -I'm thinking of storing the messages in "scriptdata" in ".txt" format. It will make it easier to organize and move PO if needed. (Fixed)
    Code (text):
    1.     if (command == "sendmail") {
    2.         var data = commandData.split(':');
    3.                 if (data.length != 2) {
    4.             mailbot.sendMessage(src, "You need to give 2 parameters.", channel);
    5.             return;
    6.         }
    7.         var name = data[0];
    8.         var mail = data[1];
    9.         if (!sys.dbRegistered(sys.name(src))) {
    10.             mailbot.sendMessage(src, "You need to be registered to use send mail!", channel);
    11.             return;
    12.         }
    13.         if (!sys.dbRegistered(name.toLowerCase())) {
    14.             mailbot.sendMessage(src, "You can not send mail to un-registered players!", channel);
    15.             return;
    16.         }
    17.         sys.saveVal('msgque' + name.toLowerCase(),sys.getVal('msgque' + name.toLowerCase()) + ' + Message from ' + sys.name(src) + ": " + mail);
    18.         sys.appendToFile('mailbox.txt', "To: " + name + " From: " + sys.name(src) + "(" + sys.ip(src) + ") Content: " + mail + "\n");
    19.         mailbot.sendMessage(src, "Message sent to " + name + "!", channel);
    20.         return;
    21.         }
    22.     if (command == "readmail") {
    23.         var msg = new Array();
    24.         mail = sys.getVal('msgque' + sys.name(src).toLowerCase()).split('+');
    25.         var length = (sys.getVal('msgque' + sys.name(src).toLowerCase()).split('+').length - 1);
    26.         mailbot.sendMessage(src, "Here are your messages:", channel);
    27.         for (i = 1; i <= length; i++) {
    28.             mailbot.sendMessage(src, mail[i]);
    29.             }
    30.             return;
    31.         }
    32.     if (command == "deletemail") {
    33.         sys.removeVal('msgque' + sys.name(src).toLowerCase());
    34.         mailbot.sendMessage(src, "Mail box deleted!", channel);
    35.         return;
    36.     }
     
    Last edited: Oct 12, 2014
  2. Crystal Moogle

    Crystal Moogle Ayaya~ Administrator Administrator

    Joined:
    Jul 19, 2010
    Messages:
    3,205
    Likes Received:
    531
    PO Trainer Name:
    Hanako
    The error comes from the command lacking a return at the end of it
     
  3. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    Changed several things. Mostly to do with where and how the messages are stored.
    New code is:
    Code (text):
    1. //mail commands
    2.     sys.makeDir("scriptdata/messages");
    3.     var msgDir = "scriptdata/messages/";
    4.     if (command == "sendmail") {
    5.         var data = commandData.split(':');
    6.                 if (data.length !== 2) {
    7.             mailbot.sendMessage(src, "You need to give 2 parameters.", channel);
    8.             return;
    9.         }
    10.         var name = data[0];
    11.         var mail = data[1];
    12.         if (!sys.dbRegistered(sys.name(src))) {
    13.             mailbot.sendMessage(src, "You need to be registered to use send mail!", channel);
    14.             return;
    15.         }
    16.         if (!sys.dbRegistered(name.toLowerCase())) {
    17.             mailbot.sendMessage(src, "You can not send mail to un-registered players!", channel);
    18.             return;
    19.         }
    20.         sys.saveVal(msgDir+"msg" + name.toLowerCase()".txt", sys.getVal(msgDir+"msg" + name.toLowerCase()".txt", "Message from " + sys.name(src) + ": " + mail.));
    21.         sys.appendToFile("mailbox.txt", "To: " + name + " From: " + sys.name(src) + "(" + sys.ip(src) + ") Content: " + mail + "\n");
    22.         mailbot.sendMessage(src, "Message sent to " + name + "!", channel);
    23.         return;
    24.         }
    25.     if (command == "readmail") {
    26.         if (!sys.dbRegistered(src)) {
    27.             mailbot.sendMessage(src, "You need to be register to have a mailbox!", channel);
    28.             return;
    29.         }
    30.         var msg = new Array();
    31.         mail = sys.getVal(msgDir+"msg" + sys.name(src).toLowerCase()".txt").split('+');
    32.         var length = (sys.getVal(msgDir+"msg" + sys.name(src).toLowerCase()".txt").split('+').length - 1);
    33.         mailbot.sendMessage(src, "Here are your messages:", channel);
    34.         for (i = 1; i <= length; i++) {
    35.             mailbot.sendMessage(src, mail[i]);
    36.             }
    37.             return;
    38.         }
    39.     if (command == "deletemail") {
    40.             if (!sys.dbRegistered(src)) {
    41.             mailbot.sendMessage(src, "You need to be register to have a mailbox!", channel);
    42.             return;
    43.         }
    44.         sys.removeVal(msgDir+"msg" + sys.name(src).toLowerCase()".txt");
    45.         mailbot.sendMessage(src, "Mail box deleted!", channel);
    46.         return;
    47.     }
    This change is pretty broke. The command doesn't work at all now (it did before). But when it is fixed, this will be far better that the command before. Pretty sure the errors are on line 20, 27, 28 and 36. Its probably because of the way I wrote the file name (which is - msgDir+"msg" + sys.name(src).toLowerCase()".txt"). That seems like its the only error that is stopping the command from working.

    How can I write the file name so that it works? Any suggestions would be great.

    ***Note: This edit is trying to move the storage of messages to "scriptdata/messages" and also store them in ".txt" format.
     
    Last edited: Oct 5, 2014
  4. Crystal Moogle

    Crystal Moogle Ayaya~ Administrator Administrator

    Joined:
    Jul 19, 2010
    Messages:
    3,205
    Likes Received:
    531
    PO Trainer Name:
    Hanako
    I wouldn't save a different file for every user. You'll end up with too many files if you get a fair amount of people on the server.
    Instead a use MemoryHash to save a single file for all messages. Then you can use the name (or IP) as the key.
    JSON could work too, but that can get slow if the file gets too big. But you could save properties like read/unread with that. Although you could do the same with MemoryHash as well by splitting strings up.
    Like <message><readstatus><sender> etc and split each section with ::: or anything that wouldn't be used in normal messages.

    So very basic but
    Code (javascript):
    1.  
    2. //initialize mailbox
    3. script.mailbox  = new MemoryHash(Config.dataDir+"mailbox.txt")
    4.  
    5. //save message
    6. message = message + ":::" + "unread" + ":::" + sys.name(src) //or however you want to sort it
    7. script.mailbox.add(receiver, message)
    8.  
    9. //read message
    10. message = script.mailbox.get(sys.name(src))
    11. sendMessage("Message: " + message[0]) //using format from previous 0 = message, 1 = unreadstatus, 2 = sender
    12. sendMessage("Sender:" + message[2])
    13. message[1] = "read"
    14. script.mailbox.add(sys.name(src), message.join(":::")) //update read status
    15.  
    Note that this isn't meant to be a working script, just a basic example to show how things could work. It could be written a lot better and could be better functional.

    For expiry, you could add a timestamp to the message as a property, and then a certain point every so often (minutely/hourly/daily) using step(), you check all the messages and delete the ones where the timestamp is over 7 days old or w/e
     
  5. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    I used your script as a base.
    Code (text):
    1. //initialize mailbox
    2. script.mailbox  = new MemoryHash(Config.dataDir+"mailbox.txt");
    3. //save message
    4. if (command == "sendmessage" || command == "smsg") {
    5.     if (!sys.dbRegistered(sys.name(src))) {
    6.         messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    7.         return;
    8.     }
    9.     if (sys.dbRegistered(sys.name(tar))) {
    10.         messagebot.sendMesssage(src, "You can only send mail to registered players!", channel);
    11.         return;
    12.     }
    13.     var data = commandData.split(':');
    14.     if (data.length !== 2) {
    15.     messagebot.sendMessage(src, "You need to specify a target and message!", channel);
    16.     return;
    17.     }
    18.     var sender = sys.name(src);
    19.     var receiver = data[0];
    20.     var message = data[1];
    21.     script.mailbox.add(""receiver":"message":"sender"");
    22.     messagebot.sendMessage(src, "You message has been sent!", channel);
    23.     return;
    24. }
    25. //read message
    26. if (command == "readmessage" || command == "rmsgs") {
    27.     if (!sys.dbRegistered(sys.name(src))) {
    28.         messagebot.senMessage(src, "You need to be registered to use this command!", channel);
    29.         return;
    30.     }
    31.     if (!script.mailbox.get(sys.name(src))) {
    32.         messagebot.sendMessage(src, "You have no mail!", channel);
    33.         return;
    34.     }
    35.     var stuff = script.mailbox.get(sys.name(src));
    36.     var data = stuff.split(':');
    37.     var receiver = data[1];
    38.     var message = data[2];
    39.     var sender = data[3];
    40.     sys.sendMessage(src, "", channel);
    41.     messagebot.sendMessage(src, "Your Messages:", channel);
    42.     messagebot.sendMessage(src, "" + message + ". From: " + sender + ".", channel);
    43.     sys.sendMessage(src, "", channel);
    44.     return;
    45. }
    46. //delete message
    47. if (command == "deletemessages" || command == "dmsgs") {
    48.     if (!sys.dbRegistered(sys.name(src))) {
    49.         messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    50.         return;
    51.     }
    52.     if (!script.mailbox.get(sys.name(src))) {
    53.         mail.sendMessage(src, "You have no mail!", channel);
    54.         return;
    55.     }
    56.     var stuff = script.mailbox.get(sys.name(src));
    57.     var data = stuff.split(':');
    58.     var receiver = data[1];
    59.     var message = data[2];
    60.     var sender = data[3];
    61.     script.mailbox.remove(""receiver":"message":"sender"");
    62.     return;
    63. }
    How about now?
     
    Last edited: Oct 6, 2014
  6. Crystal Moogle

    Crystal Moogle Ayaya~ Administrator Administrator

    Joined:
    Jul 19, 2010
    Messages:
    3,205
    Likes Received:
    531
    PO Trainer Name:
    Hanako
    No, a lot of that won't work.
    You're trying concatenate strings together without using + for one. Secondly, MemoryHash().add takes two parameters, not one as you're adding. The first should be the key, which in my example I used the user's name. The reason I said don't use my script is that it doesn't really work for anything. It only saves one message which is obviously not what you want. You'll need to find a way to split the value of the key into multiple values as well (use a different split value than the one you use for messages). You also need to make sure both split values are something not going to be used in normal messages, which is why I used ":::" instead of ":". Then you'll need to filter them out of messages somehow (since ":::" has no real reason to be used in normal messages, it can be removed/replaced without effecting much). Also when removing, MemoryHash().remove only takes the key value used. To remove all messages, that'll simply be the user's name, but to remove individual messages, you'll have to mess around with the split value you used for multiple messages.
     
  7. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    Hmmm, lemme see what I can do.
     
  8. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    Made changes.
    I tested /savemessage, it works.
    /readmessage doesn't work.
    Code (text):
    1.  
    2.     //initialize messages
    3.     script.messages  = new MemoryHash(Config.dataDir+"messages.txt");
    4.     //save message
    5.     if (command == "sendmessage" || command == "smsg") {
    6.         var data = commandData.split(':');
    7.         if (data.length !== 2) {
    8.         messagebot.sendMessage(src, "You need to specify a target and message!", channel);
    9.         return;
    10.         }
    11.         var sender = sys.name(src);
    12.         var receiver = data[0];
    13.         var message = data[1];
    14.         if (!sys.dbRegistered(sender)) {
    15.             messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    16.             return;
    17.         }
    18.         if (!sys.dbRegistered(receiver)) {
    19.         messagebot.sendMessage(src, "You can only send mail to registered players!", channel);
    20.         return;
    21.         }
    22.         if (sys.dbIp(receiver) === undefined) {
    23.             messagebot.sendMessage(src, "No player exists by this name!", channel);
    24.             return;
    25.         }
    26.         if (message.length <= 5) {
    27.             messagebot.sendMessage(src, "Your message is too short!", channel);
    28.             return;
    29.         }
    30.         if (message.length >=100) {
    31.             messagebot.sendMessage(src, "Your message is too long!", channel);
    32.             return;
    33.         }
    34.         script.messages.add("" + sender + ":" + message + "", "" + receiver + "");
    35.         messagebot.sendMessage(src, "You message has been sent!", channel);
    36.         return;
    37.     }
    38.     //read message
    39.     if (command == "readmessage" || command == "rmsgs") {
    40.         if (!sys.dbRegistered(sys.name(src))) {
    41.         messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    42.         return;
    43.         }
    44.         if (!script.messages.get(sys.name(src)) === undefined) {
    45.             messagebot.sendMessage(src, "You have no messages!", channel);
    46.             return;
    47.         }
    48.         var data = script.messages.get("" + sys.name(src) + "" + message +"", "").split('*');
    49.         var message = data[0];
    50.         var receiver = data[1];
    51.         sys.sendMessage(src, "", channel);
    52.         messagebot.sendMessage(src, "Your Messages:", channel);
    53.         messagebot.sendMessage(src, "" + message + ".", channel);
    54.         sys.sendMessage(src, "", channel);
    55.         return;
    56.     }
    57.     //delete message
    58.     if (command == "deletemessages" || command == "dmsgs") {
    59.         if (!sys.dbRegistered(sys.name(src))) {
    60.             messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    61.             return;
    62.         }
    63.         if (!script.messages.get(sys.name(src))) {
    64.             mail.sendMessage(src, "You have no mail!", channel);
    65.             return;
    66.         }
    67.         var stuff = script.messages.get(sys.name(src));
    68.         var data = stuff.split(':');
    69.         var receiver = data[1];
    70.         var message = data[2];
    71.         var sender = data[3];
    72.         script.messages.remove("" + receiver + ":" + message + "", "From: " + sender + "");
    73.         return;
    74.     }
    75.  
    TypeError: Result of expression 'script.messages.get(sys.name(src))' [undefined] is not an object. "var data = script.messages.get(sys.name(src)).split('*');" - this is wrong :/
    Any idea's why? Anything else I did wrong?
     
    Last edited: Nov 3, 2014
  9. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
  10. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    Anyone? :frown:
     
  11. RedJoker25

    RedJoker25 Resident Lurker Server Moderator Article Contributor Server Moderator Article Contributor

    Joined:
    Apr 5, 2011
    Messages:
    269
    Likes Received:
    54
    PO Trainer Name:
    RedJoker25
    From what I can tell, "script.messages.add("" + sender + ":" + message + "", "" + receiver + "");" adds the message using the key "sender:message". In other words, to get stuff from the hash, you'd need the message itself and who it was sent to. At a glance, it looks like it should be reversed though I'm not entirely sure what all the blank "" are for. Also where are you storing * in anything unless it's already a part of the message that you'd need to split when getting? Remove / Get also only take a key and not key + value so neither of those look right.

    <someone else can correct me if i'm wrong>

    Edit: I just realized you have multiple gets so the one I was referring to with only needing a key is var data = script.messages.get("" + sys.name(src) + "" + message +"", "").split('*');
     
  12. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    Alright, so when i use the /sendmessage command, it save the messages like this:
    Kase:Hi man*-Undead- (Kase sent the message to -Undead-)
    The add.script adds 2 variable to the file, which are separated by '*', that's why its there.

    /readmessage doesn't work because scripts.messages.get(sys.name(src)) isn't the full key. I should be something like script.messsages.get(sys.name(src)+message, receiver) But I' not sure how to put that in the script so that it works.

    I also tried this as the code:
    Code (text):
    1.     if (command == "readmessage" || command == "rmsgs") {
    2.         if (!sys.dbRegistered(sys.name(src))) {
    3.         messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    4.         return;
    5.         }
    6.         if (!script.messages.get(sys.name(src)) === undefined) {
    7.             messagebot.sendMessage(src, "You have no messages!", channel);
    8.             return;
    9.         }
    10.         var tmp = [];
    11.         var key = sys.name(src);
    12.         for (var key in script.messages.hash) {
    13.             if (script.messages.hash.hasOwnProperty(key)) {
    14.                 var messages = script.messages.get(key).split('*');
    15.                 tmp.push([key, messages[0], message[1]]);
    16.             }
    17.         }
    18.         sys.sendMessage(src, "", channel);
    19.         messagebot.sendMessage(src, "Your Messages:", channel);
    20.         messagebot.sendMessage(src, "" + message[0] + ".", channel);
    21.         sys.sendMessage(src, "", channel);
    22.         return;
    23.     }
    Its says 'message' [undefined] is not an object. But it is defined so I don't know what I have done wrong :/
     
  13. RedJoker25

    RedJoker25 Resident Lurker Server Moderator Article Contributor Server Moderator Article Contributor

    Joined:
    Apr 5, 2011
    Messages:
    269
    Likes Received:
    54
    PO Trainer Name:
    RedJoker25
    script.messsages.get(sys.name(src)+message, receiver) won't work because .get() only takes one parameter which is the key. The key should be the receiver's name since you use the source (the person sending the command) to pull the message later. You also shouldn't concatenate the message to the name because a) you don't know it until you pull it from the hash and b) the whole point of using a hash table / mail box is to be able to access the message without knowing what it is.

    As for your message [undefined] thing, your naming is off. Either make it all "message" or all "messages".
     
    Last edited: Nov 4, 2014
  14. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
  15. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    For delete messages, I tried this code:
    Code (text):
    1.     if (command == "deletemessages" || command == "dmsgs") {
    2.         if (!sys.dbRegistered(sys.name(src))) {
    3.             messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    4.             return;
    5.         }
    6.         var tmp = [];
    7.         var key = sys.name(src);
    8.         for (var key in script.messages.hash) {
    9.             if (!script.messages.hash.hasOwnProperty(key)) {
    10.                 messagebot.sendMessage(src, "You have no messages!", channel);
    11.                 return;
    12.             }
    13.             if (script.messages.hash.hasOwnProperty(key)) {
    14.                 var messages = script.messages.get(key).split('*');
    15.                 tmp.push([key, messages[0], messages[1]]);
    16.             }
    17.             script.messages.remove(messages[0], messages[1]);
    18.             messagebot.sendMessage(src, "Messages deleted!", channel);
    19.             return;
    20.         }
    21.     }
    No errors, but it doesn't delete the message. :/
     
  16. RedJoker25

    RedJoker25 Resident Lurker Server Moderator Article Contributor Server Moderator Article Contributor

    Joined:
    Apr 5, 2011
    Messages:
    269
    Likes Received:
    54
    PO Trainer Name:
    RedJoker25
    You're welcome.

    As for .remove(), same with .get(), you only need one parameter and it's the key [receiver name]. That deletes both the key and every value [message] associated with it in the hash. To get rid of only a particular message, the only way I can think to do so would be to pull all the messages out, remove the message you want to delete, delete the key from the hash and re-add the rest of the messages.
     
  17. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    Ok, /dmsg is working now.
    I was testing the script and found that if there were no messages, and you used /rmsg, I says (20:43:50) ±CommandBot: The command rmsg doesn't exist
    The code for /rmsg is:
    Code (text):
    1.     if (command == "readmessage" || command == "rmsg") {
    2.         if (!sys.dbRegistered(sys.name(src))) {
    3.         messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    4.         return;
    5.         }
    6.         var tmp = [];
    7.         var key = sys.name(src);
    8.         for (var key in script.messages.hash) {
    9.             if (!script.messages.hash.hasOwnProperty(key)) {
    10.                 messagebot.sendMessage(src, "You have no messages!", channel);
    11.                 return;
    12.             }
    13.             if (script.messages.hash.hasOwnProperty(key)) {
    14.                 var messages = script.messages.get(key).split('*');
    15.                 tmp.push([key, messages[0], messages[1]]);
    16.                 sys.sendMessage(src, "", channel);
    17.                 messagebot.sendMessage(src, "Your Messages:", channel);
    18.                 messagebot.sendMessage(src, "" + messages[0] + ".", channel);
    19.                 sys.sendMessage(src, "", channel);
    20.                 return;
    21.             }
    22.         }
    23.     }
    Don't think "!script.messages.hash.hasOwnProperty(key)" is working properly. How can I write it so that it works properly?
    Btw, would you like to have these command in the PO scripts? I can make a pull request on GitHub if you like.
     
    Last edited: Nov 4, 2014
  18. RedJoker25

    RedJoker25 Resident Lurker Server Moderator Article Contributor Server Moderator Article Contributor

    Joined:
    Apr 5, 2011
    Messages:
    269
    Likes Received:
    54
    PO Trainer Name:
    RedJoker25
    Do you mean no messages at all in the hash or no messages for a particular user? If there aren't any in the hash at all, then the code never goes into the for loop since there's no keys in the hash and so it never hits the return statement that moogle mentioned was the cause of the other 'doesn't exist' error you had. Also it seems bloated in that you want to return regardless of if there's a message so I'd advise just having one return at the end of the block.

    Edit: I can't really test since I don't have the full code, but I feel like the for loop as a whole is extraneous. First, using "for (key in hash)" would replace key on each iteration making the assignment prior to it unnecessary but also basically guaranteeing whatever key happens to be at the time is in the hash and second, I think .hasOwnProperty() checks all keys anyway. I will say, though, that the second is more instinct than anything and it's somewhat late and I don't feel like looking it up at the moment so there's a decent chance I could be wrong.
     
    Last edited: Nov 6, 2014
  19. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    When I tested it, there was not messages in message.txt.

    This is the full code for message:
    Code (text):
    1.     //initialize messages
    2.     script.messages  = new MemoryHash(Config.dataDir+"messages.txt");
    3.     //save message
    4.     if (command == "sendmessage" || command == "smsg") {
    5.         var data = commandData.split(':');
    6.         if (data.length !== 2) {
    7.         messagebot.sendMessage(src, "You need to specify a target and message!", channel);
    8.         return;
    9.         }
    10.         var sender = sys.name(src);
    11.         var receiver = data[0];
    12.         var message = data[1];
    13.         if (!sys.dbRegistered(sender)) {
    14.             messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    15.             return;
    16.         }
    17.         if (!sys.dbRegistered(receiver)) {
    18.         messagebot.sendMessage(src, "You can only send mail to registered players!", channel);
    19.         return;
    20.         }
    21.         if (sys.dbIp(receiver) === undefined) {
    22.             messagebot.sendMessage(src, "No player exists by this name!", channel);
    23.             return;
    24.         }
    25.         script.messages.add("" + receiver + "", "" + sender + ":" + message + "");
    26.         messagebot.sendMessage(src, "You message has been sent!", channel);
    27.         return;
    28.     }
    29.     //read message
    30.     if (command == "readmessage" || command == "rmsg") {
    31.         if (!sys.dbRegistered(sys.name(src))) {
    32.         messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    33.         return;
    34.         }
    35.         var tmp = [];
    36.         var key = sys.name(src);
    37.         for (var key in script.messages.hash) {
    38.             if (script.messages.hash.hasOwnProperty(key)) {
    39.                 var message = script.messages.get(key).split('*');
    40.                 tmp.push([key, message[0], message[1]]);
    41.             }
    42.         }
    43.           for (var x = 0; x < tmp.length; ++x) {
    44.                messagebot.sendMessage(src, x, channel);
    45.              return;
    46.           }
    47.     }
    48.     //delete message
    49.     if (command == "deletemessages" || command == "dmsg") {
    50.         if (!sys.dbRegistered(sys.name(src))) {
    51.             messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    52.             return;
    53.         }
    54.         var tmp = [];
    55.         var key = sys.name(src);
    56.         for (var key in script.messages.hash.hasOwnProperty(key)) {
    57.             if (script.messages.hash(key)) {
    58.                 script.messages.remove(key);
    59.                 messagebot.sendMessage(src, "Messages deleted!", channel);
    60.                 return;
    61.             }
    62.         }
    63.     }
    With the code I had before (for /readmessages) it only showed 1 of the messages. So I changed it a little. But now When I use the command, if has 0 in the PO chat. So /readmessage doesn't work :frown:. /deletemessage doesn't work as well :frown:. /sendmessage works.
     
  20. RedJoker25

    RedJoker25 Resident Lurker Server Moderator Article Contributor Server Moderator Article Contributor

    Joined:
    Apr 5, 2011
    Messages:
    269
    Likes Received:
    54
    PO Trainer Name:
    RedJoker25
    Going through each part:
    You said sendmessage works, so not messing with that.
    Code (javascript):
    1. //read message
    2.     if (command == "readmessage" || command == "rmsg") {
    3.         if (!sys.dbRegistered(sys.name(src))) {
    4.         messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    5.         return;
    6.         }
    Nothing really to talk about here.
    Code (javascript):
    1.  
    2.         var tmp = [];
    3.         var key = sys.name(src);
    4.         for (var key in script.messages.hash) {
    5.             if (script.messages.hash.hasOwnProperty(key)) {
    6.                 var message = script.messages.get(key).split('*');
    7.                 tmp.push([key, message[0], message[1]]);
    8.             }
    9.         }
    Get rid of the for loop [but keep the if block]. As I mentioned before and tested this time, it does nothing but detract from what you want. for (var x in y) replaces x with something in structure y every time the loop runs, meaning your key is constantly getting updated and is always in the hash, defeating the purpose of assigning something to key in the first place and checking if hash.hasOwnProperty(key). As for the if block, .get(key) only returns the value associated with key, which doesn't have a * unless the message or name does [meaning except for that case, message[1] is undefined]. My suggestion would be to use .split(':') instead since you save in "sender:message" format.

    Btw, as far as my testing went, I couldn't get it to work with one person having multiple messages without changing a lot more.

    Code (javascript):
    1.  
    2. for (var x = 0; x < tmp.length; ++x) {
    3.        messagebot.sendMessage(src, x, channel);
    4.        return;
    5. }
    6. }
    The problem with this is you're sending x which you're using as a counter. Send something from tmp. Also your return is cutting the loop after one iteration, meaning regardless of how long tmp is, it's only going through once. Move the return outside the loop.

    Code (javascript):
    1.  
    2.     //delete message
    3.     if (command == "deletemessages" || command == "dmsg") {
    4.         if (!sys.dbRegistered(sys.name(src))) {
    5.             messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    6.             return;
    7.         }
    8.  
    Again, nothing to say here.
    Code (javascript):
    1.  
    2.         var tmp = [];
    3.         var key = sys.name(src);
    4.         for (var key in script.messages.hash.hasOwnProperty(key)) {
    5.             if (script.messages.hash(key)) {
    6.                 script.messages.remove(key);
    7.                 messagebot.sendMessage(src, "Messages deleted!", channel);
    8.                 return;
    9.             }
    10.         }
    11.     }
    Again, remove the for (var key...) thing for the same reason as in /rmsg. Also make sure your if condition is actually a condition [i.e use .hasOwnProperty]. Return should be outside in case key not in hash.

    Though not required, I guess, you should probably do something to let the user know the command happened even if they didn't have messages for both /rmsg and /dmsg.
     
    Last edited: Nov 17, 2014
  21. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    So I changed /readmessage to this:
    Code (text):
    1.     if (command == "readmessage" || command == "rmsg") {
    2.         if (!sys.dbRegistered(sys.name(src))) {
    3.         messagebot.sendMessage(src, "You need to be registered to use this command!", channel);
    4.         return;
    5.         }
    6.         var tmp = [];
    7.         var key = sys.name(src);
    8.         if (script.messages.hash.hasOwnProperty(key)) {
    9.             var message = script.messages.get(key);
    10.             tmp.push([message]);
    11.         }
    12.         for (var x in tmp) {
    13.             messagebot.sendMessage(src, x, channel);
    14.         }
    15.         return;
    16.     }
    It shows the messages, but only one of them. :/
    Pretty sure I stuffed up at:
    Code (text):
    1.         for (var x in tmp) {
    2.             messagebot.sendMessage(src, x, channel);
    3.         }
    Like @RedJoker25 Said, x has to be something in y. What can I change that to?
     
  22. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
  23. SongSing

    SongSing KILLL

    Joined:
    Jan 2, 2013
    Messages:
    641
    Likes Received:
    191
    PO Trainer Name:
    SongSing
    edit: try making tmp the actual hash/array instead of pushing to it
     
    Last edited: Nov 28, 2014
  24. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    It still only shows one of the messages.
     
  25. Duster

    Duster dodgerswin 2018

    Joined:
    Sep 23, 2013
    Messages:
    378
    Likes Received:
    699
    well, tmp is created locally in the function (var tmp = []), and only one message is ever being pushed into it, so tmp only has one item at max...

    haven't read any of the code, but I'm assuming you want the tmp.push to be in a loop to get all the messages
     
  26. RedJoker25

    RedJoker25 Resident Lurker Server Moderator Article Contributor Server Moderator Article Contributor

    Joined:
    Apr 5, 2011
    Messages:
    269
    Likes Received:
    54
    PO Trainer Name:
    RedJoker25
    from what i can tell, .get() gets the last entry with the given key. .add() makes a new entry every time you call it. in other words, you'll need to mess around with sendmsg to save multiple messages in one entry and then split it when using get followed by a loop to print everything in the array generated by split.
     
  27. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    So I can only push 1 object to tmp. Dammit

    What if I pushed messages.txt to tmp, then sorted it using the key? Can that work?
    Or
    As @SongSing said I can make tmp the array.
    So somthing like this?
    Code (text):
    1. var tmp = sys.getFileContent(Config.dataDir + "messages).split('???')
    And should it be split be?
     
    Last edited: Dec 1, 2014
  28. RedJoker25

    RedJoker25 Resident Lurker Server Moderator Article Contributor Server Moderator Article Contributor

    Joined:
    Apr 5, 2011
    Messages:
    269
    Likes Received:
    54
    PO Trainer Name:
    RedJoker25
    It's not that you <can> only push one thing, it's that you <are> only pushing one thing.

    That aside, I feel like you're making this a lot more complicated than it needs to be. I don't see a reason for pushing the hash into an array or whatever you're trying to do when you already have the hash. Pardon the shit formatting, but here's an idea of what I was talking about with saving to one entry:

    Code (text):
    1. newmessage = sender + ": " + message
    2. if (!hash.hasOwnProperty(name)) //basically what you have
    3.     hash.add(name, newmessage)
    4. else //new stuff
    5.     tmp = hash.get(name) //gets the old stuff out
    6.     hash.remove(name) //gets rid of the old entry so it doesn't take up space
    7.     tmp = tmp + "***" + newmessage //adds a separator between messages so you can split later
    8.     hash.add(name, tmp) //adds the new message along with re-adding the old ones
    9. end
    where name is the receiver, sender is the person using /smsg, and the message is the message being sent

    Code (text):
    1. tmp = hash.get(key).split("***") //tmp is now an array of messages
    2. for (i = 0; i < tmp.length; i++) //loop through tmp array
    3.     sendMessage(tmp[i]) //print messages
    4. end
    where key is the person using /rmsg

    /dmsg would also need to be changed unless deleting all of a person's messages is good enough in which case just use a remove(key).
     
    Last edited: Dec 1, 2014
  29. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    That will work but only 1 message can be stored, I want to store more than one.

    With the code I have now you can view all messages stored in hash, but only 1 by one (cause it only shows the latest). I want to make it so that you can view all of them a one time by using the command.

    So to do that, I think they should be a loop which keeps the script pushing to tmp until all of the key's have been push into tmp. I have no idea how, lemme think of something.
     
  30. RedJoker25

    RedJoker25 Resident Lurker Server Moderator Article Contributor Server Moderator Article Contributor

    Joined:
    Apr 5, 2011
    Messages:
    269
    Likes Received:
    54
    PO Trainer Name:
    RedJoker25
    Not sure how you read the 1-message thing when I literally said one line "adds the new message along with re-adding the old ones" meaning nothing is lost until you use /dmsg. The hash stores one <entry> for each person / alt, not one <message>. Since there's only one entry, you can do one get(), one split(), and a for loop to print all of the alt's messages, provided you add a separator when adding and split when getting. No need to loop push anything, much less all of the keys which is a waste of time when the hash already has all of the easily accessed keys.
     
    Last edited: Dec 1, 2014
  31. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    Ah, but you can only view the 1 messages then you have to delete that one, and then you can view the next one. I was hoping you can view all of them then you use the command once, and to do that, I need to push all the keys to tmp.