Beispiel #1
0
        static async Task Main()
        {
            // Sets up Miki.Logging for internal library logging. Can be removed if you do not want to
            // see internal logs.
            ExampleHelper.InitLog(LogLevel.Information);

            // Fetches your token from environment values.
            var token = ExampleHelper.GetTokenFromEnv();

            var memCache = new InMemoryCacheClient(new ProtobufSerializer());

            var apiClient = new DiscordApiClient(token, memCache);

            // Discord direct gateway implementation.
            var gateway = new GatewayCluster(
                new GatewayProperties
            {
                ShardCount = 1,
                ShardId    = 0,
                Token      = token.ToString(),
            });

            var discordClient = new DiscordClient(apiClient, gateway, memCache);

            // Subscribe to ready event.
            discordClient.Events.MessageCreate.SubscribeTask(OnMessageReceived);

            // Start the connection to the gateway.
            await gateway.StartAsync();

            // Wait, else the application will close.
            await Task.Delay(-1);
        }
Beispiel #2
0
        static async Task MainAsync(string[] args)
        {
            // Enables library wide logging for all Miki libraries. Consider using this logging for debugging or general information.
            new LogBuilder().SetLogHeader(x => $"[{DateTime.UtcNow.ToLongTimeString()}]")
            .AddLogEvent((msg, level) =>
            {
                if (level >= logLevel)
                {
                    Console.WriteLine($"{msg}");
                }
            }).Apply();

            // A cache client is needed to store ratelimits and entities.
            // This is an in-memory cache, and will be used as a local storage repository.
            IExtendedCacheClient cache = new InMemoryCacheClient(
                new ProtobufSerializer()
                );

            // Discord REST API implementation gets set up.
            IApiClient api = new DiscordApiClient(Token, cache);

            // Discord direct gateway implementation.
            IGateway gateway = new GatewayCluster(new GatewayProperties
            {
                ShardCount             = 1,
                ShardId                = 0,
                Token                  = Token,
                Compressed             = true,
                WebSocketClientFactory = () => new BasicWebSocketClient()
            });

            // This function adds additional utility, caching systems and more.
            DiscordClient bot = new DiscordClient(new DiscordClientConfigurations
            {
                ApiClient   = api,
                Gateway     = gateway,
                CacheClient = cache
            });

            // Add caching events for the gateway.
            new BasicCacheStage().Initialize(gateway, cache);

            // Hook up on the MessageCreate event. This will send every message through this flow.
            bot.MessageCreate += async(msg) => {
                if (msg.Content == "ping")
                {
                    IDiscordTextChannel channel = await msg.GetChannelAsync();

                    await channel.SendMessageAsync("pong!");
                }
            };

            // Start the connection to the gateway.
            await gateway.StartAsync();

            // Wait, else the application will close.
            await Task.Delay(-1);
        }