forked from Justiphi/Tatenashi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
355 lines (320 loc) · 16.1 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using System;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using Discord.Commands;
using Justibot.Modules.Public;
using Justibot.Services;
using Discord.Rest;
namespace Justibot
{
public class Program
{
public static void Main(string[] args) =>
new Program().Start().GetAwaiter().GetResult();
private DiscordSocketClient client;
private CommandHandler handler;
PublicModule module = new PublicModule();
DateTime startTime = DateTime.Now;
public async Task Start()
{
Configuration.configure();
//create client and add what serverity of information to log
client = new DiscordSocketClient(new DiscordSocketConfig()
{
LogLevel = LogSeverity.Info
});
//create template for logged messages
client.Log += (message) =>
{
Console.WriteLine($"{System.DateTime.Now.ToString()} {message}");
return Task.CompletedTask;
};
//when client is ready, load important information to memory for quick access and set game to help message
client.Ready += async () =>
{
await client.SetGameAsync("Use +help");
PrefixService.LoadPrefixs(client);
WelcomeService.LoadWelcomes(client);
LeavingService.Loadleaving(client);
XpService.Loadxps(client);
//start timer for xp module
XpService.timerStart();
//if version from data file has changed, reset update time (used to track time since update)
if (Justibot.Loader.checkUpdate().Value != Justibot.Data.version)
{
Justibot.Saver.addVersion(DateTime.Now, Justibot.Data.version);
}
};
//log the client into discord with the bot token
await client.LoginAsync(TokenType.Bot, Configuration.config.Token);
await client.StartAsync();
//when a user joins a guild, check appropriate permissions and react accordingly
client.UserJoined += async (user) =>
{
//check welcome module
var results = Justibot.Loader.LoadPerm(user, "WELCOME");
var guild = user.Guild as IGuild;
//if welcome module enabled send message
if (results.Item1 == true)
{
ulong result2 = results.Item2;
//get designated channel for welcome messages
SocketTextChannel welcomeChannel = user.Guild.GetChannel(result2) as SocketTextChannel;
//if welcome message is available for guild use it, otherwise use default
bool check = welcomedict.welcomes.TryGetValue(guild.Id, out string welcome);
if (!check)
{
welcome = "Welcome, **[user]** has joined **[server]!!!** \n" +
"Have a good time!!!";
}
//fill in placeholder text with appropriate information
welcome = welcome.Replace("[user]", user.Username);
welcome = welcome.Replace("[server]", guild.Name);
//if permission argument is plain send message in plain text otherwise send in embeded message
if (results.Item3 == "PLAIN")
{
await welcomeChannel.SendMessageAsync(welcome);
}
else
{
string avatar = user.GetAvatarUrl();
if (avatar == null)
{
avatar = "https://cdn.discordapp.com/embed/avatars/0.png";
}
Color color = new Color(0, 255, 0);
if (avatar.Contains("/a_"))
{
avatar = $"{avatar.Remove(avatar.Length - 12)}gif?size=128";
}
var embed = new EmbedBuilder()
.WithColor(color)
.WithTitle($"{Format.Bold($"{client.CurrentUser.Username}:")}")
.WithDescription(welcome)
.WithThumbnailUrl(avatar)
.Build();
await welcomeChannel.SendMessageAsync(user.Mention, false, embed);
}
}
//if joining role is enabled give new user the role
var joinRoles = Justibot.Loader.LoadPerm(user, "JOINROLE");
if (joinRoles.Item1 == true)
{
IRole joinerRole = user.Guild.GetRole(joinRoles.Item2) as IRole;
await user.AddRoleAsync(joinerRole);
}
//if log permission enabled send message to log channel
var results2 = Justibot.Loader.LoadPerm(user, "LOG");
if (results2.Item1 == true)
{
ulong result2 = results2.Item2;
SocketTextChannel logChannel = user.Guild.GetChannel(result2) as SocketTextChannel;
if (results2.Item3 == "PLAIN")
{
await logChannel.SendMessageAsync($"{Format.Bold($"{user.Username}")}#{user.Discriminator}, ID:<{user.Id}> has joined the server. \n" +
$"There are now {user.Guild.MemberCount} members.");
}
else
{
string avatar = user.GetAvatarUrl();
Color color = new Color(0, 255, 0);
//if no avatar available use deafult
if (avatar == null)
{
avatar = "https://cdn.discordapp.com/embed/avatars/0.png";
}
//check if avatar is gif (gif avatars contain /a_ in their URLs
if (avatar.Contains("/a_"))
{
avatar = $"{avatar.Remove(avatar.Length - 12)}gif?size=128";
}
var embed = new EmbedBuilder()
.WithColor(color)
.WithTitle($"{Format.Bold($"{client.CurrentUser.Username}:")}")
.WithDescription($"{Format.Bold($"{user.Username}")}#{user.Discriminator}, ID:<{user.Id}> has joined the server. \n" +
$"There are now {user.Guild.MemberCount} members.")
.WithThumbnailUrl(avatar)
.Build();
await logChannel.SendMessageAsync("", false, embed);
}
}
//if bunker mode is enabled on server instantly kick new user
if (Settings.bunker.Contains(guild.Id))
{
await user.Guild.DefaultChannel.SendMessageAsync($"{Format.Bold($"{user.Username}")} was kicked by bunker mode!");
await user.KickAsync("Bunker mode enabled");
}
};
//when user leaves guild check appropriate permissions and act accordingly
client.UserLeft += async (user) =>
{
//check leaving permission
var results = Justibot.Loader.LoadPerm(user, "LEAVING");
if (results.Item1 == true)
{
ulong result2 = results.Item2;
var guild = user.Guild as IGuild;
//get channel to send leaving message to
SocketTextChannel leaveChannel = user.Guild.GetChannel(result2) as SocketTextChannel;
//if leaving message available set it, else use default message
bool check = welcomedict.leaves.TryGetValue(guild.Id, out string leave);
if (!check)
{
leave = "**[mention]** has left **[server]**, goodbye.";
}
//replace placeholder text with appropriate items
leave = leave.Replace("[user]", user.Username);
leave = leave.Replace("[mention]", user.Mention);
leave = leave.Replace("[server]", guild.Name);
//if leaving permission argument is plain, send plain text otherwise send as an embed message
if (results.Item3 == "PLAIN")
{
await leaveChannel.SendMessageAsync(leave);
}
else
{
string avatar = user.GetAvatarUrl();
Color color = new Color(255, 0, 0);
//if no avatar available use deafult
if (avatar == null)
{
avatar = "https://cdn.discordapp.com/embed/avatars/0.png";
}
//check if avatar is gif (gif avatars contain /a_ in their URLs
if (avatar.Contains("/a_"))
{
avatar = $"{avatar.Remove(avatar.Length - 12)}gif?size=128";
}
var embed = new EmbedBuilder()
.WithColor(color)
.WithTitle($"{Format.Bold($"{client.CurrentUser.Username}:")}")
.WithDescription(leave)
.WithThumbnailUrl(avatar)
.Build();
await leaveChannel.SendMessageAsync("", false, embed);
}
}
//if log module is enabled, send message to log channel bassed on argument (plain text or embed)
var results2 = Justibot.Loader.LoadPerm(user, "LOG");
if (results2.Item1 == true)
{
ulong result2 = results2.Item2;
var guild = user.Guild as IGuild;
SocketTextChannel welcomeChannel = user.Guild.GetChannel(result2) as SocketTextChannel;
if (results2.Item3 == "PLAIN")
{
await welcomeChannel.SendMessageAsync($"{Format.Bold($"{user.Username}")}#{user.Discriminator}, ID:<{user.Id}> has left the server. \n" +
$"There are now {user.Guild.MemberCount} members.");
}
else
{
string avatar = user.GetAvatarUrl();
Color color = new Color(255, 0, 0);
//if no avatar available use deafult
if (avatar == null)
{
avatar = "https://cdn.discordapp.com/embed/avatars/0.png";
}
//check if avatar is gif (gif avatars contain /a_ in their URLs
if (avatar.Contains("/a_"))
{
avatar = $"{avatar.Remove(avatar.Length - 12)}gif?size=128";
}
var embed = new EmbedBuilder()
.WithColor(color)
.WithTitle($"{Format.Bold($"{client.CurrentUser.Username}:")}")
.WithDescription($"{Format.Bold($"{user.Username}")}#{user.Discriminator}, ID:<{user.Id}> has left the server. \n" +
$"There are now {user.Guild.MemberCount} members.")
.WithThumbnailUrl(avatar)
.Build();
await welcomeChannel.SendMessageAsync("", false, embed);
}
}
};
//if user messages a channel the bot can see, check permissions
client.MessageReceived += async (message) =>
{
var guild = (message.Channel as SocketTextChannel)?.Guild;
var user = (message.Author as IGuildUser);
if (guild != null)
{
//if user isnt bot, give xp to user
if (user.IsBot == false)
{
var guild2 = guild as IGuild;
XpService.AddXp(user, guild2);
}
//if message contains a discord invite link, check if use has manage guild permissions
if ((message.ToString().ToUpper()).Contains("DISCORD.GG/") && (!(user.IsBot)))
{
//if user does not have manage guild permission check if advertising module is enabled
if (!(user.GuildPermissions.Has(GuildPermission.ManageGuild)) || !(Justibot.Loader.isStaff(user)))
{
var results = Justibot.Loader.LoadPerm(user, "ADVERTISING");
if (results.Item1 == true)
{
//if module is set to delete, remove message and asks user to not advertise in the server
ulong result2 = results.Item2;
if (results.Item3 == "DELETE")
{
await message.DeleteAsync();
var response = await message.Channel.SendMessageAsync($"{user.Mention} please do not advertise in this server");
respond(response);
}
//if module is set to repost, send copy of message to advertising channel and ask user to only post advertisements to channel
else if (results.Item3 == "REPOST")
{
SocketTextChannel advertisingChannel = guild.GetChannel(result2) as SocketTextChannel;
if (message.Channel != advertisingChannel)
{
await advertisingChannel.SendMessageAsync($"{user.Mention} advertised message: \n{message.ToString()}");
await message.DeleteAsync();
var response = await message.Channel.SendMessageAsync($"{user.Mention} please keep advertising to {advertisingChannel.Mention}, your message has been shifted there.");
respond(response);
}
}
}
}
}
}
};
//initialize commands and commandhandler
handler = new CommandHandler();
await handler.InitCommands(client);
//prevent application shutdown
await Task.Delay(-1);
}
private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
//if log message contains failed message restart bot
if(msg.ToString().Contains("Failed to resume previous session"))
{
restartBot();
}
return Task.CompletedTask;
}
//removes message after 3 seconds
private async void respond(RestUserMessage response)
{
await Task.Delay(3000);
if (response.Channel.GetMessageAsync(response.Id) != null)
{
//if message is deleted prior, to 3 second time, ignore error, otherwise delete message
try
{
await response.DeleteAsync();
}
catch
{ }
}
}
public void restartBot()
{
//dispose of current client and create a new one
client.Dispose();
new Program().Start().GetAwaiter().GetResult();
}
}
}