-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilterBot.cs
399 lines (341 loc) · 15.4 KB
/
FilterBot.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
namespace PokeFilterBot
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using PokeFilterBot.Commands;
using PokeFilterBot.Configuration;
using PokeFilterBot.Data;
using PokeFilterBot.Extensions;
using PokeFilterBot.Utilities;
public class FilterBot
{
#region Variables
private DiscordClient _client;
private readonly Database _db;
private readonly Config _config;
private readonly Random _rand;
private readonly string[] _wakeupMessages =
{
"Whoa, whoa...alright I'm awake.",
"No need to push, I'm going...",
"That was a weird dream, wait a minute...",
//"Circuit overload, malfunktshun."
"Circuits fully charged, let's do this!",
"What is this place? How did I get here?",
"Looks like we're not in Kansas anymore...",
"Hey...watch where you put those mittens!"
};
#endregion
#region Properties
public Dictionary<string, ICustomCommand> Commands { get; private set; }
#endregion
#region Constructor
public FilterBot()
{
_db = Database.Load();
_config = Config.Load();
_rand = new Random();
}
#endregion
#region Public Methods
public async Task Start()
{
if (_client != null)
{
Console.WriteLine($"{AssemblyUtils.AssemblyName} already started, no need to start again.");
return;
}
_client = new DiscordClient(new DiscordConfiguration
{
AutoReconnect = true,
//DiscordBranch = Branch.Stable,
LogLevel = LogLevel.Debug,
Token = _config.AuthToken,
TokenType = TokenType.Bot
});
_client.MessageCreated += Client_MessageCreated;
_client.Ready += Client_Ready;
_client.DmChannelCreated += Client_DmChannelCreated;
_client.GuildMemberAdded += Client_GuildMemberAdded;
_client.GuildMemberRemoved += Client_GuildMemberRemoved;
_client.GuildBanAdded += Client_GuildBanAdded;
_client.GuildBanRemoved += Client_GuildBanRemoved;
if (Commands == null)
{
Commands = new Dictionary<string, ICustomCommand>()
{
{ "demo", new DemoCommand() },
{ "help", new HelpCommand() },
{ "info", new InfoCommand(_db) },
{ "version", new VersionCommand() },
{ "setup", new SetupCommand(_client, _db) },
{ "remove", new RemoveCommand(_client, _db) },
{ "sub", new SubscribeCommand(_db) },
{ "unsub", new UnsubscribeCommand(_db) },
{ "enable", new EnableDisableCommand(_db, true) },
{ "disable", new EnableDisableCommand(_db, false) },
{ "iam", new IamCommand(_client, _config) },
{ "create_roles", new CreateRolesCommand(_client) },
{ "delete_roles", new DeleteRolesCommand(_client) },
{ "lobby", new CreateRaidLobbyCommand(_client, _db) },
{ "checkin", new RaidLobbyCheckInCommand(_client, _db) },
{ "ontheway", new RaidLobbyOnTheWayCommand(_client, _db) },
{ "restart", new RestartCommand() },
{ "shutdown", new ShutdownCommand() }
};
}
Console.WriteLine("Connecting to discord server...");
await _client.ConnectAsync();
await Task.Delay(-1);
}
public async Task Stop()
{
if (_client == null)
{
Console.WriteLine($"{AssemblyUtils.AssemblyName} has not been started, therefore it cannot be stopped.");
return;
}
Console.WriteLine($"Shutting down {AssemblyUtils.AssemblyName}...");
await _client.DisconnectAsync();
_client.Dispose();
_client = null;
}
#endregion
#region Discord Events
private async Task Client_Ready(ReadyEventArgs e)
{
await DisplaySettings();
var randomWelcomeMessage = _wakeupMessages[_rand.Next(0, _wakeupMessages.Length - 1)];
await SendMessage(_config.WebHookUrl, randomWelcomeMessage);
foreach (var user in _client.Presences)
{
Console.WriteLine($"User: {user.Key}: {user.Value.User.Username}");
}
}
private async Task Client_MessageCreated(MessageCreateEventArgs e)
{
//Console.WriteLine($"Message recieved from server {e.Guild.Name} #{e.Message.Channel.Name}: {e.Message.Author.Username} (IsBot: {e.Message.Author.IsBot}) {e.Message.Content}");
if (e.Message.Author.Id == _client.CurrentUser.Id) await Task.CompletedTask;
if (e.Message.Author.IsBot)
{
await CheckSponsorRaids(e.Message);
await CheckSubscriptions(e.Message);
}
else if (e.Message.Channel.Name == _config.CommandsChannel)
{
await ParseCommand(e.Message);
}
}
private async Task Client_DmChannelCreated(DmChannelCreateEventArgs e)
{
var msg = await e.Channel.GetMessageAsync(e.Channel.LastMessageId);
if (msg != null)
{
await ParseCommand(msg);
}
}
private async Task Client_GuildBanAdded(GuildBanAddEventArgs e)
{
var channel = _client.GetChannelByName(_config.CommandsChannel);
await channel.SendMessageAsync($"OH SNAP! The ban hammer was just dropped on {e.Member.Mention}, cya!");
}
private async Task Client_GuildBanRemoved(GuildBanRemoveEventArgs e)
{
var channel = _client.GetChannelByName(_config.CommandsChannel);
await channel.SendMessageAsync($"Zeus was feeling nice today and unbanned {e.Member.Mention}, welcome back! Hopefully you'll learn to behave this time around.");
}
private async Task Client_GuildMemberAdded(GuildMemberAddEventArgs e)
{
var channel = _client.GetChannelByName(_config.CommandsChannel);
await channel.SendMessageAsync($"Everyone let's welcome {e.Member.Mention} to the server! We've been waiting for you!");
}
private async Task Client_GuildMemberRemoved(GuildMemberRemoveEventArgs e)
{
var channel = _client.GetChannelByName(_config.CommandsChannel);
await channel.SendMessageAsync($"Sorry to see you go {e.Member.Mention}, hope to see you back soon!");
}
#endregion
#region Private Methods
public async Task ParseCommand(DiscordMessage message)
{
var command = new Command(message.Content);
if (!command.ValidCommand && !message.Author.IsBot) return;
if (Commands.ContainsKey(command.Name))
{
var isOwner = message.Author.Id == _config.OwnerId;
if ((Commands[command.Name].AdminCommand && isOwner) || !Commands[command.Name].AdminCommand)
{
await Commands[command.Name].Execute(message, command);
}
//else
//{
// //TODO: You are not the owner so your commands are not recognized.
//}
}
//switch (command.Name)
//{
// case "lobby":
// if (command.HasArgs && command.Args.Count == 2)
// {
// switch (command.Args[0])
// {
// case "checkin":
// await new RaidLobbyCheckInCommand(_db).Execute(message, command);
// break;
// case "join":
// //await new RaidLobbyCheckInCommand(_db).Execute(message, command);
// break;
// case "ontheway":
// await new OnTheWayRaidLobbyCommand(_client, _db).Execute(message, command);
// break;
// default:
// break;
// }
// //if (command.Args[0] == "checkin")
// //{
// // var lobbyName = command.Args[1];
// // var eta = command.Args[2];
// // return;
// //}
// //var raidMessageId = Convert.ToUInt64(command.Args[1]);
// }
// break;
//}
_db.Save();
}
private async Task CheckSponsorRaids(DiscordMessage message)
{
foreach (DiscordEmbed embed in message.Embeds)
{
if (embed.Description.Contains("Starbucks") ||
embed.Description.Contains("Sprint"))
{
switch (message.Channel.Id)
{
case 375047782827950092: //Legendary_Raids
case 366049816188420096: //Upland_Raids
case 374809552928899082: //Upland_Legendary_Raids
case 366359725857832973: //Ontario_Raids
case 374817863690747905: //Ontario_Legendary_Raids
case 366049617642520596: //Pomona_Raids
case 374817900273336321: //Pomona_Legendary_Raids
case 366049983725830145: //EastLA_Raids
case 374819488174178304: //EastLA_Legendary_Raids
var webHook = "https://discordapp.com/api/webhooks/374830905547816960/qjSyb2EPRSdmKXOK2N_82nna8fZGAWHmUoLjBrxI5518Ua2OOcOGRpzKCltZqOA45wOh";
await SendMessage(webHook, string.Empty, embed);
break;
}
}
}
}
private async Task CheckSubscriptions(DiscordMessage message)
{
DiscordUser discordUser;
foreach (var user in _db.Subscriptions)
{
if (!user.Enabled) continue;
discordUser = await _client.GetUserAsync(user.UserId);
if (discordUser == null) continue;
if (!user.Channels.Contains(message.Channel.Name)) continue;
foreach (var pokeId in user.PokemonIds)
{
var pokemon = _db.Pokemon.Find(x => x.Index == pokeId);
if (pokemon == null) continue;
if (string.Compare(message.Author.Username, pokemon.Name, true) == 0)
{
var msg = $"A wild {pokemon.Name} has appeared!\r\n\r\n" + message.Content;
Console.WriteLine($"Notifying user {discordUser.Username} that a {pokemon.Name} has appeared...");
Notify(discordUser, msg, pokemon, message.Embeds[0]);
await DirectMessage(discordUser, msg, message.Embeds.Count == 0 ? null : message.Embeds[0]);
}
}
}
}
private async Task DirectMessage(DiscordUser user, string message, DiscordEmbed embed)
{
var dm = await _client.CreateDmAsync(user);
if (dm != null)
{
await dm.SendMessageAsync(message, false, embed);
}
}
private async Task SendMessage(string webHookUrl, string message, DiscordEmbed embed = null)
{
var data = Utils.GetWebHookData(webHookUrl);
if (data == null) return;
var guildId = Convert.ToUInt64(Convert.ToString(data["guild_id"]));
var channelId = Convert.ToUInt64(Convert.ToString(data["channel_id"]));
DiscordGuild guild = await _client.GetGuildAsync(guildId);
if (guild == null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error: Guild does not exist!");
Console.ResetColor();
return;
}
//DiscordChannel channel = guild.GetChannel(channelId);
DiscordChannel channel = await _client.GetChannelAsync(channelId);
if (channel == null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error: Channel does not exist!");
Console.ResetColor();
return;
}
await channel.SendMessageAsync(message, false, embed);
}
private async Task DisplaySettings()
{
var owner = await _client.GetUserAsync(_config.OwnerId);
Console.WriteLine($"Owner: {owner?.Username} ({_config.OwnerId})");
Console.WriteLine($"Authentication Token: {_config.AuthToken}");
Console.WriteLine($"Commands Channel: {_config.CommandsChannel}");
Console.WriteLine($"Startup WebHook: {_config.WebHookUrl}");
Console.WriteLine();
Console.WriteLine("Current Subscriptions:");
Console.WriteLine();
foreach (var sub in _db.Subscriptions)
{
var user = await _client.GetUserAsync(sub.UserId);
if (user != null)
{
Console.WriteLine($"Enabled: {(sub.Enabled ? "Yes" : "No")}");
Console.WriteLine($"Username: {user.Username}");
Console.WriteLine($"Pokemon Notifications:");
foreach (var pokeId in sub.PokemonIds)
{
Console.WriteLine(_db.Pokemon.Find(x => x.Index == pokeId).Name + $" ({pokeId})");
}
Console.WriteLine($"Channel Subscriptions:");
foreach (var channel in sub.Channels)
{
Console.WriteLine("#" + channel);
}
Console.WriteLine();
Console.WriteLine();
}
}
}
private void Notify(DiscordUser user, string message, Pokemon pokemon, DiscordEmbed embed)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine("***********************************");
Console.WriteLine($"********** {pokemon.Name} FOUND **********");
Console.WriteLine("***********************************");
Console.WriteLine(DateTime.Now.ToString());
//Console.WriteLine("Title: \t\t{0}", embed.Title); //DIRECTIONS
Console.WriteLine(embed.Description); //CP, IV, etc...
Console.WriteLine(embed.Url); //GMaps link
Console.WriteLine("***********************************");
Console.WriteLine();
Console.ResetColor();
Console.WriteLine($"Alerting discord user {user.Username} of {message}");
}
#endregion
}
}