C# Discord bot made using NetCord for keeping a TikTok-style streak
1using Microsoft.EntityFrameworkCore;
2using Microsoft.Extensions.DependencyInjection;
3using Microsoft.Extensions.Hosting;
4using NetCord.Gateway;
5using NetCord.Hosting.Gateway;
6using NetCord.Hosting.Services;
7using NetCord.Hosting.Services.ApplicationCommands;
8using StreakBot.Data;
9using StreakBot.Services;
10
11namespace StreakBot;
12
13public class Program
14{
15 public static async Task Main(string[] args)
16 {
17 var dataPath = Environment.GetEnvironmentVariable("DATA_PATH") ?? ".";
18 var connectionString = $"Data Source={Path.Combine(dataPath, "streaks.db")}";
19
20 var builder = Host.CreateDefaultBuilder(args)
21 .ConfigureServices(services =>
22 {
23 services.AddDbContext<StreakDbContext>(options =>
24 options.UseSqlite(connectionString));
25 services.AddScoped<ChannelService>();
26 services.AddScoped<StreakService>();
27 services.AddHostedService<StreakResetBackgroundService>();
28 services.AddGatewayHandlers(typeof(Program).Assembly);
29 })
30 .UseDiscordGateway(options =>
31 {
32 options.Intents = GatewayIntents.Guilds
33 | GatewayIntents.GuildMessages
34 | GatewayIntents.MessageContent;
35 })
36 .UseApplicationCommands();
37
38 var host = builder.Build();
39
40 using (var scope = host.Services.CreateScope())
41 {
42 var db = scope.ServiceProvider.GetRequiredService<StreakDbContext>();
43 await db.Database.MigrateAsync();
44 }
45
46 host.AddSlashCommand("ping", "Ping!", () => "Pong!");
47 host.AddModules(typeof(Program).Assembly);
48
49 await host.RunAsync();
50 }
51}