示例#1
0
        public static void Create(Token token, string appName, Func <Client, Task> runner)
        {
            DiscordDebug.WriteLine("Creating client...", appName);
            var env = new DiscordEnvironment(new Client(token), appName);
            var t   = new Thread(() => {
                AsyncContext.Switch(async() => {
                    await env.RunAsync(async() => {
                        DiscordDebug.WriteLine("Client created.", appName);
                        await runner(env.Client);
                    });
                });
            });

            t.Start();
        }
示例#2
0
        private static void CreateGateway(Token token, string appName, GatewayOptions options, int shardId, int shardCount, Func <GatewayClient, Task> runner)
        {
            DiscordDebug.WriteLine("Creating gateway client...", appName);
            var env = new DiscordEnvironment(new GatewayClient(token, shardId, shardCount), appName);
            var t   = new Thread(() => {
                AsyncContext.Switch(async() => {
                    await env.RunAsync(async() => {
                        env.GatewayClient.FirePinEvents = options.HasFlag(GatewayOptions.FirePinEvents);
                        Server[] servers = null;
                        if (!options.HasFlag(GatewayOptions.ManualConnect))
                        {
                            DiscordDebug.WriteLine("Connecting...", appName);
                            servers = await env.GatewayClient.ConnectAsync();
                            DiscordDebug.WriteLine("Connected.", appName);
                        }
                        DiscordDebug.WriteLine("Gateway client created.", appName);
                        if (env.GatewayClient.FirePinEvents && servers != null)
                        {
                            DiscordDebug.WriteLine("Getting initial channel pins...");
                            var tasks = new List <Task>();
                            // ReSharper disable LoopCanBeConvertedToQuery (confusing)
                            foreach (Server s in servers)
                            {
                                foreach (IChannel channel in s.Channels.Values.Where(x => x is ServerTextChannel))
                                {
                                    var c = (ServerTextChannel)channel;
                                    tasks.Add(c.GetPinnedMessagesAsync(env.Client));
                                }
                            }
                            // ReSharper restore LoopCanBeConvertedToQuery
                            await Task.WhenAll(tasks);
                            DiscordDebug.WriteLine("Initial channel pins retreived.");
                        }
                        await runner((GatewayClient)env.Client);
                    });
                });
            });

            t.Start();
        }
        public static Thread CreateSubthread(ThreadStart start, int maxStackSize)
        {
            DiscordEnvironment env = Current;

            return(new Thread(() => {
                DiscordDebug.WriteLine("Starting environment thread...", env?.AppName);
                try {
                    if (env != null)
                    {
                        env.Run(() => {
                            start();
                        });
                    }
                    else
                    {
                        start();
                    }
                } catch (Exception e) {
                    DiscordDebug.WriteLine(e.ToString());
                } finally {
                    DiscordDebug.WriteLine("Environment thread done.", env?.AppName);
                }
            }, maxStackSize));
        }