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 static Embed Error(string message) { var builder = new EmbedBuilder(); return(builder.WithTitle("Error") .WithDescription(message) .WithColor(Discord.Color.Red) .WithThumbnailUrl(BotConfig.GetCachedConfig().Lang.EmbedImage) .Build()); }
public static async Task EditMessage(Embed e) { var client = Program._client; var guild = client.GetGuild(BotConfig.GetCachedConfig().Discord.GuildId); var channel = guild.GetTextChannel(BotConfig.GetCachedConfig().Discord.MessageChannelId); var message = await(channel.GetMessageAsync(BotConfig.GetCachedConfig().InternalMessageId)) as RestUserMessage; await message.ModifyAsync(m => { m.Embed = new Optional <Embed>(e); }); }
public static Embed ServerStopped(string footer = "") { var builder = new EmbedBuilder(); return(builder.WithTitle(BotConfig.GetCachedConfig().Lang.EmbedTitle) .WithDescription(BotConfig.GetCachedConfig().Lang.EmbedServerStopped) .WithColor(Discord.Color.Red) .WithFooter(footer) .WithThumbnailUrl(BotConfig.GetCachedConfig().Lang.EmbedImage) .Build()); }
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 static Embed ServerStopping() { var builder = new EmbedBuilder(); var footer = new StringBuilder(BotConfig.GetCachedConfig().Lang.EmbedFooter); footer.Replace("%ip%", BotConfig.GetCachedConfig().Minecraft.MinecraftServerIP); footer.Replace("%port%", BotConfig.GetCachedConfig().Minecraft.MinecraftServerPort.ToString()); return(builder.WithTitle(BotConfig.GetCachedConfig().Lang.EmbedTitle) .WithDescription(BotConfig.GetCachedConfig().Lang.EmbedServerStopping) .WithColor(Discord.Color.Orange) .WithFooter(footer.ToString()) .WithThumbnailUrl(BotConfig.GetCachedConfig().Lang.EmbedImage) .Build()); }
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; }
public async Task RunBotAsync() { _client = new DiscordSocketClient(); _services = new ServiceCollection() .AddSingleton(_client) .BuildServiceProvider(); string botToken = BotConfig.GetCachedConfig().Discord.BotToken; _client.Log += _client_Log; RegisterEvents(); await _client.LoginAsync(TokenType.Bot, botToken); await _client.StartAsync(); await Task.Delay(-1); }
public async Task StartServer() { Console.WriteLine("Starting server."); IsWorking = true; await Embeds.EditMessage(Embeds.ServerStarting()); var request = new StartInstancesRequest(); request.InstanceIds = new List <string>() { BotConfig.GetCachedConfig().Aws.EC2InstanceId }; var response = await client.StartInstancesAsync(request); bool suc = await AwaitServerStart(); if (suc) { while (true) { await Task.Delay((int)BotConfig.GetCachedConfig().PollingDelay); try { MineStat ms = new MineStat(BotConfig.GetCachedConfig().Minecraft.MinecraftServerIP, BotConfig.GetCachedConfig().Minecraft.MinecraftServerPort); if (ms.ServerUp) { Console.WriteLine("MC Server up."); break; } Console.WriteLine("MC Server is not up."); } catch { Console.WriteLine("Polling Minecraft server to see if it is online.. resulted in error."); } } await Embeds.EditMessage(Embeds.ServerStarted()); await StopServer(); } else { Console.WriteLine("Server start failed due to maxing out maxInstanceStartAttempts."); return; } }
private async Task <bool> AwaitServerStart() { ushort attempts = 0; while (attempts < BotConfig.GetCachedConfig().Aws.MaxInstanceStartAttempts) { await Task.Delay((int)BotConfig.GetCachedConfig().PollingDelay); var state = await this.GetState(); if (state.InstanceState.Code == 16) // 16 = instance started { return(true); } attempts++; } return(false); }
public async Task <InstanceStatus> GetState() { var request = new DescribeInstanceStatusRequest(); request.InstanceIds = new List <string>() { BotConfig.GetCachedConfig().Aws.EC2InstanceId }; request.IncludeAllInstances = true; // show instances that aren't running as well var response = await client.DescribeInstanceStatusAsync(request); InstanceStatus status = null; response.InstanceStatuses.ForEach(i => // unpack the response into an instance status { Console.WriteLine("checking state of instance " + i.InstanceId); status = i; }); return(status); }
private ServerStateManager() { client = new AmazonEC2Client(new BasicAWSCredentials(BotConfig.GetCachedConfig().Aws.EC2AccessKey, BotConfig.GetCachedConfig().Aws.EC2AccessSecret), RegionEndpoint.GetBySystemName(BotConfig.GetCachedConfig().Aws.Region)); }