-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
203 lines (172 loc) · 6.49 KB
/
Program.cs
File metadata and controls
203 lines (172 loc) · 6.49 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Morpheus.Database;
using Morpheus.Handlers;
using Morpheus.Jobs;
using Morpheus.Services;
using Morpheus.Utilities;
using Quartz;
// Load environment variables from .env file
Env.Load(".env");
GatewayIntents intents =
GatewayIntents.Guilds
| GatewayIntents.GuildMembers
| GatewayIntents.GuildMessages
| GatewayIntents.MessageContent
| GatewayIntents.GuildMessageReactions
| GatewayIntents.DirectMessages
| GatewayIntents.DirectMessageReactions;
// Set up configs
DiscordSocketConfig clientConfig = new()
{
MessageCacheSize = 100,
AlwaysDownloadUsers = true,
LogLevel = LogSeverity.Verbose,
GatewayIntents = intents,
#if DEBUG
UseInteractionSnowflakeDate = false,
#endif
};
CommandServiceConfig commandServiceConfig = new()
{
LogLevel = LogSeverity.Verbose,
DefaultRunMode = Discord.Commands.RunMode.Async,
};
// Set up dependency injection
IServiceCollection services = new ServiceCollection();
// Main services
services.AddSingleton(clientConfig);
services.AddSingleton<DiscordSocketClient>();
services.AddSingleton(commandServiceConfig);
services.AddSingleton<CommandService>();
// Scoped Services
services.AddScoped<GuildService>();
services.AddScoped<UsersService>();
services.AddScoped<LogsService>();
services.AddScoped<ActivityService>();
services.AddScoped<ChannelService>();
services.AddScoped<EconomyService>();
services.AddScoped<StocksService>();
services.AddScoped<BotAvatarJob>();
services.AddScoped<TemporaryBansJob>();
services.AddScoped<HoneypotRenameJob>();
services.AddScoped<StockUpdateJob>();
services.AddScoped<UbiJob>();
services.AddScoped<WealthTaxJob>();
// Add Quartz
services.AddQuartz(q =>
{
q.ScheduleJob<BotActivityJob>(trigger => trigger
.WithIdentity("every6hours", "discord")
.StartNow()
.WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromHours(6)).RepeatForever())
);
q.ScheduleJob<DeleteOldLogsJob>(trigger => trigger
.WithIdentity("deleteOldLogs", "discord")
.StartNow()
.WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromDays(1)).RepeatForever())
);
q.ScheduleJob<ActivityRolesJob>(trigger => trigger
.WithIdentity("activityRoles", "discord")
.StartAt(DateTime.UtcNow.AddMinutes(1))
.WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromDays(1)).RepeatForever())
);
q.ScheduleJob<RemindersJob>(trigger => trigger
.WithIdentity("reminders", "discord")
.StartNow()
.WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromMinutes(1)).RepeatForever())
);
q.ScheduleJob<BotAvatarJob>(trigger => trigger
.WithIdentity("botAvatarDaily", "discord")
.StartNow()
.WithCronSchedule("0 0 0 * * ?") // every day at 00:00 UTC
);
q.ScheduleJob<TemporaryBansJob>(trigger => trigger
.WithIdentity("temporaryBansDaily", "discord")
.StartNow()
.WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromDays(1)).RepeatForever())
);
q.ScheduleJob<HoneypotRenameJob>(trigger => trigger
.WithIdentity("honeypotRenameDaily", "discord")
.StartNow()
.WithCronSchedule("0 5 0 * * ?") // daily at 00:05 UTC
);
q.ScheduleJob<UbiJob>(trigger => trigger
.WithIdentity("ubiDistribution", "discord")
.StartNow()
.WithCronSchedule("0 0 0 * * ?") // every day at 00:00 UTC
);
q.ScheduleJob<WealthTaxJob>(trigger => trigger
.WithIdentity("wealthTax", "discord")
.StartNow()
.WithCronSchedule("0 30 23 * * ?") // every day at 23:30 UTC (before UBI)
);
q.ScheduleJob<StockUpdateJob>(trigger => trigger
.WithIdentity("stockUpdate", "discord")
.StartAt(DateTime.UtcNow.AddMinutes(2))
.WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromMinutes(5)).RepeatForever())
);
});
services.AddQuartzHostedService(options =>
{
options.WaitForJobsToComplete = true;
});
// Add the handlers (singletons that create a scope per event)
services.AddSingleton<MessagesHandler>();
services.AddSingleton<WelcomeHandler>();
services.AddSingleton<HoneypotHandler>();
services.AddSingleton<InteractionsHandler>();
services.AddSingleton<LogsHandler>();
services.AddSingleton<ActivityHandler>();
services.AddSingleton<FunnyResponsesHandler>();
services.AddSingleton<ReactionRolesHandler>();
// Add the database context
services.AddDbContextPool<DB>(options => options.UseNpgsql(Env.Variables["DB_CONNECTION_STRING"])
.ConfigureWarnings(c => c.Ignore(RelationalEventId.CommandExecuted)));
IHost host = Host.CreateDefaultBuilder().ConfigureServices((ctx, srv) =>
{
foreach (ServiceDescriptor service in services)
srv.Add(service);
}).Build();
// Run database migrations within a scope
using (var scope = host.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<DB>();
db.Database.Migrate();
const string balanceBackfillKey = "balance_backfill_v1";
var backfill = db.BotSettings.FirstOrDefault(s => s.Key == balanceBackfillKey);
if (backfill == null)
{
int updated = db.Database.ExecuteSqlRaw("UPDATE \"Users\" SET \"Balance\" = 1000.00 WHERE \"Balance\" = 0");
db.BotSettings.Add(new Morpheus.Database.Models.BotSetting
{
Key = balanceBackfillKey,
Value = updated.ToString(),
UpdateDate = DateTime.UtcNow
});
db.SaveChanges();
}
}
// Start the handlers
_ = host.Services.GetRequiredService<LogsHandler>();
_ = host.Services.GetRequiredService<WelcomeHandler>();
_ = host.Services.GetRequiredService<HoneypotHandler>();
_ = host.Services.GetRequiredService<InteractionsHandler>();
_ = host.Services.GetRequiredService<ActivityHandler>();
_ = host.Services.GetRequiredService<FunnyResponsesHandler>();
_ = host.Services.GetRequiredService<ReactionRolesHandler>();
// ReactionsHandler intentionally not started: approval flows use interaction buttons now.
MessagesHandler messagesHandler = host.Services.GetRequiredService<MessagesHandler>();
// Register the commands
await messagesHandler.InstallCommands();
// Start the bot
DiscordSocketClient client = host.Services.GetRequiredService<DiscordSocketClient>();
await client.LoginAsync(TokenType.Bot, Env.Variables["BOT_TOKEN"]);
await client.StartAsync();
// Keep the app running
await host.RunAsync();