forked from RPCS3/discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInviteWhitelistProvider.cs
99 lines (88 loc) · 3.66 KB
/
InviteWhitelistProvider.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
using System;
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using Microsoft.EntityFrameworkCore;
namespace CompatBot.Database.Providers;
internal static class InviteWhitelistProvider
{
public static bool IsWhitelisted(ulong guildId)
{
using var db = new BotDb();
return db.WhitelistedInvites.Any(i => i.GuildId == guildId);
}
public static async Task<bool> IsWhitelistedAsync(DiscordInvite invite)
{
var code = string.IsNullOrWhiteSpace(invite.Code) ? null : invite.Code;
var name = string.IsNullOrWhiteSpace(invite.Guild.Name) ? null : invite.Guild.Name;
await using var db = new BotDb();
var whitelistedInvite = await db.WhitelistedInvites.FirstOrDefaultAsync(i => i.GuildId == invite.Guild.Id);
if (whitelistedInvite == null)
return false;
if (name != null && name != whitelistedInvite.Name)
whitelistedInvite.Name = invite.Guild.Name;
if (string.IsNullOrEmpty(whitelistedInvite.InviteCode) && code != null)
whitelistedInvite.InviteCode = code;
await db.SaveChangesAsync().ConfigureAwait(false);
return true;
}
public static async Task<bool> AddAsync(DiscordInvite invite)
{
if (await IsWhitelistedAsync(invite).ConfigureAwait(false))
return false;
var code = invite.IsRevoked || string.IsNullOrWhiteSpace(invite.Code) ? null : invite.Code;
var name = string.IsNullOrWhiteSpace(invite.Guild.Name) ? null : invite.Guild.Name;
await using var db = new BotDb();
await db.WhitelistedInvites.AddAsync(new WhitelistedInvite { GuildId = invite.Guild.Id, Name = name, InviteCode = code }).ConfigureAwait(false);
await db.SaveChangesAsync().ConfigureAwait(false);
return true;
}
public static async Task<bool> AddAsync(ulong guildId)
{
if (IsWhitelisted(guildId))
return false;
await using var db = new BotDb();
await db.WhitelistedInvites.AddAsync(new WhitelistedInvite {GuildId = guildId}).ConfigureAwait(false);
await db.SaveChangesAsync().ConfigureAwait(false);
return true;
}
public static async Task<bool> RemoveAsync(int id)
{
await using var db = new BotDb();
var dbItem = await db.WhitelistedInvites.FirstOrDefaultAsync(i => i.Id == id).ConfigureAwait(false);
if (dbItem == null)
return false;
db.WhitelistedInvites.Remove(dbItem);
await db.SaveChangesAsync().ConfigureAwait(false);
return true;
}
public static async Task CleanupAsync(DiscordClient client)
{
while (!Config.Cts.IsCancellationRequested)
{
await using var db = new BotDb();
foreach (var invite in db.WhitelistedInvites.Where(i => i.InviteCode != null))
{
try
{
var result = await client.GetInviteByCodeAsync(invite.InviteCode).ConfigureAwait(false);
if (result?.IsRevoked == true)
invite.InviteCode = null;
}
catch (NotFoundException)
{
invite.InviteCode = null;
Config.Log.Info($"Removed invite code {invite.InviteCode} for server {invite.Name}");
}
catch (Exception e)
{
Config.Log.Debug(e);
}
}
await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);
await Task.Delay(TimeSpan.FromHours(1), Config.Cts.Token).ConfigureAwait(false);
}
}
}