/// <summary> /// TODO: manage threading. /// </summary> public void MessageHandler() { var commandManager = new AdaCommandManager(); if (!Configuration.IgnoreOwnEvents) chatRoom.EventManager.IgnoreOwnEvents = false; chatRoom.EventManager.ConnectListener(EventType.MessagePosted, new Action<Message>(message => { if (!message.Content.StartsWith("@Ada", StringComparison.InvariantCultureIgnoreCase)) return; var command = commandManager.GetCommand(message.Content); command.Invoke(message.Author, chatRoom, chatClient); })); // Fun while testing. chatRoom.EventManager.ConnectListener(EventType.UserEntered, new Action<User>(user => { var messageBuilder = new MessageBuilder(); messageBuilder.AppendPing(user); messageBuilder.AppendText("!! Welcome to the SOCVR testing facility!! Please keep your arms and head within the room at all times!!!"); chatRoom.PostMessageLight(messageBuilder); })); }
public void ProcessMessage(Message msg, ref Room rm) { var cmdsMsg = new MessageBuilder(MultiLineMessageType.Code, false); cmdsMsg.AppendPing(msg.Author); foreach (var cmd in commands) { cmdsMsg.AppendText("\n" + cmd.Usage + " - " + cmd.Description); } cmdsMsg.AppendText("\n" + Usage + " - " + Description); rm.PostMessageFast(cmdsMsg); }
public void Execute(string command, string arguments, User user, Room room, Client client) { if (!AdaPermissions.IsAuthorized(permissionLevel, user)) { var messageBuilder = new MessageBuilder(); messageBuilder.AppendPing(user); messageBuilder.AppendText("You are not authorised to make this command."); room.PostMessage(messageBuilder); } else { Action(command, arguments, user, room, client); } }
public static string FormatReport(Post post, ClassificationResults results, float threshold) { if (post == null || results == null || results.Action == ClassificationResults.SuggestedAction.Nothing) { return null; } var msg = new MessageBuilder(); var boldTh = 1 - ((1 - threshold) / 2); var resData = $"Similarity: {Math.Round(results.Similarity * 100)}%"; switch (results.Action) { case ClassificationResults.SuggestedAction.Edit: { msg.AppendLink("Edit", editUrl, resData, results.Similarity >= boldTh ? TextFormattingOptions.Bold : TextFormattingOptions.None, WhiteSpace.None); break; } case ClassificationResults.SuggestedAction.Close: { msg.AppendLink("Close", closeUrl, resData, results.Similarity >= boldTh ? TextFormattingOptions.Bold : TextFormattingOptions.None, WhiteSpace.None); break; } case ClassificationResults.SuggestedAction.Delete: { msg.AppendLink("Delete", deleteUrl, resData, results.Similarity >= boldTh ? TextFormattingOptions.Bold : TextFormattingOptions.None, WhiteSpace.None); break; } } msg.AppendText(": "); msg.AppendLink(post.Title, post.Url, "Score: " + post.Score, TextFormattingOptions.None, WhiteSpace.None); msg.AppendText(", by "); msg.AppendLink(post.AuthorName, post.AuthorLink, "Reputation: " + post.AuthorRep, TextFormattingOptions.None, WhiteSpace.None); msg.AppendText("."); return msg.ToString(); }
static void Main() { Console.WriteLine("This is a ChatExchange.Net demonstration. Press the 'Q' key to exit...\n\n"); // Create a client to authenticate the user (which will then allow us to interact with chat). var client = new Client("*****@*****.**", "MySuperStr0ngPa55word"); // Join a room by specifying its URL (returns a Room object). var sandbox = client.JoinRoom("http://chat.stackexchange.com/rooms/1/sandbox"); // Post a new message in the room (if successful, returns a Message object, otherwise returns null). // (If you have no use of the returned Message object, I'd recommend using .PostMessageFast() instead.) var myMessage = sandbox.PostMessage("Hello world!"); // Listen to the InternalException event for any exceptions that may arise during execution. // See our wiki for examples of other events // (https://github.com/ArcticEcho/ChatExchange.Net/wiki/Hooking-up-chat-events). sandbox.EventManager.ConnectListener(EventType.InternalException, new Action<Exception>(ex => Console.WriteLine("[ERROR] " + ex))); // Listen to the MessagePosted event for new messages. sandbox.EventManager.ConnectListener(EventType.MessagePosted, new Action<Message>(message => { // Print the new message (with the author's name). Console.WriteLine(message.Author.Name + ": " + message.Content); // If the message contains "3... 2... 1...", post "KA-BOOM!" (this is simply an [awful] example). if (message.Content.Contains("3... 2... 1...")) { // Create a new MessageBuilder to format our message. var msgBuilder = new MessageBuilder(); // Append the text "KA-BOOM!" (formatted in bold). msgBuilder.AppendText("KA-BOOM!", TextFormattingOptions.Bold); // Finally post the formatted message. // (PostMessage() internally calls the message's ToString() method.) var success = sandbox.PostMessage(msgBuilder) != null; Console.WriteLine("'KA-BOOM' message successfully posted: " + success); } })); // Listen to the UserEntered event and post a welcome message when the event is fired. sandbox.EventManager.ConnectListener(EventType.UserEntered, new Action<User>(user => { var success = sandbox.PostMessage("Hello " + user + "!") != null; Console.WriteLine("'Welcome' message successfully posted: " + success); })); // Wait for the user to press the "Q" key before we exit (not the best way to do this, but it'll suffice). while (char.ToLower(Console.ReadKey(true).KeyChar) != 'q') { Thread.Sleep(500); } // Not necessary for safe disposal, but still, it's nice to "physically" // leave the room rather than letting timeouts figure out that we've gone. sandbox.Leave(); // Safely dispose of the client object (which'll also clean up all created room instances). client.Dispose(); }
public static void EchoMessage(string command, string arguments, User user, Room room, Client client) { var messageBuilder = new MessageBuilder(); messageBuilder.PingAndMessage(user, command + "!! :D"); room.PostMessageLight(messageBuilder); }
/// <summary> /// Continue Post hunting. /// </summary> public static void Continue(string command, string arguments, User user, Room room, Client client) { var messageBuilder = new MessageBuilder(); messageBuilder.PingAndMessage(user, "Placeholder for future unpausing of dupe search."); room.PostMessageLight(messageBuilder); }
/// <summary> /// Give user permissions. /// </summary> public static void AddUser(string command, string arguments, User user, Room room, Client client) { var messageBuilder = new MessageBuilder(); messageBuilder.PingAndMessage(user, "Placeholder for future adding users."); room.PostMessageLight(messageBuilder); }
private static void Sleep(string command, string arguments, User user, Room room, Client client) { var messageBuilder = new MessageBuilder(); messageBuilder.AppendText("leave"); room.PostMessageLight(messageBuilder); }
/// <summary> /// Show list of commands. duh /// </summary> private static void ShowCommands(string command, string arguments, User user, Room room, Client client) { var messageBuilder = new MessageBuilder(); messageBuilder.PingAndMessage(user, "Here is a list of my commands:\n"); var commands = AdaCommands.Where(kvp => kvp.Value.IsCommand).Select(kvp => kvp.Key); messageBuilder.AppendText(string.Join(Environment.NewLine, commands)); room.PostMessageLight(messageBuilder); }
public Action<User, Room, Client> GetCommand(string content) { return (user, room, client) => { var keyValuePair = AdaCommands.FirstOrDefault(c => content.Contains(c.Key)); var matchingCommand = keyValuePair.Value; if (matchingCommand == null) { var messageBuilder = new MessageBuilder(); messageBuilder.AppendPing(user); messageBuilder.AppendText("I don't know what you mean"); room.PostMessageLight(messageBuilder); } else { var command = keyValuePair.Key; var startIndex = content.IndexOf(command); var arguments = content.Substring(startIndex + command.Length).Trim(); matchingCommand.Execute(keyValuePair.Key, arguments, user, room, client); } }; }
/// <summary> /// No where near having ready. /// </summary> public static void SummaryReport(string command, string arguments, User user, Room room, Client client) { var messageBuilder = new MessageBuilder(); messageBuilder.PingAndMessage(user, "Placeholder for future summary reports"); room.PostMessageLight(messageBuilder); }