Beispiel #1
0
        private string FindToken(IGuildChannel channel, AtisGuildRecord guildData)
        {
            if (guildData.BotTokenByChannelName.ContainsKey(channel.Name))
            {
                // If the channel is already handled by a bot, restart to change audio
                var token = guildData.BotTokenByChannelName[channel.Name];
                if (guildData.ProcessesByToken.TryGetValue(token, out var currentProcess))
                {
                    currentProcess.Kill();
                    logger.LogDebug("Kill existing process");
                    guildData.ProcessesByToken.Remove(token);
                }
                return(token);
            }
            else
            {
                // Find available token
                string availableToken = null;
                foreach (var token in atisOptions.BotTokens)
                {
                    if (!guildData.ProcessesByToken.ContainsKey(token))
                    {
                        availableToken = token;
                        break;
                    }
                }

                if (availableToken == null)
                {
                    throw new Exception("Cannot find any available ATIS bot! Please try to stop one first.");
                }

                return(availableToken);
            }
        }
Beispiel #2
0
        private void StartProcess(IGuildChannel channel, string filePath, string botToken, AtisGuildRecord guildData, string nickname)
        {
            var arguments = $"--BotToken {botToken} --AudioFilePath {filePath} --ServerId {channel.Guild.Id} --ChannelId {channel.Id} --Nickname \"{nickname}\"";

            logger.LogDebug("Launching Bot {filePath} {arguments}", atisOptions.BotExecutionPath, arguments);
            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName         = atisOptions.BotExecutionPath,
                    WorkingDirectory = Path.GetDirectoryName(atisOptions.BotExecutionPath),
                    Arguments        = arguments
                }
            };

            process.Start();

            guildData.ProcessesByToken.TryAdd(botToken, process);
            guildData.BotTokenByChannelName.TryAdd(channel.Name, botToken);

            //if (!process.HasExited)
            //{
            //    process.WaitForExit();
            //}
        }