예제 #1
0
        private static void SetupMessageListener()
        {
            Client.MessageReceived += async(s, m) =>
            {
                ICommand command;
                var      message = m.Message.RawText.Trim();

                if (m.Message.IsAuthor || m.Channel.IsPrivate)
                {
                    return;
                }

                // Checking if we can output in this channel
                using (var db = new Database())
                {
                    Match match;
                    var   server = await db.servers.FirstAsync(o => o.Id == m.Server.Id);

                    // Failing fast
                    if (!(match = Regex.Match(message, $"^\\{server.Trigger}(.+?)(?:\\s|$)")).Groups[1].Success ||
                        !Commands.TryGetValue(match.Groups[1].Value.ToLower(), out command) ||
                        (match.Groups[1].Value.ToLower() != "use" && !db.channels.Any(c => c.Id == m.Channel.Id)))
                    {
                        return;
                    }
                }

                dynamic args;

                try
                {
                    await m.Channel.SendIsTyping();
                }

                catch (Exception)
                {
                    return;
                }

                // Parsing input
                try
                {
                    var commandLine = message.Contains(' ')
                        ? message.Substring(message.IndexOf(' ') + 1)
                        : "";

                    args = await command.ParseArguments(CommandLineUtil.CommandLineToArgs(commandLine), m.Message);

                    // Checking if args returns a boolean
                    if (args is bool)
                    {
                        return;
                    }
                }

                catch (ControlledException e)
                {
                    await m.Channel.SendMessage(e.Message);

                    return;
                }

                catch (Exception e)
                {
                    await m.Channel.SendMessage("An error occured when parsing your input.\n" +
                                                $"```{e.GetBaseException().Message}```");

                    Console.WriteLine(e.StackTrace);
                    return;
                }

                // Executing command
                try
                {
                    // Running command
                    await command.Execute(args, m.Message);
                }

                catch (ControlledException e)
                {
                    await m.Channel.SendMessage(e.Message);
                }

                catch (Exception e)
                {
                    await m.Channel.SendMessage("An error occured when executing the command.\n" +
                                                $"```{e.GetBaseException().Message}```");

                    Console.WriteLine(e.StackTrace);
                }
            };
        }