Exemplo n.º 1
0
        public async Task RunBotAsync()
        {
            // first, let's load our configuration file
            var json = "";

            using (var fs = File.OpenRead("content\\config.json"))
                using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
                    json = await sr.ReadToEndAsync();

            // next, let's load the values from that file
            // to our client's configuration
            var configJson = JsonConvert.DeserializeObject <ConfigJson>(json);
            var config     = new DiscordConfiguration
            {
                Token     = configJson.Token,
                TokenType = TokenType.Bot,

                AutoReconnect   = true,
                MinimumLogLevel = LogLevel.Debug
            };

            this.Prefix = configJson.CommandPrefix;
            print("Current Command Prefix: " + configJson.CommandPrefix);

            await LoadContent();

            IEnumerable <MessageCommand> exporters = typeof(MessageCommand)
                                                     .Assembly.GetTypes()
                                                     .Where(t => t.IsSubclassOf(typeof(MessageCommand)) && !t.IsAbstract)
                                                     .Select(t => (MessageCommand)Activator.CreateInstance(t));

            command = new Dictionary <string, Func <DiscordClient, MessageCreateEventArgs, Task> >();

            foreach (var item in exporters)
            {
                MessageCommand c = (MessageCommand)item;
                command.Add(this.Prefix + c.Command, new Func <DiscordClient, MessageCreateEventArgs, Task>(c.Function));
            }


            // then we want to instantiate our client
            this.Client = new DiscordClient(config);

            // next, let's hook some events, so we know
            // what's going on
            this.Client.Ready          += this.Client_Ready;
            this.Client.GuildAvailable += this.Client_GuildAvailable;
            this.Client.ClientErrored  += this.Client_ClientError;
            this.Client.MessageCreated += this.Client_MessageCreated;
            // finally, let's connect and log in
            await this.Client.ConnectAsync();

            // and this is to prevent premature quitting
            await Task.Delay(-1);
        }
Exemplo n.º 2
0
        private string GetMessage(MessageCommand command)
        {
            string message = "`" + command.Command;

            if (command.HelpArguments.Length > 0)
            {
                message += " " + command.HelpArguments;
            }

            message += "`";

            message += "** desc:** " + command.HelpDescription;

            message += "\n";

            return(message);
        }