public void RegisterCommand(SlackBotCommand command, SlackBotV3 slackBot) { ICommandType commandType = CommandTypeRegistry.GetCommandType(command.Name); ICommandHandler commandHandler = commandType.MakeCommandHandler(slackBot); CommandContainer[GetCommandKey(command)] = commandHandler; }
public void Reply(SlackBotCommand command, string response, string botName = null, string iconUrl = IconUrl, string channelID = null) { if (botName == null) { botName = BotName; } if (channelID == null) { channelID = command.Channel.id; } if (command.Channel.name == "the-the-the-the-the-") { int numThes = response.NormalizeSpace().Split(' ').Count(); StringBuilder responseBuilder = new StringBuilder(); for (int i = 0; i < numThes; ++i) { responseBuilder.Append("the "); } response = responseBuilder.ToString(); botName = "TheTheTheTheThe"; } SlackBot.PostMessage((messageReceived) => { }, channelID, response, botName, icon_url: iconUrl); }
private void HandleMessage(NewMessage newMessage) { if (newMessage.user == null) //The message is from us, ignore { newMessage.user = SlackBot.MySelf.id; } //return; SlackBotCommand command; if (!SlackBotCommand.GetCommand(newMessage, SlackBot, out command)) { return; } // DM to slackbot doesn't have a channel. if (command.Channel == null) { return; } if (CommandTypeRegistry.HasHandler(command.Name)) { PrivilegeLevel privilegeLevel = CommandTypeRegistry.GetCommandType(command.Name).GetPrivilegeLevel(); if ((int)GetUserPrivilege(command.User.name) < (int)privilegeLevel) { Reply(command, "http://i.imgur.com/egUwx5Q.gif"); Reply(command, "You better check your privilege!"); return; } if (!CommandHandlerRegistry.HasCommand(command)) { CommandHandlerRegistry.RegisterCommand(command, this); } ICommandHandler handler = CommandHandlerRegistry.GetCommand(command); if (!handler.Execute(command)) { CommandHandlerRegistry.DeRegisterCommand(command); } } else { Reply(command, ":clippy: \"Were you trying to run this command?\""); Reply(command, string.Format("@badjob {0} not knowing the commands.", command.User.name)); } }
private int GetCommandKey(SlackBotCommand command) { int commandId = CommandTypeRegistry.GetCommandId(command.Name); CommandScope scope = CommandTypeRegistry.GetCommandType(command.Name).GetCommandScope(); object preKey = null; switch (scope) { case CommandScope.Global: preKey = commandId; break; case CommandScope.Channel: preKey = new Tuple <int, string>(commandId, command.Channel.id); break; case CommandScope.User: preKey = new Tuple <int, string, string>(commandId, command.Channel.id, command.User.id); break; } return(preKey.GetHashCode()); }
public ICommandHandler GetCommand(SlackBotCommand command) { return(CommandContainer[GetCommandKey(command)]); }
public bool HasCommand(SlackBotCommand command) { return(CommandContainer.ContainsKey(GetCommandKey(command))); }
public void DeRegisterCommand(SlackBotCommand command) { CommandContainer.Remove(GetCommandKey(command)); }
public static bool GetCommand(NewMessage newMessage, SlackSocketClient slackBot, out SlackBotCommand command) { command = new SlackBotCommand(newMessage, slackBot); return(newMessage.text.StartsWith(CommandPrefix)); }