public static bool TryParse(ChatProviderType type, string str, out ChatCommand cmd) { string name = ""; string args = ""; cmd = new ChatCommand(); cmd.Source = type; if (!str.StartsWith("$vote", StringComparison.InvariantCultureIgnoreCase)) { return false; } // remove $vote str = str.Remove(0, 5).Trim(); if (str.Length == 0) { cmd.Name = "help"; cmd.Args = ""; return true; } if (!str.Contains(" ")) { cmd.Name = str; cmd.Args = ""; return true; } var arr = str.Split(new char[] { ' ' }, 2); name = arr[0]; args = arr[1]; if (!COMMANDS.Contains(name)) { cmd.Name = "help"; cmd.Args = ""; return true; } cmd.Name = name; cmd.Args = args; return true; }
private void ProcessMessage(SlackMessage message) { // we should never respond to messages from the bot itself if (String.Equals(message.User.Name, "ConsensusBot", StringComparison.InvariantCultureIgnoreCase) && !String.Equals(message.ChatHub.Name, "general", StringComparison.InvariantCulture)) { return; } var mod = Moderator.GetModerator(message.User.Name); if (message.Text.StartsWith("$vote", StringComparison.InvariantCultureIgnoreCase)) { ChatCommand cmd = null; if (ChatCommand.TryParse(ChatProviderType.Slack, message.Text, out cmd)) { OnCommandReceived(new CommandReceivedEventArgs { Command = cmd, Message = message.Text, Moderator = mod }); } } }
private void CommandCast(ChatCommand cmd, Moderator mod) { var usage = "usage: $vote cast [choice]{ yes | no | abstain } [motion id] "; if (cmd.Args.Length == 0) { WriteUser(usage, mod,cmd.Source); return; } var stuff = cmd.Args.Split(' '); if (stuff.GetUpperBound(0) < 1) { WriteUser(usage, mod,cmd.Source); return; } var id = -1; try { id = Int32.Parse(stuff[1]); } catch { WriteUser(usage, mod,cmd.Source); return; } Choice c = Choice.Abstain; bool error = false; switch (stuff[0].ToLower()) { case "yes": case "y": case "+1": case "yay": case "yae": case "yea": case "oui": case "si": case "ja": case "da": c = Choice.Yes; break; case "no": case "n": case "-1": case "nae": case "nay": case "non": case "nein": case "neit": c = Choice.No; break; case "abstain": case "abs": case "0": c = Choice.Abstain; break; default: error = true; break; } if (error) { WriteUser(usage, mod); return; } WriteUser("Submitting ballot. Please wait.",mod,cmd.Source); var motion = Motion.GetMotion(id); var b = new Ballot(); b.Choice = c; b.Moderator = mod; b.Motion = motion; b.Insert(); WriteUser("Your ballot has been recorded", mod,cmd.Source); }
private void CommandShow(ChatCommand cmd, Moderator mod, bool broadcast = true) { var usage = "usage: $vote show [ {motion id} | ]"; var opts = cmd.Args.Split(' '); // args empty if (opts.GetUpperBound(0) == 0) { WriteUser(usage, mod); return; } try { var motion = Motion.GetMotion(Int32.Parse(opts[0])); var message = new StringBuilder(); message.AppendFormat("Motion [{0}]", motion.Id.ToString()); message.AppendFormat("{{0}/{1}/{2}}", motion.Votes.Yes, motion.Votes.No, motion.Votes.Abstain); message.AppendFormat(": {0} # {1}", motion.Text, motion.PostUrl); if (broadcast) { WriteChannel(message.ToString(),ChatProviderType.All); } else { WriteUser(message.ToString(),mod,cmd.Source); } } catch { WriteUser(usage, mod,cmd.Source); } }
private void CommandMotion(ChatCommand cmd,Moderator mod) { var usage = "usage: $vote motion [text of the motion]"; if (cmd.Args.Length == 0) { WriteUser(usage, mod,cmd.Source); return; } WriteUser("Submitting your motion...this make take a second.", mod,cmd.Source); var motion = Motion.Create(mod, cmd.Args); var result = "Motion entered [" + motion.Id.ToString() + "]: " + motion.PostUrl; WriteChannel(result,ChatProviderType.All); }
private void CommandHelp(Moderator mod, ChatCommand cmd = null) { string context = ""; if (cmd != null && cmd.Args != null && cmd.Args.Trim().Contains(" ")) { context = cmd.Args.Split(' ')[0]; } switch (context.ToLowerInvariant()) { case "help": WriteUser("usage: $vote [ new { text } | cast { id } { yes/no/abstain } | help ]",mod,cmd.Source); break; case "new": WriteUser("usage: $vote new I'd like to do this. all in favor?",mod,cmd.Source); break; case "cast": WriteUser("usage: $vote cast [ yes / yea / +1 | no / nae / -1 | abstain / abs / 0 ]", mod,cmd.Source); break; default: WriteUser("usage: $vote [ new { text } | cast { id } { yes/no/abstain } | help ]", mod,cmd.Source); break; } }