Example #1
0
        public static async Task MainAsync(string[] args)
        {
            // Read the config json file ...
            using FileStream fs   = new(Path.Join("Config", "bot_config.json"), FileMode.Open);
            using StreamReader sr = new(fs);
            var json = await sr.ReadToEndAsync();

            var jobj = JObject.Parse(json);

            // ... create a new DiscordClient for the bot ...
            Discord = new DiscordClient(new DiscordConfiguration
            {
                Token      = jobj["token"].ToString(),
                TokenType  = TokenType.Bot,
                ShardCount = 1,
                Intents    = DiscordIntents.AllUnprivileged
            });
            // ... register commands ...
            var next = Discord.UseCommandsNext(new CommandsNextConfiguration
            {
                StringPrefixes = new string[] { jobj["prefix"].ToString() }
            });

            next.RegisterCommands(Assembly.GetExecutingAssembly());
            // ... connect to discord ...
            await Discord.ConnectAsync();

            var defaultResponseData = new InteractionApplicationCommandCallbackDataBuilder()
                                      .WithContent("`Test Automated Response`");

            IServiceCollection c = new ServiceCollection();

            c.AddTransient <TestService>();

            // ... use the discord connection to build the Slash Client config ...
            Slash = new DiscordSlashClient(new DiscordSlashConfiguration
            {
                Client = Discord,
                Token  = jobj["token"].ToString(),
                DefaultResponseType = InteractionResponseType.ChannelMessageWithSource,
                DefaultResponseData = defaultResponseData
            }, c);

            Slash.RegisterCommands(Assembly.GetExecutingAssembly());

            // ... start the slash client ...
            await Slash.StartAsync();

            // ... build the web server for receiving HTTP POSTs from discord ...
            var host = Host.CreateDefaultBuilder(args)
                       .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup <Startup>();
            });
            // ... and start the webserver ...
            await host.Build().StartAsync();

            // ... then hold here to prevent premature closing.
            await Task.Delay(-1);
        }
        static void Main(string[] args)
        {
            // Read the config json file ...
            using FileStream fs   = new(Path.Join("Config", "bot_config.json"), FileMode.Open);
            using StreamReader sr = new(fs);
            var json = sr.ReadToEnd();

            var jobj = JObject.Parse(json);

            // ... create a new DiscordClient for the bot ...
            Discord = new DiscordClient(new DiscordConfiguration
            {
                Token           = jobj["token"].ToString(),
                TokenType       = TokenType.Bot,
                ShardCount      = 1,
                Intents         = DiscordIntents.AllUnprivileged,
                MinimumLogLevel = Microsoft.Extensions.Logging.LogLevel.Debug
            });

            // ... create a custom default response ...
            var defaultResponseData = new InteractionApplicationCommandCallbackDataBuilder()
                                      .WithContent("`Test Automated Response`");

            // ... use the discord client to build the Slash Client config ...
            Slash = new DiscordSlashClient(new DiscordSlashConfiguration
            {
                Client = Discord,
                Token  = jobj["token"].ToString(),
                DefaultResponseType = InteractionResponseType.ChannelMessageWithSource,
                DefaultResponseData = defaultResponseData,
                Logger = Discord.Logger
            });

            // ... register normal bot commands ...
            var next = Discord.UseCommandsNext(new CommandsNextConfiguration
            {
                StringPrefixes = new string[] { jobj["prefix"].ToString() }
            });

            next.RegisterCommands(Assembly.GetExecutingAssembly());
            // ... register the interaction event ...
            Discord.InteractionCreated += Slash.HandleGatewayEvent;
            Discord.InteractionCreated += (x, y) =>
            {
                Discord.Logger.LogInformation("Interaction Created Received");
                return(Task.CompletedTask);
            };
            Discord.ApplicationCommandCreated += Discord_ApplicationCommandCreated;
            Discord.ApplicationCommandDeleted += Discord_ApplicationCommandDeleted;
            Discord.ApplicationCommandUpdated += Discord_ApplicationCommandUpdated;

            // ... connect to discord ...
            Discord.ConnectAsync().GetAwaiter().GetResult();
            // ... register the slash commands ...
            Slash.RegisterCommands(Assembly.GetExecutingAssembly());

            // ... start the slash client ...
            Slash.StartAsync().GetAwaiter().GetResult();

            // ... and prevent this from stopping.
            Task.Delay(-1).GetAwaiter().GetResult();
        }
 public InteractionResponseBuilder WithData(InteractionApplicationCommandCallbackDataBuilder data)
 {
     Data = data;
     return(this);
 }