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.AddHostedService<MonthlyRestoreBackgroundService>();
29 services.AddGatewayHandlers(typeof(Program).Assembly);
30 })
31 .UseDiscordGateway(options =>
32 {
33 options.Intents = GatewayIntents.Guilds
34 | GatewayIntents.GuildMessages
35 | GatewayIntents.MessageContent;
36 })
37 .UseApplicationCommands();
38
39 var host = builder.Build();
40
41 using (var scope = host.Services.CreateScope())
42 {
43 var db = scope.ServiceProvider.GetRequiredService<StreakDbContext>();
44 await db.Database.MigrateAsync();
45 }
46
47 host.AddSlashCommand("ping", "Ping!", () => "Pong!");
48 host.AddModules(typeof(Program).Assembly);
49
50 await host.RunAsync();
51 }
52}