Method code for $robot.add_reaction_cmd()

[Turn off line numbering]
  1: arg cmdstr, cmd, args, prep1, hook, prep2, me;
  2: var t, type, method, times, chance, min, max, hook_args, i, opts, o, def_opts;
  3: 
  4: (> .perms(caller(), 'command) <);
  5: hook = (| hook.to_symbol() |);
  6: if (!hook)
  7:     throw(~args, "Invalid hook");
  8: def_opts = [["t?ype", 1], ["m?ethod", 1], ["ti?mes", 1], ["c?hance", 1], ["min", 1], ["max", 1], ["a?rgs", 1]];
  9: [t, opts] = $parse_lib.getopt(args, def_opts);
 10: t = t.join();
 11: 
 12: // Set the defaults
 13: method = 'regexp;
 14: type = 'tosay;
 15: chance = 100;
 16: times = -1;
 17: hook_args = [];
 18: min = 0;
 19: max = 0;
 20: catch any {
 21:     // check the options!
 22:     o = opts.slice(1);
 23:     if ((i = "m?ethod" in o)) {
 24:         method = (| opts[i][4].to_symbol() |);
 25:         if (!method)
 26:             throw(~args, "Invalid method specified with +m?ethod=??");
 27:     }
 28:     if ((i = "t?ype" in o)) {
 29:         type = (| opts[i][4].to_symbol() |);
 30:         if (!type)
 31:             throw(~args, "No type specified with +t?ype=??");
 32:     }
 33:     if ((i = "c?hance" in o)) {
 34:         chance = (| opts[i][4].to_number() |);
 35:         if (chance < 1 || chance > 100)
 36:             throw(~args, "+c?hance=?? requires an integer between 1 and 100");
 37:     }
 38:     if ((i = "ti?mes" in o)) {
 39:         times = (| opts[i][4].to_number() |);
 40:         if (!times || times < -1)
 41:             throw(~args, "+ti?mes=?? requires an integer >= -1");
 42:     }
 43:     if ((i = "min" in o)) {
 44:         min = (| opts[i][4].to_number() |);
 45:         if (!min || min < 0)
 46:             throw(~args, "+min=?? requires an integer >= 0");
 47:     }
 48:     if ((i = "max" in o)) {
 49:         max = (| opts[i][4].to_number() |);
 50:         if (!max || max < 0)
 51:             throw(~args, "+max=?? requires an integer >= 0");
 52:     }
 53:     if ((i = "a?rgs" in o)) {
 54:         hook_args = (> fromliteral(opt[i][4]) <);
 55:         if (type(hook_args) != 'list)
 56:             hook_args = [hook_args];
 57:     }
 58:     if (max < min)
 59:         max = min;
 60:     (> .add_reaction(method, t, type, chance, times, [hook, hook_args], min, max) <);
 61: } with {
 62:     return traceback()[1][2];
 63: }
 64: return strfmt("Reaction %d added to %s.%s() with match method %s, type %s, chance %d, times %d, args %s, min_delay %s, and max_delay %s.", t, toliteral(this()), hook, toliteral(method), toliteral(type), chance, times, toliteral(hook_args), min, max);
 65: 
 66: // $#Copied 21 Feb 98 01:50 from $programmer.add_command_cmd() by $user_sean
 67: // $#Edited: 21 Feb 98 03:23 $user_sean
 68: // $#Edited: 21 Feb 98 03:24 $user_sean

["// Created 26-Mar-1995 as a part of ColdCore, see: @help Credit"]

the Cold Dark