示例#1
0
        public static void Shutdown()
        {
            TelegramIntegration.StopPoll();
            Updater.Stop();

            if (Client != null)
            {
                try
                {
                    "Disconnecting..".Log();

                    foreach (ServerData SD in ServerData.Servers.Values)
                    {
                        $"{SD.Music.Save(SD.Server)} songs saved in {SD.Name}".Log();
                        SD.StopHandlers();
                    }

                    Client.Disconnect().Wait();
                }
                catch { }
                finally
                {
                    Client = null;
                }
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            Console.Title = "Loading..";

            int Width  = 93;
            int Height = 23;

            Console.SetWindowSize(Width, Height);
            Console.SetBufferSize(Width, 512);

            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Write(string.Empty.PadLeft(Width - 1, '-') +
                          "\n [<< Kuriyama Mirai Bot for Discord created by Amir Zaidi, built on the Discord.Net API >>] \n" +
                          string.Empty.PadLeft(Width - 1, '-'));

            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine();

            if (!File.Exists(CredentialsFile))
            {
                "Create a new bot account at https://discordapp.com/developers/applications/me".Log();

                Console.Write("[Text] Bot Token: ");
                Token = Console.ReadLine();

                Console.Write("[Number] Application Id: ");
                while (!ulong.TryParse(Console.ReadLine(), out AppId))
                {
                    ;
                }

                "You can find your id (the bot owner's id) by typing \\@YourName in a server".Log();

                Console.Write("[Number] Owner Id: ");
                while (!ulong.TryParse(Console.ReadLine(), out Owner))
                {
                    ;
                }

                File.WriteAllText(CredentialsFile, Token + "\r\n" + AppId + "\r\n" + Owner);
            }

            var Credentials = File.ReadAllText(CredentialsFile).Replace("\r", string.Empty).Split('\n');

            Token = Credentials[0];
            if (ulong.TryParse(Credentials[1], out AppId))
            {
                InviteLink.Log();
            }

            if (Credentials.Length <= 2 || !ulong.TryParse(Credentials[2], out Owner))
            {
                "Couldn't load owner id from credentials".Log();
            }

            if (Credentials.Length > 3)
            {
                MainDir = Credentials[3];
            }

            Client = new DiscordClient();
            Client.UsingAudio(new AudioServiceConfigBuilder()
            {
                Channels          = 2,
                EnableEncryption  = false,
                EnableMultiserver = true,
                Bitrate           = AudioServiceConfig.MaxBitrate,
                BufferLength      = 1000,
                Mode = AudioMode.Outgoing
            }.Build());

            MusicHandler.Buffers = new ByteBuffer(1920 * 2, (int)Math.Pow(2, 16));

            InitCommands();

            Client.Log.Message     += ClientEvents.LogMessage;
            Client.MessageReceived += ClientEvents.MessageReceived;

            Client.UserJoined += ClientEvents.UserJoined;
            Client.UserLeft   += ClientEvents.UserLeft;

            Client.JoinedServer    += ClientEvents.JoinedServer;
            Client.ServerAvailable += ClientEvents.ServerAvailable;
            Client.LeftServer      += ClientEvents.LeftServer;

            try
            {
                new Func <Task>(async delegate
                {
                    await Client.Connect(Token);
                }).UntilNoExceptionAsync <Discord.Net.WebSocketException>(3).Wait();
            }
            catch (AggregateException)
            {
            }

            if (Client.State != ConnectionState.Connected)
            {
                Console.WriteLine("Could not connect to Discord. Type 'delete' to delete the current credentials file");

                if (Console.ReadLine() == "delete")
                {
                    File.Delete(CredentialsFile);
                }

                return;
            }

            Updater           = new Timer(1000);
            Updater.Elapsed  += UpdateTitle;
            Updater.AutoReset = true;
            Start             = DateTime.Now;
            Updater.Start();

            "The Discord functions have successfully booted".Log();

            handler = new ConsoleEventDelegate(ConsoleEventCallback);
            SetConsoleCtrlHandler(handler, true);

            AppDomain.CurrentDomain.UnhandledException += (s, e) =>
            {
                var StartInfo = Process.GetCurrentProcess().StartInfo;
                StartInfo.FileName = Process.GetCurrentProcess().MainModule.FileName;
                Process.Start(StartInfo);

                Shutdown();
            };

            if (File.Exists(TelegramFile))
            {
                TelegramIntegration.Start(File.ReadAllText(TelegramFile).Trim()).Forget();
            }

            Client.Wait();
        }