예제 #1
0
파일: Bot.cs 프로젝트: remmody/Pootis-Bot
        /// <summary>
        /// Starts the bot
        /// </summary>
        /// <returns></returns>
        public async Task StartBot()
        {
            IsStreaming          = false;
            IsRunning            = true;
            Global.BotStatusText = Config.bot.DefaultGameMessage;

            //Make sure the token isn't null or empty, if so open the bot config menu.
            if (string.IsNullOrEmpty(Global.BotToken))
            {
                new ConfigMainMenu().OpenConfigMenu();
            }

            Logger.Debug("Creating new Discord client...");

            Client = new DiscordSocketClient(new DiscordSocketConfig
            {
                LogLevel = LogSeverity.Verbose
            });

            Logger.Debug("Setting up events");

            //Setup client events
            Client.Log   += Log;
            Client.Ready += BotReady;

            //Setup the remaining events
            EventsSetup unused = new EventsSetup(Client);

            Logger.Debug("Signing in using token...");

            try
            {
                await Client.LoginAsync(TokenType.Bot,
                                        Global.BotToken); //Logging into the bot using the token in the config.
            }
            catch (Discord.Net.HttpException)
            {
                Logger.Error("The supplied token was invalid!");
                await EndBot();

                Environment.Exit(0);

                return;
            }
            catch (HttpRequestException)
            {
                Logger.Error("There was an error connecting to Discord! This may be because Discord API is down, or there is no internet connection.");
                await EndBot();

                Environment.Exit(0);

                return;
            }

            await Client.StartAsync();             //Start the client

            Logger.Debug("Sign in successful!");

            CommandHandler handler = new CommandHandler(Client);

            Logger.Debug("Installing commands...");

            //Install all the Modules
            await handler.SetupCommandHandlingAsync();

            //Check all help modules
            HelpModulesManager.CheckHelpModules();

            //Bot owner
            Global.BotOwner = (await Client.GetApplicationInfoAsync()).Owner;

            Logger.Debug($"The owner of this bot is {Global.BotOwner}");

            //Enable the Steam services if an api key is provided
            if (!string.IsNullOrWhiteSpace(Config.bot.Apis.ApiSteamKey))
            {
                SteamService.SetupSteam();
            }

            //Set the bot status to the default game status
            await Client.SetGameAsync(Config.bot.DefaultGameMessage);

            //Starts the check connection status task, which will run indefinitely until the bot is stopped.
            await CheckConnectionStatusTask();
        }