Example #1
0
        public async Task ServerStatus(string host = "")
        {
            if (string.IsNullOrWhiteSpace(_rconPassword) || string.IsNullOrWhiteSpace(_rconHost) || _rconPort == 0)
            {
                return;
            }

            var rconPassword = _rconPassword;
            var rconPort     = _rconPort;
            var rconHost     = _rconHost;

            if (!string.IsNullOrEmpty(host))
            {
                var server = await _serverRepository.Find(Context.Guild.Id, host);

                if (server != null)
                {
                    rconPort     = server.Port;
                    rconHost     = server.Host;
                    rconPassword = !string.IsNullOrWhiteSpace(server.RconPassword) ?
                                   EncryptionProvider.AESDecrypt(server.RconPassword, _encryptionSettings.Key, _encryptionSettings.IV) :
                                   string.Empty;

                    if (string.IsNullOrWhiteSpace(rconPassword) || string.IsNullOrWhiteSpace(rconHost) || rconPort == 0)
                    {
                        await Context.Message.ReplyAsync(
                            $"Can't show server status since no rcon password has been set for the server {host}")
                        .AutoRemoveMessage(15);

                        return;
                    }
                }
            }

            try
            {
                var response = await RCON.UDPSendCommand("status", rconHost, rconPassword, rconPort).ConfigureAwait(false);

                _logger.LogInformation($"serverstatus response: {response}");
                var serverStatus = new ServerStatus(response);

                var embed = new EmbedBuilder
                {
                    Title       = $"Server status for {rconHost}",
                    Description = $"**Map:** _{serverStatus.Map} _" +
                                  $"{Environment.NewLine}" +
                                  "**Players**" +
                                  $"{Environment.NewLine}"
                };

                embed.Description += $"```{Environment.NewLine}" +
                                     (serverStatus.Players.Any() ?
                                      $"{serverStatus.PlayersToTable()}" :
                                      "No players are currently online") +
                                     $"{Environment.NewLine}```";

                await Context.Message.ReplyAsync(embed : embed.Build());
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
            }
        }