-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlathers.cs
214 lines (186 loc) · 7.04 KB
/
Blathers.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
using System;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using Eco.Core.IoC;
using Eco.Core.Utils;
using Eco.Gameplay.Players;
using Eco.Gameplay.Systems.Chat;
using Eco.Gameplay.Systems.TextLinks;
using Eco.Shared.Localization;
using Eco.Shared.Services;
using Eco.Shared.Utils;
using Eco.Simulation.Time;
using Microsoft.Extensions.DependencyInjection;
using Eco.Gameplay.Items;
using Eco.Gameplay.Systems.Tooltip;
using System.Text.RegularExpressions;
using System.Text;
namespace Crossing
{
public class Blathers
{
private static Guild Guild => AutoSingleton<Guild>.Obj;
private readonly IdentityManager _identity;
private readonly Regex _mentionRegex;
private DiscordSocketClient _client;
private IMessageChannel _debug;
private SocketGuild _guild;
public SocketGuild SocketGuild()
{
return _guild;
}
private static bool _ready;
public bool Ready()
{
return _ready;
}
public Blathers(IServiceProvider services)
{
_identity = services.GetRequiredService<IdentityManager>();
_mentionRegex = new Regex(@"<[@#!&]*(:[^:]*:)?([^>]*)>");
}
public async Task InitializeAsync(DiscordSocketClient discord)
{
Log.WriteLine(Localizer.Do($"Blathers initializing."));
_client = discord;
_client.Log += LogAsync;
_client.Ready += ReadyAsync;
_client.MessageReceived += MessageReceivedAsync;
try
{
await _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("BLATHERS_TOKEN"));
await _client.StartAsync();
Log.WriteLine(Localizer.Do($"[BLATHERS] Succesfully logged in to Discord"));
} catch (Exception e)
{
Log.WriteLine(Localizer.Do($"[BLATHERS] Error logging in to Discord {e.ToString()}"));
}
}
private Task LogAsync(LogMessage msg)
{
Log.WriteLine(Localizer.Do($"[BLATHERS] {msg.ToString()}"));
return Task.CompletedTask;
}
private async Task ReadyAsync()
{
_debug = (IMessageChannel)_client.GetChannel(Guild.DebugChannel);
if (_debug == null)
{
throw new Exception("[BLATHERS] Could not find channel");
}
_guild = _client.GetGuild(Guild.Id);
if (_guild == null)
{
throw new Exception("[BLATHERS] Could not find guild");
}
_ready = true;
await _debug.SendMessageAsync("Blathers connected.");
}
private Task MessageReceivedAsync(SocketMessage message)
{
if (message.Author.IsBot || message.Author.IsWebhook)
{
return Task.CompletedTask;
}
if (Environment.GetEnvironmentVariable("STAGING") == "1")
{
if (message.Channel.Name == "staging")
{
HandleGeneral(message);
}
}
else {
if (message.Channel.Name == "general")
{
HandleGeneral(message);
}
}
return Task.CompletedTask;
}
private void HandleGeneral(SocketMessage message)
{
string discordId = $"{message.Author.Id}";
string steamId = _identity.DiscordToSteam.GetOrDefault(discordId);
var user = UserManager.FindUserBySteamId(steamId);
if (user != null)
{
StringBuilder content = new StringBuilder();
int curr = 0;
foreach (Match match in _mentionRegex.Matches(message.Content))
{
content.Append(message.Content.Substring(curr, match.Index - curr));
curr = match.Index + match.Length;
// Handle emotes
if (match.Groups[1].Value.Length > 2)
{
content.Append(match.Groups[1].Value);
continue;
}
// Handle mentions
string value = match.Groups[2].Value;
ulong snowflakeId = Convert.ToUInt64(value);
bool matched = false;
foreach (SocketUser mention in message.MentionedUsers)
{
if (mention.Id == snowflakeId)
{
string mentionSteamId = _identity.DiscordToSteam.GetOrDefault(value);
User ecoMention = UserManager.FindUserBySteamId(mentionSteamId);
if (ecoMention == null)
{
content.Append($"@{mention.Username}");
}
else
{
content.Append($"{ecoMention.UILink()}");
}
matched = true;
break;
}
}
if (!matched)
{
foreach (SocketRole mention in message.MentionedRoles)
{
if (mention.Id == snowflakeId)
{
content.Append($"@{mention.Name}");
matched = true;
break;
}
}
}
if (!matched)
{
foreach (SocketGuildChannel mention in message.MentionedChannels)
{
if (mention.Id == snowflakeId)
{
content.Append($"#{mention.Name}");
matched = true;
break;
}
}
}
}
content.Append(message.Content.Substring(curr, message.Content.Length - curr));
Log.WriteLine(Localizer.Do($"Discord->ECO {message.Author.Username}: {content.ToString()}"));
ChatMessage msg = new ChatMessage
{
Tag = DefaultChatTags.General.TagName(),
Sender = user.Name,
SenderUILink = user.UILink(),
Text = content.ToString(),
TimeSeconds = WorldTime.Seconds,
Category = MessageCategory.Chat
};
ServiceHolder<IChatManager>.Obj.ProcessMessage(msg);
}
else
{
Log.WriteLine(Localizer.Do($"[BLATHERS] Did not find user with SteamId {steamId}"));
}
}
}
}