public async Task SetRconPassword(string host)
        {
            var server = await _serverRepository.Find(Context.Guild.Id, host);

            if (server != null)
            {
                await ReplyAndDeleteAsync($"Check your DM's for information on how to set the password {Context.User.Mention}",
                                          timeout : TimeSpan.FromSeconds(10));

                var msg = await Context.User.SendMessageAsync($"Type in the rcon password you wish to set for the `{server.Host}` server");

                var response = await NextMessageAsync(new EnsureFromUserDmCriterion(msg.Channel, Context.User), TimeSpan.FromSeconds(30));

                if (!string.IsNullOrWhiteSpace(response?.Content))
                {
                    server.RconPassword = EncryptionProvider.AESEncrypt(response.Content, _encryptionSettings.Key, _encryptionSettings.IV);
                    var result = await _serverRepository.Save(server);

                    if (result)
                    {
                        await Context.User.SendMessageAsync($"Rcon password for server `{server.Host}` has been saved");

                        return;
                    }
                }

                await Context.User.SendMessageAsync($"Failed to save the password for the `{server.Host}` server");
            }
            else
            {
                await ReplyAndDeleteAsync($"Could not find a server with host `{host}`", timeout : TimeSpan.FromSeconds(10));
            }
        }
        public async Task TriggerRconNotification(PickupQueue queue)
        {
            if (!queue.Rcon)
            {
                return;
            }

            var rconPassword = "";
            var rconPort     = 0;
            var rconHost     = "";

            var server = await _serverRepository.Find(Convert.ToUInt64(queue.GuildId), queue.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)
            {
                return;
            }

            try
            {
                var redTeam  = queue.Teams.FirstOrDefault();
                var blueTeam = queue.Teams.LastOrDefault();

                var command = $"say \"^2Pickup '^3{queue.Name}^2' has started! " +
                              $"^1RED TEAM: ^5{string.Join(", ", redTeam.Subscribers.Select(w => w.Name))} ^7- " +
                              $"^4BLUE TEAM: ^5{string.Join(", ", blueTeam.Subscribers.Select(w => w.Name))}\"";

                await RCON.UDPSendCommand(command, rconHost, rconPassword, rconPort, true);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
            }
        }
Beispiel #3
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);
            }
        }