-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
88 lines (71 loc) · 3.05 KB
/
Program.cs
File metadata and controls
88 lines (71 loc) · 3.05 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
using Discord;
using Discord.WebSocket;
using System;
using System.Threading.Tasks;
using SinkingYachts;
using System.Linq;
namespace Example
{
public static class Program
{
private static readonly DiscordSocketClient Client = new (new DiscordSocketConfig()
{
GatewayIntents = GatewayIntents.Guilds | GatewayIntents.GuildMessages
});
public static readonly StorageMode Mode = StorageMode.LocalWS;
private static readonly YachtsClient Yachts = new(Mode, "Example Bot", 3);
private static readonly string Token = Environment.GetEnvironmentVariable("BOT_TOKEN");
public static async Task Main()
{
if (string.IsNullOrEmpty(Token))
{
await Logger.Log("Bot", $"No bot token has been configured. Please set one as the \"BOT_TOKEN\" environment variable.", LogSeverity.Critical);
throw new("Empty bot token");
}
Client.Log += Logger.Log;
Client.Ready += Ready;
Client.MessageReceived += OnMessageReceived;
try
{
await Client.LoginAsync(TokenType.Bot, Token);
await Client.StartAsync();
}
catch (Exception ex)
{
await Logger.Log("Bot", $"Exception while trying to log in: {ex.GetType().Name} => {ex.Message}", LogSeverity.Critical);
throw;
}
if (Mode == StorageMode.LocalWS)
{
Yachts.DomainAdded += async (sender, domain) =>
{
await Logger.Log("Bot", $"New domain added: {domain}", LogSeverity.Info);
};
Yachts.DomainDeleted += async (sender, domain) =>
{
await Logger.Log("Bot", $"New domain deleted: {domain}", LogSeverity.Info);
};
}
await Task.Delay(-1);
}
public static async Task Ready()
{
await Logger.Log("Bot", $"Ready to protect your server from {await Yachts.GetDatabaseSize()} phishing domains", LogSeverity.Info);
Change[] changes = await Yachts.GetRecent(TimeSpan.FromDays(1));
int added = changes.Count(x => x.Type == ChangeType.Add);
int deleted = changes.Count(x => x.Type == ChangeType.Delete);
await Logger.Log("Bot", $"Domains added within the past day: {added}", LogSeverity.Info);
await Logger.Log("Bot", $"Domains deleted within the past day: {deleted}", LogSeverity.Info);
}
public static async Task OnMessageReceived(SocketMessage msg)
{
if (msg is not SocketUserMessage) return;
if (msg.Channel is not SocketTextChannel) return;
if (msg.Author.IsBot) return;
bool isPhishing = await Yachts.IsPhishing(msg.Content);
if (!isPhishing) return;
await msg.DeleteAsync();
await msg.Channel.SendMessageAsync($"{msg.Author.Mention}, phishing links are not allowed.");
}
}
}