forked from drizzle-mizzle/CharacterAI-Discord-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactionsHandler.cs
211 lines (180 loc) · 8.5 KB
/
ReactionsHandler.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
using CharacterAI;
using Discord;
using Discord.Webhook;
using Discord.WebSocket;
using CharacterAiDiscordBot.Services;
using CharacterAiDiscordBot.Models.Database;
using static CharacterAiDiscordBot.Services.CommonService;
using static CharacterAiDiscordBot.Services.CommandsService;
using Microsoft.Extensions.DependencyInjection;
using CharacterAiDiscordBot.Models.Common;
using static System.Net.Mime.MediaTypeNames;
using Microsoft.EntityFrameworkCore;
namespace CharacterAiDiscordBot.Handlers
{
internal class ReactionsHandler
{
private readonly IServiceProvider _services;
private readonly DiscordSocketClient _client;
private readonly IntegrationService _integration;
public ReactionsHandler(IServiceProvider services)
{
_services = services;
_integration = _services.GetRequiredService<IntegrationService>();
_client = _services.GetRequiredService<DiscordSocketClient>();
_client.ReactionAdded += (msg, channel, reaction) =>
{
Task.Run(async () =>
{
try { await HandleReactionAsync(msg, channel, reaction); }
catch (Exception e) { await HandleReactionException(channel, reaction, e); }
});
return Task.CompletedTask;
};
_client.ReactionRemoved += (msg, channel, reaction) =>
{
Task.Run(async () =>
{
try { await HandleReactionAsync(msg, channel, reaction); }
catch (Exception e) { await HandleReactionException(channel, reaction, e); }
});
return Task.CompletedTask;
};
}
private async Task HandleReactionAsync(Cacheable<IUserMessage, ulong> rawMessage, Cacheable<IMessageChannel, ulong> discordChannel, SocketReaction reaction)
{
var user = reaction.User.GetValueOrDefault();
if (user is null || user is not SocketUser userReacted || userReacted.IsBot) return;
IUserMessage originalMessage;
try { originalMessage = await rawMessage.DownloadAsync(); }
catch { return; }
if (originalMessage.Author.Id != _client.CurrentUser.Id) return;
var db = new StorageContext();
var channel = await db.Channels.FindAsync(discordChannel.Id);
if (channel is null) return;
if ((reaction.Emote?.Name == STOP_BTN.Name) && channel.StopBtnEnabled)
{
channel.SkipNextBotMessage = true;
await db.SaveChangesAsync();
return;
}
//if (reaction.Emote.Name == TRANSLATE_BTN.Name)
//{
// _ = TranslateMessageAsync(message, currentChannel.Data.TranslateLanguage);
// return;
//}
bool userIsLastCaller = channel.LastDiscordUserCallerId == userReacted.Id;
bool msgIsSwipable = originalMessage.Id == channel.LastCharacterDiscordMsgId;
if (!(userIsLastCaller && msgIsSwipable)) return;
if ((reaction.Emote?.Name == ARROW_LEFT.Name) && channel.CurrentSwipeIndex > 0)
{ // left arrow
if (await _integration.CheckIfUserIsBannedAsync(reaction, _client)) return;
channel.CurrentSwipeIndex--;
await db.SaveChangesAsync();
await UpdateCharacterMessage(originalMessage, channel);
}
else if (reaction.Emote?.Name == ARROW_RIGHT.Name)
{ // right arrow
if (await _integration.CheckIfUserIsBannedAsync(reaction, _client)) return;
channel.CurrentSwipeIndex++;
await db.SaveChangesAsync();
await UpdateCharacterMessage(originalMessage, channel);
}
}
/// <summary>
/// Super complicated shit, but I don't want to refactor it...
/// </summary>
private async Task UpdateCharacterMessage(IUserMessage characterOriginalMessage, Channel channel)
{
var availResponses = _integration.AvailableCharacterResponses;
if (channel.HistoryId is null || !availResponses.ContainsKey(channel.HistoryId)) return;
var db = new StorageContext();
// Check if fetching a new message, or just swiping among already available ones
bool gottaFetch = availResponses[channel.HistoryId].Count < channel.CurrentSwipeIndex + 1;
if (gottaFetch)
{
await characterOriginalMessage.ModifyAsync(msg =>
{
msg.Content = null;
msg.Embed = WAIT_MESSAGE;
msg.AllowedMentions = AllowedMentions.None;
});
var characterResponse = await _integration.CallCaiCharacterAsync("", channel.HistoryId, null, channel.LastUserMsgId);
if (characterResponse.IsFailure)
{
await characterOriginalMessage.ModifyAsync(msg =>
{
msg.Embed = characterResponse.Text.ToInlineEmbed(Color.Red);
msg.AllowedMentions = AllowedMentions.All;
});
return;
}
// Add to the storage
var newResponse = new AvailableCharacterResponse()
{
MessageId = characterResponse.CharacterMessageId!,
Text = characterResponse.Text,
ImageUrl = characterResponse.ImageRelPath
};
lock (availResponses)
{
availResponses[channel.HistoryId].Add(newResponse);
}
}
AvailableCharacterResponse newCharacterMessage;
try { newCharacterMessage = availResponses[channel.HistoryId][channel.CurrentSwipeIndex]; }
catch { return; }
channel.LastCharacterMsgId = newCharacterMessage.MessageId;
// Add image the message
Embed? embed = null;
string? imageUrl = newCharacterMessage.ImageUrl;
if (imageUrl is not null && await TryGetImageAsync(imageUrl, _integration.HttpClient))
embed = new EmbedBuilder().WithImageUrl(imageUrl).Build();
// Add text to the message
string responseText = newCharacterMessage.Text ?? " ";
if (responseText.Length > 2000)
responseText = responseText[0..1994] + "[...]";
// Send (update) message
if (responseText.Length > 2000) responseText = responseText[0..1994] + "[max]";
try
{
await characterOriginalMessage.ModifyAsync(msg =>
{
msg.Content = responseText;
msg.Embed = embed;
msg.AllowedMentions = AllowedMentions.All;
});
}
catch
{
return;
}
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException e)
{
e.Entries.Single().Reload();
db.SaveChanges();
}
//var tm = TranslatedMessages.Find(tm => tm.MessageId == message.Id);
//if (tm is not null) tm.IsTranslated = false;
}
private async Task HandleReactionException(Cacheable<IMessageChannel, ulong> channel, SocketReaction reaction, Exception e)
{
LogException(new[] { e });
var guildChannel = (await channel.GetOrDownloadAsync()) as SocketGuildChannel;
var guild = guildChannel?.Guild;
TryToReportInLogsChannel(_client, title: "Reaction Exception",
desc: $"Guild: `{guild?.Name} ({guild?.Id})`\n" +
$"Owner: `{guild?.Owner.GetBestName()} ({guild?.Owner.Username})`\n" +
$"Channel: `{guildChannel?.Name} ({guildChannel?.Id})`\n" +
$"User: `{reaction.User.GetValueOrDefault()?.Username}`\n" +
$"Reaction: {reaction.Emote.Name}",
content: e.ToString(),
color: Color.Red,
error: true);
}
}
}