-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathReactionMessage.cs
159 lines (131 loc) · 5.35 KB
/
ReactionMessage.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DSharpPlus;
using DSharpPlus.Entities;
using Newtonsoft.Json;
namespace RoboBot
{
public class ReactionMessage
{
public DiscordMessage Message;
public Dictionary<DiscordEmoji, DiscordRole> Rules = new Dictionary<DiscordEmoji, DiscordRole>();
public ReactionMessage() { }
public ReactionMessage(DiscordMessage message, Dictionary<DiscordEmoji, DiscordRole> rules)
{
Message = message;
Rules = rules;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder("");
sb.Append("\tMessage : " + Message.JumpLink);
sb.Append("\n\tRules :\n");
foreach (KeyValuePair<DiscordEmoji, DiscordRole> rule in Rules)
{
sb.Append($"\t\t{rule.Key} : {rule.Value.Mention}\n");
}
return sb.ToString();
}
}
public class SerializedRule
{
public bool IsGuildEmoji;
public string EmojiNameOrId;
public ulong RoleId;
public SerializedRule(bool isGuildEmoji, string emojiNameOrId, ulong roleId)
{
IsGuildEmoji = isGuildEmoji;
EmojiNameOrId = emojiNameOrId;
RoleId = roleId;
}
}
public class SerializedReactionMessage
{
[JsonIgnore] private static DiscordClient _client;
public ulong GuildId { get; }
public ulong ChannelId { get; }
public ulong MessageId { get; }
public List<SerializedRule> Rules { get; }
public static void SetDiscordClient(DiscordClient client) => _client = client;
public SerializedReactionMessage(ReactionMessage reactionMessage)
{
GuildId = (ulong)reactionMessage.Message.Channel.GuildId;
ChannelId = reactionMessage.Message.ChannelId;
MessageId = reactionMessage.Message.Id;
Rules = new List<SerializedRule>();
foreach (KeyValuePair<DiscordEmoji, DiscordRole> rule in reactionMessage.Rules)
{
if (rule.Key.Id != 0)
{
Rules.Add(new SerializedRule(true, rule.Key.Id.ToString(), rule.Value.Id));
continue;
}
Rules.Add(new SerializedRule(false, rule.Key.GetDiscordName(), rule.Value.Id));
}
}
[JsonConstructor]
public SerializedReactionMessage(ulong guildId, ulong channelId, ulong messageId,
List<SerializedRule> rules)
{
GuildId = guildId;
ChannelId = channelId;
MessageId = messageId;
Rules = rules;
}
public ReactionMessage ToReactionMessage()
{
if (!_client.Guilds.TryGetValue(GuildId, out DiscordGuild guild))
{
Console.WriteLine("Could not get the guild of this message, perhaps the bot have been removed from it? (Skipping ReactionMessage)");
return null;
}
if (!guild.Channels.TryGetValue(ChannelId, out DiscordChannel channel))
{
Console.WriteLine("Could not get the channel of this message, perhaps the bot have not access to it anymore? (Skipping ReactionMessage)");
return null;
}
DiscordMessage message;
try
{
message = channel.GetMessageAsync(MessageId).Result;
}
catch (Exception e)
{
Console.WriteLine("Could not get the channel of this message, perhaps it has been removed? (Skipping ReactionMessage)");
Console.WriteLine(e);
return null;
}
Dictionary<DiscordEmoji, DiscordRole> reactionRules = new Dictionary<DiscordEmoji, DiscordRole>();
foreach (SerializedRule rule in Rules)
{
DiscordEmoji emoji;
if (rule.IsGuildEmoji)
{
ulong id = ulong.Parse(rule.EmojiNameOrId);
if(!DiscordEmoji.TryFromGuildEmote(_client, id, out emoji))
{
GuildEventLogger.Instance.LogWarning(guild, $"Could not load emoji {rule.EmojiNameOrId}. Has it been removed or renamed? (Skipping this rule)");
continue;
}
}
else
{
if(!DiscordEmoji.TryFromName(_client, rule.EmojiNameOrId, out emoji))
{
GuildEventLogger.Instance.LogWarning(guild, $"Could not load emoji {rule.EmojiNameOrId}. Has it been removed or renamed? (Skipping this rule)");
continue;
}
}
if(!guild.Roles.TryGetValue(rule.RoleId, out DiscordRole role))
{
GuildEventLogger.Instance.LogWarning(guild, $"Could not load role ID {rule.RoleId}. Has it been removed? (Skipping this rule)");
continue;
}
reactionRules.Add(emoji, role);
}
return new ReactionMessage(message, reactionRules);
}
}
}