-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
231 lines (205 loc) · 9.6 KB
/
Copy pathProgram.cs
File metadata and controls
231 lines (205 loc) · 9.6 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using Amazon.EC2.Model;
using Discord;
using Discord.Rest;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using TraceLd.MineStatSharp;
namespace StartBot
{
public class Program
{
static void Main(string[] args)
{
try
{
BotConfig.LoadConfig();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("A valid configuration file could not be found. Please create one at ./config.yml and enter in the appropriate values.");
//Environment.Exit(1);
}
new Program().RunBotAsync().GetAwaiter().GetResult();
}
public static DiscordSocketClient _client;
public static IServiceProvider _services;
public static async Task ModLog(string title, string description)
{
var chan = _client.GetChannel(831964996287332352) as SocketTextChannel;
var embed = new EmbedBuilder().WithTitle(title).WithDescription(description).WithColor(Color.Blue).Build();
await chan.SendMessageAsync(embed: embed);
}
public static IServiceProvider GetServices()
{
return _services;
}
public async Task RunBotAsync()
{
_client = new DiscordSocketClient();
_services = new ServiceCollection()
.AddSingleton(_client)
.BuildServiceProvider();
string botToken = BotConfig.GetCachedConfig().Discord.BotToken;
_client.Log += _client_Log;
RegisterEvents();
await _client.LoginAsync(TokenType.Bot, botToken);
await _client.StartAsync();
await Task.Delay(-1);
}
public async Task Start()
{
if (_client.GetGuild(BotConfig.GetCachedConfig().Discord.GuildId) != null)
{
var guild = _client.GetGuild(BotConfig.GetCachedConfig().Discord.GuildId);
var deferStopCommand = new SlashCommandBuilder();
var forceStartInstanceBuilder = new SlashCommandBuilder();
var forceStopInstanceBuilder = new SlashCommandBuilder();
var forceSyncBuilder = new SlashCommandBuilder();
deferStopCommand.WithName("deferstop");
deferStopCommand.WithDescription("Defers the server from stopping for five minutes.");
deferStopCommand.WithDefaultPermission(true);
forceStartInstanceBuilder.WithName("forcestartinstance");
forceStartInstanceBuilder.WithDescription("Admin only");
forceStartInstanceBuilder.WithDefaultPermission(false);
forceStopInstanceBuilder.WithName("forcestopinstance");
forceStopInstanceBuilder.WithDescription("Admin only");
forceStopInstanceBuilder.WithDefaultPermission(false);
forceSyncBuilder.WithName("forcesync");
forceSyncBuilder.WithDescription("Admin only");
forceSyncBuilder.WithDefaultPermission(false);
var deferStopCommandCmd = await Program._client.Rest.CreateGuildCommand(deferStopCommand.Build(), 795714783801245706);
var forceStartInstance = await Program._client.Rest.CreateGuildCommand(forceStartInstanceBuilder.Build(), 795714783801245706);
var forceStopInstance = await Program._client.Rest.CreateGuildCommand(forceStopInstanceBuilder.Build(), 795714783801245706);
var forcesync = await Program._client.Rest.CreateGuildCommand(forceSyncBuilder.Build(), 795714783801245706);
await forceStartInstance.ModifyCommandPermissions(new ApplicationCommandPermission[] {
new ApplicationCommandPermission(164890197237039104, ApplicationCommandPermissionTarget.User, true)
});
await forceStopInstance.ModifyCommandPermissions(new ApplicationCommandPermission[] {
new ApplicationCommandPermission(164890197237039104, ApplicationCommandPermissionTarget.User, true)
});
await forcesync.ModifyCommandPermissions(new ApplicationCommandPermission[] {
new ApplicationCommandPermission(164890197237039104, ApplicationCommandPermissionTarget.User, true)
});
if (guild.GetTextChannel(BotConfig.GetCachedConfig().Discord.MessageChannelId) != null)
{
var chan = guild.GetTextChannel(BotConfig.GetCachedConfig().Discord.MessageChannelId);
if(BotConfig.GetCachedConfig().InternalMessageId == 0 || await chan.GetMessageAsync(BotConfig.GetCachedConfig().InternalMessageId) == null)
{
var comps = new ComponentBuilder();
comps.WithButton("Refresh Server", "STARTBOT_REFRESH", emote: new Emoji("🔄"));
var msg = await chan.SendMessageAsync(embed: Embeds.ServerStopped(), components: comps.Build());
var cfg = BotConfig.GetCachedConfig();
cfg.InternalMessageId = (ulong) msg.Id;
BotConfig.SaveConfig(cfg);
}
}
else
{
Console.WriteLine("Could not find channel specified in the config file.");
}
}
else
{
Console.WriteLine("Could not find guild specified in config file.");
}
ThreadPool.QueueUserWorkItem(async delegate
{
await new WebServe().Run();
});
/*
* Stop the server in case the bot crashed.
*/
Console.WriteLine("Attempting server start.");
try
{
var stopRequest = new StopInstancesRequest();
var request = new StopInstancesRequest();
request.InstanceIds = new List<string>()
{
BotConfig.GetCachedConfig().Aws.EC2InstanceId
};
request.Force = false;
var response = await ServerStateManager.Instance().client.StopInstancesAsync(request);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static ReactEventHandler _reh = new ReactEventHandler();
private async Task HandleBotReactions(SocketMessageComponent smc)
{
await _reh.Handle(smc);
}
public void RegisterEvents()
{
_client.Ready += Start;
//_client.ReactionAdded += HandleBotReactions;
_client.InteractionCreated += async (inter) =>
{
if (inter is SocketMessageComponent smc)
{
if (smc.Data.CustomId == "STARTBOT_REFRESH")
{
await HandleBotReactions(smc);
await smc.DeferAsync();
}
}
if(inter is SocketSlashCommand cmd)
{
if(cmd.CommandName == "deferstop")
{
var status = await ServerStateManager.Instance().GetState();
if(status.InstanceState.Code == 16)
{
if(ServerStateManager.Instance().StopDeferred)
{
await cmd.RespondAsync("The stopping of the server has already been deferred.", ephemeral: false);
}
else
{
ServerStateManager.Instance().StopDeferred = true;
await cmd.RespondAsync("The stopping of the server is now deferred for an extra five minutes.", ephemeral: false);
}
}
else
{
await cmd.RespondAsync("The server is not running at the moment.", ephemeral: true);
}
}
if(cmd.CommandName == "forcestartinstance")
{
var request = new StartInstancesRequest();
request.InstanceIds = new List<string>()
{
BotConfig.GetCachedConfig().Aws.EC2InstanceId
};
var response = await ServerStateManager.Instance().client.StartInstancesAsync(request);
await cmd.RespondAsync(response.ToString(), ephemeral: true);
}
if (cmd.CommandName == "forcestopinstance")
{
var request = new StopInstancesRequest();
request.InstanceIds = new List<string>()
{
BotConfig.GetCachedConfig().Aws.EC2InstanceId
};
var response = await ServerStateManager.Instance().client.StopInstancesAsync(request);
await cmd.RespondAsync(response.ToString(), ephemeral: true);
}
}
};
}
private Task _client_Log(LogMessage l)
{
Console.WriteLine(l);
return Task.CompletedTask;
}
}
}