-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIsabelle.cs
128 lines (103 loc) · 3.99 KB
/
Isabelle.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
using System;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using Eco.Core.Utils;
using Eco.Gameplay.Players;
using Eco.Shared.Localization;
using Eco.Shared.Utils;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
namespace Crossing
{
public class Isabelle
{
private static Guild Guild => AutoSingleton<Guild>.Obj;
private readonly IdentityManager _identity;
private DiscordSocketClient _client;
private IMessageChannel _debug;
private SocketGuild _guild;
public SocketGuild SocketGuild()
{
return _guild;
}
private static bool _ready;
public bool Ready()
{
return _ready;
}
public Isabelle(IServiceProvider services)
{
_identity = services.GetRequiredService<IdentityManager>();
}
public async Task InitializeAsync(DiscordSocketClient discord)
{
_client = discord;
_client.Log += LogAsync;
_client.Ready += ReadyAsync;
_client.MessageReceived += MessageReceivedAsync;
try
{
await _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("ISABELLE_TOKEN"));
await _client.StartAsync();
Log.WriteLine(Localizer.Do($"[ISABELLE] Succesfully logged in to Discord"));
} catch (Exception e)
{
Log.WriteLine(Localizer.Do($"[ISABELLE] Error logging in to Discord {e.ToString()}"));
}
}
private Task LogAsync(LogMessage msg)
{
Log.WriteLine(Localizer.Do($"[ISABELLE] {msg.ToString()}"));
return Task.CompletedTask;
}
private async Task ReadyAsync()
{
_debug = (IMessageChannel)_client.GetChannel(Guild.DebugChannel);
if (_debug == null)
{
throw new Exception("[ISABELLE] Could not find channel");
}
_guild = _client.GetGuild(Guild.Id);
if (_guild == null)
{
throw new Exception("[ISABELLE] Could not find guild");
}
_ready = true;
await _debug.SendMessageAsync("Isabelle connected.");
}
private async Task MessageReceivedAsync(SocketMessage message)
{
if (message.Author.IsBot || message.Author.IsWebhook)
{
return;
}
await HandleThankMention(message);
}
private async Task HandleThankMention(SocketMessage message)
{
IReadOnlyCollection<SocketUser> mentions = message.MentionedUsers;
if (mentions.Count == 0) { return; }
string[] thankMatches = {"thank", "ty", "thx", "appreciate", "cheers"};
if (!message.Content.ToLower().ContainsAny(thankMatches)) { return; }
string steamId = _identity.DiscordToSteam.GetOrDefault($"{message.Author.Id}");
if (steamId == "") { return; }
var user = UserManager.FindUserBySteamId(steamId);
if (user == null || user.Player == null) { return; }
foreach(SocketUser mention in mentions)
{
if (mention.Id == message.Author.Id)
{
continue;
}
string mentionSteamId = _identity.DiscordToSteam.GetOrDefault($"{mention.Id}");
if (mentionSteamId == "") { continue; }
var mentionUser = UserManager.FindUserBySteamId(mentionSteamId);
if (mentionUser == null) { continue; }
user.Player.GiveReputationTo(mentionUser.Name, 1, message.Content);
await _debug.SendMessageAsync($"{message.Author.Mention} gave {mention.Mention} 1 reputation by thanking them in a mention.");
}
await message.AddReactionAsync(new Emoji("🏅"));
}
}
}