This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Listener.cs
340 lines (321 loc) · 15.3 KB
/
Listener.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
using System;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Discord;
using Discord.Rest;
using Discord.WebSocket;
using TjulfarBot.Net.Economy;
using TjulfarBot.Net.Leveling;
using TjulfarBot.Net.Managers;
using TjulfarBot.Net.Tempban;
using TjulfarBot.Net.Utils;
using TjulfarBot.Net.Warn;
using TjulfarBot.Net.Youtube;
using Color = Discord.Color;
using Image = System.Drawing.Image;
using ImageFormat = System.Drawing.Imaging.ImageFormat;
namespace TjulfarBot.Net
{
public class Listener
{
private static DiscordSocketClient SocketClient;
public static CommandManager CommandManager = new CommandManager();
public static void Set(DiscordSocketClient socketClient)
{
SocketClient = socketClient;
SocketClient.Connected += SocketClientOnConnected;
SocketClient.Log += SocketClientOnLog;
SocketClient.Ready += SocketClientOnReady;
SocketClient.MessageReceived += SocketClientOnMessageReceived;
SocketClient.UserJoined += SocketClientOnUserJoined;
SocketClient.UserLeft += SocketClientOnUserLeft;
SocketClient.UserBanned += SocketClientOnUserBanned;
SocketClient.UserUnbanned += SocketClientOnUserUnbanned;
SocketClient.UserVoiceStateUpdated += SocketClientOnUserVoiceStateUpdated;
}
private static Task SocketClientOnUserVoiceStateUpdated(SocketUser user, SocketVoiceState before, SocketVoiceState after)
{
if (before.VoiceChannel == null && after.VoiceChannel != null)
{
if (user.IsBot) return Task.CompletedTask;
if(LevelManager.Get().ExistsBlacklist(user.Id)) return Task.CompletedTask;
if(after.VoiceChannel.Id == after.VoiceChannel.Guild.AFKChannel.Id) return Task.CompletedTask;
VoiceCounter.Get().VoiceCounters.Add(user.Id, after);
}
else if (before.VoiceChannel != null && after.VoiceChannel != null)
{
if(user.IsBot) return Task.CompletedTask;
if(!VoiceCounter.Get().VoiceCounters.ContainsKey(user.Id)) return Task.CompletedTask;
if (after.VoiceChannel.Id == after.VoiceChannel.Guild.AFKChannel.Id)
{
VoiceCounter.Get().VoiceCounters.Remove(user.Id);
return Task.CompletedTask;
}
VoiceCounter.Get().VoiceCounters[user.Id] = after;
}
else if (before.VoiceChannel != null && after.VoiceChannel == null)
{
if(user.IsBot) return Task.CompletedTask;
if(!VoiceCounter.Get().VoiceCounters.ContainsKey(user.Id)) return Task.CompletedTask;
VoiceCounter.Get().VoiceCounters.Remove(user.Id);
}
return Task.CompletedTask;
}
public static void OnWarned(WarnManager.Warn warn)
{
if(Program.instance.Settings.serverLogChannel == -1) return;
var channel = warn.mod.Guild.GetTextChannel((ulong) Program.instance.Settings.serverLogChannel);
var builder = new EmbedBuilder();
builder.WithTitle($"{warn.user} wurde gewarnt").WithColor(new Color(255, 255, 0)).WithThumbnailUrl(warn.user.GetAvatarUrl());
string reason;
if (warn.reason == null) reason = "Nicht angegeben";
else reason = warn.reason;
builder.WithFields(new[]
{
new EmbedFieldBuilder().WithName("Verwarner:").WithValue(warn.mod).WithIsInline(false),
new EmbedFieldBuilder().WithName("Grund:").WithValue(reason).WithIsInline(false)
});
channel.SendMessageAsync(null, false, builder.Build()).GetAwaiter().GetResult();
}
private static Task SocketClientOnUserUnbanned(SocketUser arg1, SocketGuild arg2)
{
if(Program.instance.Settings.serverLogChannel == -1) return Task.CompletedTask;
var channel = arg2.GetTextChannel((ulong) Program.instance.Settings.serverLogChannel);
Task.Delay(250).GetAwaiter().GetResult();
var logs = arg2.GetAuditLogsAsync(10).FlattenAsync().GetAwaiter().GetResult();
IUser mod = null;
foreach (var log in logs)
{
if(log.Action != ActionType.Unban) continue;
mod = log.User;
break;
}
var builder = new EmbedBuilder();
builder.WithTitle("Mitglied wurde entbannt !").WithColor(Color.Red).WithThumbnailUrl(arg1.GetAvatarUrl());
builder.WithFields(new[]
{
new EmbedFieldBuilder().WithName("Mitglied:").WithValue(arg1.ToString()).WithIsInline(true),
new EmbedFieldBuilder().WithName("Mod:").WithValue(mod.Mention).WithIsInline(true)
});
builder.WithFooter($"User-ID: {arg1.Id}");
channel.SendMessageAsync(null, false, builder.Build()).GetAwaiter().GetResult();
return Task.CompletedTask;
}
private static Task SocketClientOnUserBanned(SocketUser arg1, SocketGuild arg2)
{
if(Program.instance.Settings.serverLogChannel == -1) return Task.CompletedTask;
var channel = arg2.GetTextChannel((ulong) Program.instance.Settings.serverLogChannel);
Task.Delay(250).GetAwaiter().GetResult();
var logs = arg2.GetAuditLogsAsync(10).FlattenAsync().GetAwaiter().GetResult();
IUser mod = null;
string reason = null;
foreach (var log in logs)
{
if(log.Action != ActionType.Ban) continue;
if((log.Data as BanAuditLogData).Target.Id != arg1.Id) continue;
mod = log.User;
reason = log.Reason;
break;
}
var builder = new EmbedBuilder();
builder.WithTitle("Mitglied wurde gebannt !").WithColor(Color.Red).WithThumbnailUrl(arg1.GetAvatarUrl());
if (reason == null)
{
builder.WithFields(new[]
{
new EmbedFieldBuilder().WithName("Mitglied:").WithValue(arg1.ToString()).WithIsInline(true),
new EmbedFieldBuilder().WithName("Mod:").WithValue(mod.Mention).WithIsInline(true),
});
}
else
{
builder.WithFields(new[]
{
new EmbedFieldBuilder().WithName("Mitglied:").WithValue(arg1.ToString()).WithIsInline(true),
new EmbedFieldBuilder().WithName("Mod:").WithValue(mod.Mention).WithIsInline(false),
new EmbedFieldBuilder().WithName("Grund:").WithValue(reason).WithIsInline(false)
});
}
builder.WithFooter($"User-ID: {arg1.Id}");
LevelManager.Get().DeleteProfile(arg1.Id);
channel.SendMessageAsync(null, false, builder.Build()).GetAwaiter().GetResult();
return Task.CompletedTask;
}
private static Task SocketClientOnUserLeft(SocketGuildUser arg)
{
LevelManager.Get().DeleteProfile(arg.Id);
Task.Delay(250).GetAwaiter().GetResult();
var logs = arg.Guild.GetAuditLogsAsync(10).FlattenAsync().GetAwaiter().GetResult();
var kicked = false;
IUser mod = null;
string reason = null;
foreach(var log in logs)
{
if(log.Action != ActionType.Kick) continue;
if((log.Data as KickAuditLogData).Target.Id != arg.Id) continue;
kicked = true;
mod = log.User;
reason = log.Reason;
break;
}
var builder = new EmbedBuilder();
if (kicked && Program.instance.Settings.serverLogChannel != -1)
{
builder.WithTitle("Mitglied gekickt !").WithColor(new Color(255, 255, 0))
.WithThumbnailUrl(arg.GetAvatarUrl());
EmbedFieldBuilder[] fields;
if (reason == null)
{
fields = new[]
{
new EmbedFieldBuilder().WithName("Mitglied: ").WithValue(arg.ToString()).WithIsInline(true),
new EmbedFieldBuilder().WithName("Mod: ").WithValue(mod.Mention).WithIsInline(true)
};
}
else
{
fields = new[]
{
new EmbedFieldBuilder().WithName("Mitglied: ").WithValue(arg.ToString()).WithIsInline(true),
new EmbedFieldBuilder().WithName("Mod: ").WithValue(mod.Mention).WithIsInline(true),
new EmbedFieldBuilder().WithName("Grund: ").WithValue(reason).WithIsInline(false)
};
}
builder.WithFields(fields);
builder.WithFooter($"User-ID: {arg.Id}");
arg.Guild.GetTextChannel((ulong) Program.instance.Settings.serverLogChannel).SendMessageAsync(null, false, builder.Build()).GetAwaiter().GetResult();
}
if (Program.instance.Settings.welcomeChannel != -1)
{
builder.WithTitle($"{arg} hat den Server verlassen !").WithColor(Color.DarkerGrey)
.WithThumbnailUrl(arg.GetAvatarUrl());
builder.WithDescription($"Wir wünschen {arg} dennoch weiterhin viel Spaß !");
arg.Guild.GetTextChannel((ulong) Program.instance.Settings.welcomeChannel).SendMessageAsync(null, false, builder.Build()).GetAwaiter().GetResult();
}
if(EcoManager.Get().HasBankAccount(arg.Id) || EcoManager.Get().HasPocketMoney(arg.Id)) EcoManager.Get().RemoveAccounts(arg.Id);
return Task.CompletedTask;
}
private static Font calibril;
private static Task SocketClientOnUserJoined(SocketGuildUser arg)
{
foreach (var autorole in AutoroleManager.Get().GetAutoroles())
{
arg.AddRoleAsync(autorole).GetAwaiter().GetResult();
}
if(Program.instance.Settings.welcomeChannel == -1) return Task.CompletedTask;
var channel = arg.Guild.GetTextChannel((ulong) Program.instance.Settings.welcomeChannel);
var webclient = new WebClient();
webclient.DownloadDataCompleted += (sender, args) =>
{
var downloadedBytes = args.Result;
var stream = new MemoryStream();
stream.Write(downloadedBytes, 0, downloadedBytes.Length);
using var pb = Image.FromStream(stream);
stream.Close();
using var welcome = Image.FromFile("TjulfarWelcome.png");
using var graphics = Graphics.FromImage(welcome);
graphics.DrawString(arg.ToString(), calibril, new SolidBrush(System.Drawing.Color.White), new PointF(280, 70));
graphics.DrawImage(pb, new Point(280, 110));
graphics.Save();
welcome.Save("welcome.png", ImageFormat.Png);
channel.SendFileAsync("welcome.png", arg.Mention).GetAwaiter().GetResult();
webclient.Dispose();
File.Delete("welcome.png");
};
webclient.DownloadDataAsync(new Uri(arg.GetAvatarUrl()));
return Task.CompletedTask;
}
private static Task SocketClientOnMessageReceived(SocketMessage arg)
{
if(arg.Author.IsBot) return Task.CompletedTask;
if(!(arg.Channel is SocketGuildChannel)) return Task.CompletedTask;
string message = arg.Content;
if (message.StartsWith(Program.instance.Settings.prefix))
{
string cmd = message.Substring(1);
foreach (var command in CommandManager.Commands)
{
if (cmd.Length == command.name.Length)
{
if (cmd.Equals(command.name))
{
CommandManager.ExecuteCommand(command, SocketClient, arg, new string[0]);
}
}
else
{
string[] cmdArray = cmd.Split(" ");
if (cmdArray[0].Equals(command.name))
{
if (cmdArray.Length == 1)
{
CommandManager.ExecuteCommand(command, SocketClient, arg, new string[0]);
}
else
{
string[] args = new string[cmdArray.Length - 1];
Array.Copy(cmdArray, 1, args, 0, cmdArray.Length - 1);
CommandManager.ExecuteCommand(command, SocketClient, arg, args);
}
}
}
}
}
else
{
if (!LevelManager.Get().ExistsLevelChannel(arg.Channel.Id)) return Task.CompletedTask;
if (LevelManager.Get().ExistsProfile(arg.Author.Id))
{
LevelManager.Get().GetProfile(arg.Author.Id).AddExp(1 * Multiplier.GetMultiplier((SocketGuildUser) arg.Author).MultiplierNumber);
}
else LevelManager.Get().CreateProfile(arg.Author.Id).AddExp(1 * Multiplier.GetMultiplier((SocketGuildUser) arg.Author).MultiplierNumber);
}
return Task.CompletedTask;
}
private static bool _wasReady = false;
private static Task SocketClientOnReady()
{
if (!_wasReady)
{
Program.instance.DatabaseManager.Init();
TempbanManager.Get().Init();
YoutubeManager.Get().Init();
WarnManager.Get().Init();
LevelManager.Get().CheckForValidEntries();
VoiceCounter.Get().Start();
ConsoleListener.Get().Start();
using var privateFontCollection = new PrivateFontCollection();
var fontBytes = File.ReadAllBytes("calibril.ttf");
var handle = GCHandle.Alloc(fontBytes, GCHandleType.Pinned);
var pointer = handle.AddrOfPinnedObject();
try
{
privateFontCollection.AddMemoryFont(pointer, fontBytes.Length);
}
finally
{
handle.Free();
}
calibril = new Font(privateFontCollection.Families.First(), 15);
_wasReady = true;
}
Console.WriteLine("Ready !");
return Task.CompletedTask;
}
private static Task SocketClientOnLog(LogMessage arg)
{
Console.WriteLine(arg.ToString());
return Task.CompletedTask;
}
private static Task SocketClientOnConnected()
{
Console.WriteLine("Connected to " + SocketClient.CurrentUser);
return Task.CompletedTask;
}
}
}