コード例 #1
0
ファイル: DockerContainer.cs プロジェクト: tomhobson/Sergen
        public DockerContainer(ILogger logger, IChatResponseToken icrt, GameServer gs)
        {
            _icrt      = icrt;
            _logger    = logger;
            GameServer = gs;

            var task = _icrt.Respond($"Current status is: Initialising...");

            _initialMessageID = task.Result;
        }
コード例 #2
0
        private async Task <bool> VerifyUser(string serverId, string userId, IChatResponseToken icrt)
        {
            if (await _allowList.IsUserAllowed(serverId, userId))
            {
                return(true);
            }

            await icrt.Respond("I'm not allowed to talk to you, ask your server admin to allowlist you!");

            return(false);
        }
コード例 #3
0
        public async Task StopById(string serverId, IChatResponseToken icrt, string containerId)
        {
            var messageId = await icrt.Respond($"Stopping game server {containerId}.");

            var success = await StopAndRemove(containerId);

            if (success)
            {
                await icrt.Update(messageId, $"Game server {containerId} removed.");
            }
            else
            {
                await icrt.Update(messageId, "No servers to stop found.");
            }
        }
コード例 #4
0
        public async Task Stop(string serverId, IChatResponseToken icrt, GameServer gameServer)
        {
            var allContainers = await GetAllContainersRanByServer(serverId);

            var gameContainers = allContainers.Where(x => x.Image.Contains(gameServer.ContainerName)).ToList();

            if (gameContainers.Any() == false)
            {
                await icrt.Respond("No servers to stop found.");

                return;
            }

            if (gameContainers.Count() == 1)
            {
                var messageId = await icrt.Respond($"Stopping game server {gameServer.ServerName}.");

                await StopAndRemove(gameContainers[0].ID);

                await icrt.Update(messageId, $"Game server {gameServer.ServerName} removed.");

                _logger.LogInformation($"{gameServer.ServerName} for ServerId:{serverId} stopped and removed.");
            }

            if (gameContainers.Count() > 1)
            {
                string runningContainerIds = "";

                foreach (var cont in gameContainers)
                {
                    runningContainerIds += $"{cont.ID} {cont.Image} {cont.Created} \n";
                }

                await icrt.Respond($"Which container would you like me to stop? `-stop *containerid*`\n Containers: {runningContainerIds}");
            }
        }
コード例 #5
0
        private async Task AttemptRun(string serverId, string input, IChatResponseToken responseToken)
        {
            // Time to do some work
            var serverName = ChatHelper.PreParseInputString(input.Replace("-run ", "").Replace("-start ", ""));
            var gameServer = _serverStore.GetGameServerByName(serverName, GetContainerInterfaceType());

            if (gameServer == null)
            {
                await responseToken.Respond("Image could not be found. Find all possible game servers with -possible");

                return;
            }
            var contId = await _containerInterface.Setup(responseToken, gameServer);

            await _containerInterface.Run(serverId, responseToken, contId);
        }
コード例 #6
0
        public async Task ProcessMessage(string serverID, IChatResponseToken icrt, string senderID, string input)
        {
            // Make the initial command case insensitive
            var firstCommand = input.Split(" ")[0].ToLower();

            // Switch statement for all commands that are constant
            switch (firstCommand)
            {
            case "-help":
                await icrt.Respond(@"Command list:
                    `-ping` Will return if Sergen is alive.
                    `-ip` Will respond the ip of the main.
                    `-version` Will return 1.0.0 because I'm too lazy to fix the version.
                    `-running` Will return all the running game servers for this discord server.
                    `-possible` Will return all possible game servers.
                    `-run {Game Server}` Will start a game server of that type.
                    `-stop {Game Server}` Will stop a game server of that type.
                    `-allowlist` Will show you your current allowlist.
                    `-allowlist enable/disable` Will enable/disable the allowlist.
                    `-allowlist add @user` Will enable that user to control servers.
                    `-allowlist remove @user` Will stop that user from controlling servers.
                     ");

                break;

            case "-ping":
                var processingTime = DateTime.Now.Subtract(icrt.SendTime);
                await icrt.Respond(processingTime.Milliseconds + "ms");

                break;

            case "-ip":
                await icrt.Respond($"My IP Address is: {await _ipGetter.GetIp()}");

                break;

            case "-whoami":
                await icrt.Respond($"You are: {_context.GetUsername(senderID)}");

                break;

            case "-version":
                System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
                FileVersionInfo            fvi      = FileVersionInfo.GetVersionInfo(assembly.Location);
                string version = fvi.FileVersion;
                await icrt.Respond($"My version is: {version}");

                break;

            case "-running":
                var allcontainers = ObjectToString.Convert(await _containerInterface.GetRunningContainers(serverID));
                await icrt.Respond($"Running containers are: {allcontainers}");

                break;

            case "-possible":
                var servers          = _serverStore.GetAllServers(GetContainerInterfaceType());
                var serverNames      = servers.Select(s => s.ServerName).ToList();
                var serverStringList = ObjectToString.Convert(serverNames);
                await icrt.Respond($"Possible game servers are: {serverStringList}");

                break;
            }

            if (input.StartsWith("-run ") || input.StartsWith("-start "))
            {
                if (await VerifyUser(serverID, senderID, icrt))
                {
                    await AttemptRun(serverID, input, icrt);
                }
                return;
            }

            if (input.StartsWith("-stop "))
            {
                if (await VerifyUser(serverID, senderID, icrt))
                {
                    await AttemptStop(serverID, input, icrt);
                }
                return;
            }

            if (input.StartsWith("-allowlist"))
            {
                if (await _allowList.IsUserAllowedToManage(serverID, senderID))
                {
                    if (input.StartsWith("-allowlist add "))
                    {
                        await _allowList.AddUser(serverID, input.Replace("-allowlist add ", ""));

                        await icrt.Respond("User added.");

                        return;
                    }

                    if (input.StartsWith("-allowlist remove "))
                    {
                        await _allowList.RemoveUser(serverID, input.Replace("-allowlist remove ", ""));

                        await icrt.Respond("User removed.");

                        return;
                    }

                    if (input.StartsWith("-allowlist enable"))
                    {
                        await _allowList.SetAllowListStatus(serverID, true);

                        await icrt.Respond("Allow list enabled.");

                        return;
                    }

                    if (input.StartsWith("-allowlist disable"))
                    {
                        await _allowList.SetAllowListStatus(serverID, false);

                        await icrt.Respond("Allow list disabled.");

                        return;
                    }

                    var allowList = await _allowList.GetAllowList(serverID);

                    var rawAllowList = string.Join("\n", allowList.Select(x => x.ToString()).ToArray());
                    await icrt.Respond("Allow list:\n" + rawAllowList);

                    return;
                }

                await icrt.Respond("I'm not allowed to talk to you, ask your server admin to allowlist you!");
            }
        }