Exemple #1
0
        public async Task Start()
        {
            if (_client.GetGuild(BotConfig.GetCachedConfig().Discord.GuildId) != null)
            {
                var guild = _client.GetGuild(BotConfig.GetCachedConfig().Discord.GuildId);
                if (guild.GetTextChannel(BotConfig.GetCachedConfig().Discord.MessageChannelId) != null)
                {
                    var chan = guild.GetTextChannel(BotConfig.GetCachedConfig().Discord.MessageChannelId);
                    if (BotConfig.GetCachedConfig().InternalMessageId == 0 || await chan.GetMessageAsync(BotConfig.GetCachedConfig().InternalMessageId) == null)
                    {
                        var msg = await chan.SendMessageAsync(embed : Embeds.ServerStopped());

                        await msg.AddReactionAsync(new Emoji("🔄"));

                        var cfg = BotConfig.GetCachedConfig();
                        cfg.InternalMessageId = (ulong)msg.Id;
                        BotConfig.SaveConfig(cfg);
                    }
                }
                else
                {
                    Console.WriteLine("Could not find channel specified in the config file.");
                }
            }
            else
            {
                Console.WriteLine("Could not find guild specified in config file.");
            }
        }
        public async Task Handle(Cacheable <IUserMessage, ulong> msg, ISocketMessageChannel chan, SocketReaction r)
        {
            if (r.UserId == Program._client.CurrentUser.Id)
            {
                return;
            }
            if (r.MessageId == BotConfig.GetCachedConfig().InternalMessageId)
            {
                if (r.Emote.Name == "🔄")
                {
                    var actualMessage = await chan.GetMessageAsync(r.MessageId);

                    await actualMessage.RemoveReactionAsync(r.Emote, r.UserId);

                    if (!debounce && !ServerStateManager.Instance().IsWorking) // ignore attempts to spam the button
                    {
                        debounce = true;
                        var status = await ServerStateManager.Instance().GetState();

                        if (status.InstanceState.Code == 80)
                        {
                            Console.WriteLine("User ID" + r.UserId + " initated a server start.");
                            ThreadPool.QueueUserWorkItem(async delegate
                            {
                                try
                                {
                                    await ServerStateManager.Instance().StartServer();
                                }
                                catch (AmazonEC2Exception e)
                                {
                                    Console.WriteLine("AWS threw an error.");
                                    Console.Write(e.Message);
                                    ServerStateManager.Instance().IsWorking = false;
                                    await Embeds.EditMessage(Embeds.ServerStopped("The server could not be started previously due to an error. Please wait a bit and try again, or ask an admin if this persists. This mainly happens after starting a server when it's just been stopped."));
                                }
                            });
                        }
                        else
                        {
                            Console.WriteLine("Rejecting refresh as server is not stopped.");
                            if (Program._client.GetUser(r.UserId) != null)
                            {
                                var usr = await chan.GetUserAsync(r.UserId);

                                await usr.SendMessageAsync(embed : Embeds.Error("The server must be stopped for the refresh button to work."));
                            }
                        }
                        debounce = false;
                    }
                }
            }
        }
        public async Task StopServer()
        {
            while (true)
            {
                await Task.Delay((int)BotConfig.GetCachedConfig().PollingDelay);

                MineStat ms = new MineStat(BotConfig.GetCachedConfig().Minecraft.MinecraftServerIP, BotConfig.GetCachedConfig().Minecraft.MinecraftServerPort);
                Console.WriteLine("Current player count: " + ms.CurrentPlayers);
                if (ms.CurrentPlayers == "0")
                {
                    Console.WriteLine("Server player is count is zero. Sleeping, and checking again.");
                    await Task.Delay((int)BotConfig.GetCachedConfig().StopServerDelay);

                    ms = new MineStat(BotConfig.GetCachedConfig().Minecraft.MinecraftServerIP, BotConfig.GetCachedConfig().Minecraft.MinecraftServerPort);
                    if (ms.ServerUp && ms.CurrentPlayers == "0")
                    {
                        Console.WriteLine("Player count is still zero. Stopping.");
                        await Embeds.EditMessage(Embeds.ServerStopping());

                        var stopRequest = new StopInstancesRequest();
                        var request     = new StopInstancesRequest();
                        request.InstanceIds = new List <string>()
                        {
                            BotConfig.GetCachedConfig().Aws.EC2InstanceId
                        };
                        request.Force = false;
                        var response = await client.StopInstancesAsync(request);

                        ushort pollCounter = 0;
                        while (pollCounter < BotConfig.GetCachedConfig().Aws.MaxInstanceStartAttempts)
                        {
                            pollCounter++;
                            await Task.Delay(5000);

                            var serverState = await this.GetState();

                            if (serverState.InstanceState.Code == 80)
                            {
                                break;
                            }
                        }
                        await Embeds.EditMessage(Embeds.ServerStopped());

                        break;
                    }
                }
            }
            IsWorking = false;
        }