예제 #1
0
 public static void FreeInstance()
 {
     if (instance == null)
     {
         return;
     }
     instance.Disconnect();
     instance.Dispose();
     instance = null;
 }
예제 #2
0
 static void Main(string[] args)
 {
     Console.CancelKeyPress += (s, e) => {
         VoiceBot.FreeInstance();
         Environment.Exit(0);
     };
     CommandLine.Parser.Default.ParseArguments <Options.CommandlineOptions>(args)
     .WithParsed <Options.CommandlineOptions>(opts => RunOptionsAndReturnExitCode(opts))
     .WithNotParsed <Options.CommandlineOptions>((errs) => HandleParseError(errs));
     while (true)
     {
         var command = Console.ReadLine();
         if (command != null && command.ToLower() == "exit")
         {
             VoiceBot.FreeInstance();
             Environment.Exit(0);
         }
     }
 }
예제 #3
0
 private static void RunOptionsAndReturnExitCode(Options.CommandlineOptions opts)
 {
     var t = VoiceBot.CreateInstanceAsync(opts);
 }
예제 #4
0
        public static async Task <VoiceBot> CreateInstanceAsync(CommandlineOptions opts)
        {
            if (instance != null)
            {
                // TRANSLATORS: Log message. Initializing Nursery.
                Logger.Log(T._("Already initialized."));
                return(instance);
            }
            // TRANSLATORS: Log message. Initializing Nursery.
            Logger.Log(T._("Load config ..."));
            try {
                Config.Initialize(opts);
                Config.Instance.Load();
                Config.Instance.MainConfig.Debug = opts.Debug;
                Logger.SetDebug(opts.Debug);
            } catch (Exception e) {
                // TRANSLATORS: Log message. Initializing Nursery.
                Logger.Log(T._("Could not load config. Check your config file."));
                Logger.DebugLog(e.ToString());
                return(null);
            }
            // TRANSLATORS: Log message. Initializing Nursery.
            Logger.Log(T._("Create VoiceBot ..."));
            try {
                instance = new VoiceBot();
            } catch (Exception e) {
                // TRANSLATORS: Log message. Initializing Nursery.
                Logger.Log(T._("Could not load config. Check your config file."));
                Logger.DebugLog(e.ToString());
                return(null);
            }
            // TRANSLATORS: Log message. Initializing Nursery.
            Logger.Log(T._("- set Discord events ..."));
            instance.discord.MessageReceived += instance.Client_MessageReceived;
            instance.discord.LoggedIn        += (async() => {
                await Task.Run(() => {
                    // TRANSLATORS: Log message. Initializing Nursery.
                    Logger.Log(T._("- Logged in!"));
                });
            });
            instance.discord.LoggedOut += (async() => {
                await Task.Run(() => {
                    // TRANSLATORS: Log message. Initializing Nursery.
                    Logger.Log(T._("- Logged out!"));
                });
            });
            instance.discord.Connected += (async() => {
                await Task.Run(() => {
                    // TRANSLATORS: Log message. Initializing Nursery.
                    Logger.Log(T._("- Connected!"));
                });
            });
            instance.discord.Disconnected += (async(ex) => {
                await Task.Run(() => {
                    // TRANSLATORS: Log message. Initializing Nursery.
                    Logger.Log(T._("- Disconnected!"));
                    if (ex != null)
                    {
                        // TRANSLATORS: DebugLog message. Initializing Nursery.
                        Logger.DebugLog(T._("Exception in disconnecting:"));
                        Logger.DebugLog(ex.ToString());
                    }
                });
            });
            // TRANSLATORS: Log message. Initializing Nursery.
            Logger.Log(T._("- login to Discord ..."));
            try {
                await instance.discord.LoginAsync(TokenType.Bot, Config.Instance.MainConfig.Token);

                await instance.discord.StartAsync();

                await instance.discord.SetGameAsync("Nursery");
            } catch (Exception e) {
                // TRANSLATORS: Log message. Initializing Nursery.
                Logger.Log(T._("Could not login to Discord. Please check bot account and its token."));
                Logger.DebugLog(e.ToString());
                instance.Dispose();
                instance = null;
                return(null);
            }
            var timeout = 0;

            while (true)
            {
                if (instance.discord.LoginState == LoginState.LoggedIn && instance.discord.ConnectionState == ConnectionState.Connected)
                {
                    // TRANSLATORS: Log message. Initializing Nursery.
                    Logger.Log(T._("- Logged in and Connected!"));
                    break;
                }
                if (timeout > 300)
                {
                    timeout = -1;
                    break;
                }
                timeout++;
                await Task.Delay(100);
            }
            if (timeout < 0)
            {
                // TRANSLATORS: Log message. Initializing Nursery.
                Logger.Log(T._("Could not login to Discord. Login challange was timeouted."));
                instance.Dispose();
                instance = null;
                return(null);
            }
            // TRANSLATORS: Log message. Initializing Nursery.
            Logger.Log(T._("Done!"));
            Logger.Log("/////////////");
            Logger.Log("// Nursery //");
            Logger.Log("/////////////");
            return(instance);
        }