-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGuildEventLogger.cs
186 lines (148 loc) · 6.22 KB
/
GuildEventLogger.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
using Newtonsoft.Json;
namespace RoboBot
{
public class GuildEventLogger
{
private static readonly string LogChannelsLocalPath =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log-channels.json");
private static readonly DiscordColor InfoColor = new DiscordColor(0, 100, 255);
private static readonly DiscordColor WarningColor = new DiscordColor(200, 200, 0);
private static readonly DiscordColor ErrorColor = new DiscordColor(175, 0, 0);
private static DiscordClient _client;
private Dictionary<ulong, DiscordChannel> _logChannels = new Dictionary<ulong, DiscordChannel>();
private GuildEventLogger() { }
public struct GuildLogInfoResult
{
public bool IsSet;
public DiscordChannel LogChannel;
}
public static readonly GuildEventLogger Instance = new GuildEventLogger();
public static void Initialize(ref DiscordClient client)
{
_client = client;
Instance.LoadChannelsFromFile();
}
public void SetLogChannel(DiscordGuild guild, DiscordChannel channel)
{
if (_logChannels.ContainsKey(guild.Id))
_logChannels.Remove(guild.Id);
_logChannels.Add(guild.Id, channel);
SaveChannelsToFile();
}
public void UnsetLogChannel(DiscordGuild guild)
{
if (!_logChannels.ContainsKey(guild.Id))
return;
_logChannels.Remove(guild.Id);
SaveChannelsToFile();
}
public GuildLogInfoResult GetGuildLogInfo(DiscordGuild guild)
{
bool isSet = _logChannels.TryGetValue(guild.Id, out DiscordChannel channel);
return new GuildLogInfoResult() { IsSet = isSet, LogChannel = channel };
}
public async Task NotifyModerator(DiscordGuild guild, string message)
{
ulong guildId = guild.Id;
if (!_logChannels.ContainsKey(guildId))
return;
try
{
IMention modRole = new RoleMention(1008209009330896946);
await _logChannels[guildId].SendMessageAsync(new DiscordMessageBuilder()
.WithAllowedMention(modRole)
.WithContent("<@&1008209009330896946>"));
Task<DiscordMessage> sendTask = _logChannels[guildId].SendMessageAsync(new DiscordEmbedBuilder()
.WithColor(DiscordColor.Red)
.WithDescription(message))
;
await sendTask;
}
catch (Exception e)
{
Console.WriteLine($"{nameof(GuildEventLogger)}.{nameof(LogInfo)}: {e.Message}");
}
}
public async Task LogInfo(DiscordGuild guild, string message)
{
ulong guildId = guild.Id;
if (!_logChannels.ContainsKey(guildId))
return;
try
{
Task<DiscordMessage> sendTask = _logChannels[guildId].SendMessageAsync(new DiscordEmbedBuilder()
.WithDescription(message)
.WithColor(InfoColor));
await sendTask;
}
catch (Exception e)
{
Console.WriteLine($"{nameof(GuildEventLogger)}.{nameof(LogInfo)}: {e.Message}");
}
}
public async Task LogWarning(DiscordGuild guild, string message)
{
ulong guildId = guild.Id;
if (!_logChannels.ContainsKey(guildId))
return;
try
{
Task<DiscordMessage> sendTask = _logChannels[guildId].SendMessageAsync(new DiscordEmbedBuilder()
.WithDescription(message)
.WithColor(WarningColor));
await sendTask;
}
catch (Exception e)
{
Console.WriteLine($"{nameof(GuildEventLogger)}.{nameof(LogWarning)}: {e.Message}");
}
}
public async Task LogError(DiscordGuild guild, string message)
{
ulong guildId = guild.Id;
if (!_logChannels.ContainsKey(guildId))
return;
try
{
Task<DiscordMessage> sendTask = _logChannels[guildId].SendMessageAsync(new DiscordEmbedBuilder()
.WithDescription(message)
.WithColor(ErrorColor));
await sendTask;
}
catch (Exception e)
{
Console.WriteLine($"{nameof(GuildEventLogger)}.{nameof(LogError)}: {e.Message}");
}
}
private void LoadChannelsFromFile()
{
_logChannels = new Dictionary<ulong, DiscordChannel>();
if (!File.Exists(LogChannelsLocalPath))
return;
string json = File.ReadAllText(LogChannelsLocalPath);
Dictionary<ulong, ulong> dictionaryFromFile = JsonConvert.DeserializeObject<Dictionary<ulong, ulong>>(json);
foreach (KeyValuePair<ulong, ulong> guildAndChannelId in dictionaryFromFile)
{
if (!_client.Guilds.TryGetValue(guildAndChannelId.Key, out DiscordGuild guild) || !guild.Channels.TryGetValue(guildAndChannelId.Value, out DiscordChannel channel))
continue;
_logChannels.Add(guildAndChannelId.Key, channel);
}
}
private void SaveChannelsToFile()
{
Dictionary<ulong, ulong> dictionaryToSerialize = new Dictionary<ulong, ulong>();
foreach (KeyValuePair<ulong, DiscordChannel> logChannel in _logChannels)
{
dictionaryToSerialize.Add(logChannel.Key, logChannel.Value.Id);
}
string json = JsonConvert.SerializeObject(dictionaryToSerialize);
File.WriteAllText(LogChannelsLocalPath, json);
}
}
}