-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
169 lines (157 loc) · 6.7 KB
/
Program.cs
File metadata and controls
169 lines (157 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.IO;
using System.Linq;
using AspNetCore.ReCaptcha;
using devanewbot.Authorization;
using devanewbot.Entities;
using devanewbot.HostedServices;
using devanewbot.Services;
using devanewbot.SlackDotNet.Options;
using Devanewbot.Discord;
using Hangfire;
using Hangfire.Redis.StackExchange;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using RollbarDotNet.Configuration;
using RollbarDotNet.Core;
using RollbarDotNet.Logger;
using SlackDotNet;
using SlackNet.AspNetCore;
using SlackNet.Blocks;
using SlackNet.Events;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile("appsettings.local.json", true)
.AddEnvironmentVariables()
.AddCommandLine(args);
var configuration = builder.Configuration;
var suffix = configuration.GetSection("SlackSocket").GetValue<string>("CommandSuffix");
builder.Services
.AddDbContext<DevanewbotContext>(options =>
options.UseNpgsql(configuration.GetConnectionString("DevanewbotContext")))
.AddHttpClient()
.AddSingleton<Slack>()
.AddSingleton<Client>()
.AddTransient<InviteService>()
.AddTransient<IChannelBanService, ChannelBanService>()
.AddTransient<IWelcomeService, WelcomeService>()
.AddSlackNet(c =>
{
c
.UseSigningSecret(configuration.GetSection("Slack").GetValue<string>("VerificationToken")!)
.UseApiToken(configuration.GetSection("Slack").GetValue<string>("OauthToken"))
.UseAppLevelToken(configuration.GetSection("SlackSocket").GetValue<string>("AppToken"));
c
.RegisterSlashCommandHandler<SpongebobCommand>("/spongebob" + suffix)
.RegisterSlashCommandHandler<GifCommand>("/jif")
.RegisterBlockActionHandler<ButtonAction, GifCommand>("post")
.RegisterBlockActionHandler<ButtonAction, GifCommand>("random")
.RegisterBlockActionHandler<ButtonAction, GifCommand>("cancel")
.RegisterBlockActionHandler<ButtonAction, InviteService>("approve_invite")
.RegisterBlockActionHandler<ButtonAction, InviteService>("decline_invite")
.RegisterSlashCommandHandler<StallmanCommand>("/stallman" + suffix)
.RegisterSlashCommandHandler<ChannelBanCommand>("/channel-ban" + suffix)
.RegisterSlashCommandHandler<RemoveBanCommand>("/remove-channel-ban" + suffix)
.RegisterSlashCommandHandler<SimpsonsCommand>("/simpsons" + suffix)
.RegisterSlashCommandHandler<FuturamaCommand>("/futurama" + suffix)
.RegisterSlashCommandHandler<RickAndMortyCommand>("/rickandmorty" + suffix)
.RegisterSlashCommandHandler<FormalizerCommand>("/formalizer" + suffix)
.RegisterViewSubmissionHandler<ChannelBanModalHandler>(ChannelBanModalHandler.ModalCallbackId)
.RegisterViewSubmissionHandler<RemoveBanModalHandler>(RemoveBanModalHandler.ModalCallbackId)
.RegisterEventHandler<MemberJoinedChannel, MemberJoinedChannelHandler>();
// "No!" says the man in Github, "you should port the code"
// I choose the lazy solution, I choose... this.
Directory.EnumerateFiles("qrmbot/lib")
.Where(f => !f.EndsWith(".csv") && !f.EndsWith("pm"))
.Select(f => f.Split("/").Last().Replace(".pl", string.Empty))
.ToList()
.ForEach(f => c.RegisterSlashCommandHandler<QrmBotCommand>($"/{f}"));
})
.Configure<RollbarOptions>(options => configuration.GetSection("Rollbar").Bind(options))
.Configure<DiscordOptions>(o => configuration.GetSection("Discord").Bind(o))
.Configure<SlackOptions>(o => configuration.GetSection("Slack").Bind(o))
.Configure<ForwardedHeadersOptions>(options =>
{
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.ForwardLimit = 3; // CDN + Load balancer
})
.AddRollbarWeb()
.AddHangfire(config => config.UseRedisStorage(configuration.GetConnectionString("Redis")))
.AddHangfireServer()
.AddAuthorization()
.AddAuthentication()
.Services
.AddReCaptcha(configuration.GetSection("ReCaptcha"))
.AddControllers()
.Services
.AddHostedService<SlackBotHostedService>()
.AddHostedService<HangfireHostedService>();
var app = builder.Build();
{
// Database Migrations
using var scope = app.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<DevanewbotContext>();
db.Database.Migrate();
}
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
loggerFactory.AddRollbarDotNetLogger(app.Services);
app
.UseSlackNet(c =>
c.UseSocketMode(true)
)
.UseForwardedHeaders()
.UseHsts()
.UseHttpsRedirection()
.UseStaticFiles(new StaticFileOptions
{
HttpsCompression = Microsoft.AspNetCore.Http.Features.HttpsCompressionMode.DoNotCompress,
OnPrepareResponse = (context) =>
{
if (context.File.Name.EndsWith(".json") ||
context.File.Name.EndsWith(".html"))
{
context.Context.Response.GetTypedHeaders().CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromDays(0)
};
}
}
})
.UseRollbarExceptionHandler()
.UseAuthentication()
.UseAuthorization()
.UseRouting()
.UseHangfireDashboard("/hangfire", new DashboardOptions
{
Authorization = new[] { new HangfireAuthorizationFilter() },
DisplayStorageConnectionString = false
})
.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
// This is literally the worst thing I've done with this project, even worse than just running perl inline
// YES THIS WILL FUCK WITH YOUR HOME DIRECTORY BLAME QRMBOT
// That or just don't fill out QrmBot:Settings
foreach (var setting in configuration.GetSection("QrmBot:Settings").GetChildren())
{
var configFile = $".{setting.Key.ToLower()}";
var content = setting.GetChildren()
.Select(c => $"${c.Key} = \"{c.Value}\";")
.Aggregate((c1, c2) => $"{c1}\n{c2}");
var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
await File.WriteAllTextAsync($"{homeDir}/{configFile}", content);
}
await app.RunAsync();