Ejemplo n.º 1
0
        internal async void RouteCommand(EventData message)
        {
            //Do other thuings
            try
            {
                var invokableAction = await FindInvokableAction(message);

                if (invokableAction != null)
                {
                    await invokableAction.Execute(_actionScheduler);

                    Console.WriteLine($"[{message.RoomId}] {message.Username} invoked {message.CommandName}");
                }
                else
                {
                    await IAction.ExecuteDefaultAction(message, _actionScheduler);
                }
            }
            catch (Exception e)
            {
                var exceptionMsg = $"    Well thanks, {message.Username}. You broke me. \r\n\r\n" + e;
                var codified     = string.Join("\r\n    ", exceptionMsg.Split("\r\n"));
                await _actionScheduler.CreateMessageAsync(codified);
            }
        }
Ejemplo n.º 2
0
        private static async Task ClientRecieved(SocketMessage arg)
        {
            if (arg.Author is SocketGuildUser user)
            {
                if (arg.Author.IsBot)
                {
                    return;
                }

                var config = Config.Manager.Config();
                Console.WriteLine($"[DIS {arg.Channel.Name}] {arg.Content}");
                //Check if we have a mapping.
                if (config.DiscordToStackMap.ContainsKey(arg.Channel.Name))
                {
                    //We are setup to map this channel's messages to stack.
                    var roomId = config.DiscordToStackMap[arg.Channel.Name];
                    //Build the message
                    //var displayname = string.IsNullOrEmpty(user.Nickname) ? user.Username : user.Nickname;

                    // Create a list of messages in case there are embedded codeblocks or stuff alongside text
                    var messages = FromDiscordExtensions.BuildSoMessage(user, config, arg);

                    foreach (var message in messages)
                    {
                        //Find the room scheduler
                        if (StackSchedulers.ContainsKey(roomId))
                        {
                            //We already have a scheduler, lets goooo
                            var sched = StackSchedulers[roomId];
                            await sched.CreateMessageAsync(message);
                        }
                        //Or create one if we already have a watcher.
                        else if (StackRoomWatchers.ContainsKey(roomId))
                        {
                            var watcher      = StackRoomWatchers[roomId];
                            var newScheduler = new ActionScheduler(watcher.Auth, RoomService.Host, roomId);
                            StackSchedulers.Add(roomId, newScheduler);
                            await newScheduler.CreateMessageAsync(message);

                            await arg.Channel.SendMessageAsync("Opened a new scheduler for sending messages to Stack. FYI.");
                        }
                        else
                        {
                            //or complain about not watching stack.
                            await arg.Channel.SendMessageAsync(
                                "Unable to sync messages to Stack - I'm not watching the corresponding channel. Invite me to the channel on stack and tryagain.");
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private static Task ClientRecieved(SocketMessage arg)

        {
            if (arg.Author is SocketGuildUser user)
            {
                if (arg.Author.IsBot)
                {
                    return(Task.CompletedTask);
                }
                var config = Config.Manager.Config();
                Console.WriteLine($"[DIS {arg.Channel.Name}] {arg.Content}");
                //Check if we have a mapping.
                if (config.DiscordToStackMap.ContainsKey(arg.Channel.Name))
                {
                    //We are setup to map this channel's messages to stack.
                    var roomId = config.DiscordToStackMap[arg.Channel.Name];
                    //Build the message
                    var displayname = string.IsNullOrEmpty(user.Nickname) ? user.Username : user.Nickname;

                    var message = $@"\[**[{displayname}]({config.DiscordInviteLink})**] {arg.Content}";
                    //Find the room scheduler
                    if (StackSchedulers.ContainsKey(roomId))
                    {
                        //We already have a scheduler, lets goooo
                        var sched = StackSchedulers[roomId];
                        sched.CreateMessageAsync(message);
                        return(Task.CompletedTask);
                    }
                    //Or create one if we already have a watcher.
                    if (StackRoomWatchers.ContainsKey(roomId))
                    {
                        var watcher      = StackRoomWatchers[roomId];
                        var newScheduler = new ActionScheduler(watcher.Auth, RoomService.Host, roomId);
                        StackSchedulers.Add(roomId, newScheduler);
                        newScheduler.CreateMessageAsync(message);
                        arg.Channel.SendMessageAsync("Opened a new scheduler for sending messages to Stack. FYI.");
                        return(Task.CompletedTask);
                    }
                    //or complain about not watching stack.
                    arg.Channel.SendMessageAsync(
                        "Unable to sync messages to Stack - I'm not watching the corresponding channel. Invite me to the channel on stack and tryagain.");
                    return(Task.CompletedTask);
                }
                //Nothing to do, who cares
                return(Task.CompletedTask);
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 4
0
        private RoomWatcher <DefaultWebSocket> NewRoomWatcherFor(int roomNumber)
        {
            var newRoomWatcher =
                new RoomWatcher <DefaultWebSocket>(_auth, $"https://chat.stackoverflow.com/rooms/{roomNumber}");
            var messageHandler = new ChatEventHandler();
            var scheduler      = new ActionScheduler(_auth, Host, roomNumber);
            var router         = new CommandRouter(_commandService, _httpService, _commandFactory, scheduler);

            messageHandler.OnEvent += router.RouteCommand;
            newRoomWatcher.EventRouter.AddProcessor(messageHandler);

            scheduler.CreateMessageAsync("Hello friends!");

            return(newRoomWatcher);
        }
Ejemplo n.º 5
0
        internal async void RouteCommand(EventData message)
        {
            //Do other thuings
            try
            {
                if (_priorityProcessor.ProcessCommand(message, out var action) ||
                    _processors.Any(p => p.ProcessCommand(message, out action)))
                {
                    await action !.Execute(_actionScheduler);
                    Console.WriteLine($"[{message.RoomId}] {message.Username} invoked {message.CommandName}");
                }
                else
                {
                    // TODO refactor this!
                    action = await _priorityProcessor.ProcessCommandAsync(message);

                    if (action == null)
                    {
                        foreach (var processor in _processors)
                        {
                            action = await processor.ProcessCommandAsync(message);

                            if (action != null)
                            {
                                await action !.Execute(_actionScheduler);
                                Console.WriteLine($"[{message.RoomId}] {message.Username} invoked {message.CommandName}");
                                break;
                            }
                        }
                    }
                    else
                    {
                        await action !.Execute(_actionScheduler);
                        Console.WriteLine($"[{message.RoomId}] {message.Username} invoked {message.CommandName}");
                    }
                }
                if (action == null)
                {
                    await IAction.ExecuteDefaultAction(message, _actionScheduler);
                }
            }
            catch (Exception e)
            {
                var exceptionMsg = $"    Well thanks, {message.Username}. You broke me. \r\n\r\n" + e;
                var codified     = string.Join("\r\n    ", exceptionMsg.Split("\r\n"));
                await _actionScheduler.CreateMessageAsync(codified);
            }
        }
Ejemplo n.º 6
0
    private static async Task Demo()
    {
        // Fetch your account's credentials from somewhere.
        var auth = new EmailAuthenticationProvider("", "");

        // Create an instance of the ActionScheduler. This will
        // allow us to execute chat actions like: posting messages,
        // kicking users, moving messages, etc.
        using (var actionScheduler = new ActionScheduler(auth, roomUrl))
        {
            // Create an instance of the RoomWatcher class. Here we
            // specify (via the type parameter) what WebSocket implementation
            // we'd like to use. This class allows you to subscribe to chat events.
            using (var roomWatcher = new RoomWatcher <DefaultWebSocket>(auth, roomUrl))
            {
                // Subscribe to the UserMentioned event.
                roomWatcher.AddUserMentionedEventHandler(async m =>
                {
                    await actionScheduler.CreateReplyAsync("hello!", m.MessageId);

                    /// Do stuff ...
                });

                // Besides being able to subscribe to the default events,
                // you can also create (and listen to) your own. Your class must
                // implement the ChatEventDataProcessor class, you can also
                // optionally implement IChatEventHandler or IChatEventHandler<T>.
                var customEventHanlder = new AllData();

                // Add a very basic handler.
                customEventHanlder.OnEvent += data => Console.WriteLine(data);

                // Add our custom event handler so we can
                // begin processing the incoming event data.
                roomWatcher.EventRouter.AddProcessor(customEventHanlder);

                // Post a simple message.
                var messageId = await actionScheduler.CreateMessageAsync("Hello world.");

                while (Console.ReadKey(true).Key != ConsoleKey.Q)
                {
                }
            }
        }
    }
Ejemplo n.º 7
0
        public async Task Execute(ActionScheduler scheduler)
        {
            // Send message to chat.so
            await scheduler.CreateMessageAsync(_message);

            // Send message to Discord
            var config        = Manager.Config();
            var channelName   = config.StackToDiscordMap[scheduler.RoomId];
            var discordClient = await Discord.GetDiscord();

            var discord = discordClient.GetChannel(config.DiscordChannelNamesToIds[channelName]);

            if (discord is SocketTextChannel textChannel)
            {
                if (_markdownMessage != null)
                {
                    await textChannel.SendMessageAsync($"{_markdownMessage}");
                }
                else
                {
                    await textChannel.SendMessageAsync($"```{_message}```");
                }
            }
        }