-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProgram.cs
171 lines (152 loc) · 6.23 KB
/
Program.cs
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
using Discord;
using Discord.Commands;
using Discord.Interactions;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Resources;
using System.Threading.Tasks;
using tModloaderDiscordBot.Services;
namespace tModloaderDiscordBot
{
public class Program
{
public static bool Ready;
public static void Main(string[] args)
=> new Program().StartAsync().GetAwaiter().GetResult();
internal static IUser BotOwner;
private CommandService _commandService;
private DiscordSocketClient _client;
private IServiceProvider _services;
private LoggingService _loggingService;
private InteractionService _interactionService;
//private ReactionRoleService _reactionRoleService;
private async Task StartAsync()
{
IServiceProvider BuildServiceProvider()
{
return new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commandService)
.AddSingleton<UserHandlerService>()
.AddSingleton<CommandHandlerService>()
.AddSingleton<HastebinService>()
.AddSingleton<AutoPinService>()
.AddSingleton<RecruitmentChannelService>()
.AddSingleton<BanAppealChannelService>()
.AddSingleton<SupportChannelAutoMessageService>()
.AddSingleton<CrosspostService>()
//.AddSingleton<ReactionRoleService>()
// How to use resources:
//_services.GetRequiredService<ResourceManager>().GetString("key")
.AddSingleton(new ResourceManager("tModloaderDiscordBot.Properties.Resources", GetType().Assembly))
.AddSingleton<LoggingService>()
.AddSingleton<GuildConfigService>()
.AddSingleton<SiteStatusService>()
.AddSingleton<GuildTagService>()
.AddSingleton<PermissionService>()
.AddSingleton<LegacyModService>()
.AddSingleton<ModService>()
.AddSingleton<AuthorService>()
.BuildServiceProvider();
}
_client = new DiscordSocketClient(new DiscordSocketConfig
{
GatewayIntents = (GatewayIntents.AllUnprivileged | GatewayIntents.GuildMembers) & ~(GatewayIntents.GuildScheduledEvents | GatewayIntents.GuildInvites),
AlwaysDownloadUsers = true,
LogLevel = LogSeverity.Verbose,
MessageCacheSize = 100
});
_commandService = new CommandService(new CommandServiceConfig
{
DefaultRunMode = Discord.Commands.RunMode.Async,
CaseSensitiveCommands = false,
#if TESTBOT
LogLevel = LogSeverity.Critical,
ThrowOnError = true,
#else
LogLevel = LogSeverity.Debug,
ThrowOnError = false
#endif
});
_services = BuildServiceProvider();
await _services.GetRequiredService<CommandHandlerService>().InitializeAsync();
_services.GetRequiredService<HastebinService>();
_services.GetRequiredService<LoggingService>().Initialize();
_services.GetRequiredService<AutoPinService>();
_client.Ready += ClientReady;
_client.GuildAvailable += ClientGuildAvailable;
_client.LatencyUpdated += ClientLatencyUpdated;
// Begin our connection once everything is hooked up and ready to go
// Because this is async, this returns immediately (connection is handled on a separate thread by the con manager)
await _client.StartAsync().ContinueWith(async _ =>
{
#if TESTBOT
await _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("TestBotToken"), validateToken: true);
#else
await _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("TmlBotToken"), validateToken: true);
#endif
});
Console.Title = $@"tModLoader Bot - {DateTime.Now}";
await Console.Out.WriteLineAsync($"https://discordapp.com/api/oauth2/authorize?client_id=&scope=bot");
await Console.Out.WriteLineAsync($"Start date: {DateTime.Now}");
await Task.Delay(-1);
}
private async Task ClientLatencyUpdated(int i, int j)
{
UserStatus newUserStatus = UserStatus.Online;
switch (_client.ConnectionState)
{
case ConnectionState.Disconnected:
newUserStatus = UserStatus.DoNotDisturb;
break;
case ConnectionState.Connecting:
newUserStatus = UserStatus.Idle;
break;
}
await _client.SetStatusAsync(newUserStatus);
}
private async Task ClientReady()
{
Ready = false;
await _client.SetGameAsync("Bot is starting...");
await _client.SetStatusAsync(UserStatus.DoNotDisturb);
BotOwner = (await _client.GetApplicationInfoAsync()).Owner;
await _services.GetRequiredService<GuildConfigService>().SetupAsync();
await _services.GetRequiredService<SiteStatusService>().UpdateAsync();
await _services.GetRequiredService<ModService>().Initialize().Maintain();
await _services.GetRequiredService<LegacyModService>().Initialize().Maintain();
//await _reactionRoleService.Maintain(_client);
await _services.GetRequiredService<LoggingService>().Log(new LogMessage(LogSeverity.Info, "ClientReady", "Done."));
// await _client.SetGameAsync("tModLoader " + LegacyModService.tMLVersion); TODO: Report the latest stable automatically? Would need to retrieve it each launch since it changes frequently.
await _client.SetGameAsync("tModLoader");
await ClientLatencyUpdated(_client.Latency, _client.Latency);
#if !TESTBOT
var botChannel = (ISocketMessageChannel)await _client.GetChannelAsync(242228770855976960);
await botChannel.SendMessageAsync("Bot has started successfully.");
#endif
_interactionService = new InteractionService(_client);
await _interactionService.AddModulesAsync(System.Reflection.Assembly.GetEntryAssembly(), _services);
#if TESTBOT
await _interactionService.RegisterCommandsToGuildAsync(276235094622994433); // replace this is testing on your own server.
#else
await _interactionService.RegisterCommandsToGuildAsync(103110554649894912);
// await interactionService.RegisterCommandsGloballyAsync();
#endif
_client.InteractionCreated += async interaction =>
{
var ctx = new SocketInteractionContext(_client, interaction);
await _interactionService.ExecuteCommandAsync(ctx, _services);
};
Ready = true;
}
private async Task ClientGuildAvailable(SocketGuild arg)
{
await _services.GetRequiredService<RecruitmentChannelService>().SetupAsync();
await _services.GetRequiredService<BanAppealChannelService>().Setup();
await _services.GetRequiredService<SupportChannelAutoMessageService>().Setup();
await _services.GetRequiredService<CrosspostService>().Setup();
return;
}
}
}