Better chat injection handling

This commit is contained in:
goat 2019-11-11 23:55:32 +09:00
parent e75841deb9
commit ba5fa1f4d0
11 changed files with 207 additions and 72 deletions

View file

@ -231,13 +231,13 @@ namespace Dalamud.DiscordBot {
wasOutgoingTell = true;
}
var chatTypeConfig =
this.config.ChatTypeConfigurations.FirstOrDefault(typeConfig => typeConfig.ChatType == type);
var chatTypeConfigs =
this.config.ChatTypeConfigurations.Where(typeConfig => typeConfig.ChatType == type);
if (chatTypeConfig == null)
if (!chatTypeConfigs.Any())
return;
var channel = await GetChannel(chatTypeConfig.Channel);
var channels = chatTypeConfigs.Select(c => GetChannel(c.Channel).GetAwaiter().GetResult());
var senderSplit = sender.Split(new[] {this.worldIcon}, StringSplitOptions.None);
@ -271,47 +271,52 @@ namespace Dalamud.DiscordBot {
Log.Error(ex, "Could not get XIVAPI character search result.");
}
var embedBuilder = new EmbedBuilder {
Author = new EmbedAuthorBuilder {
IconUrl = avatarUrl,
Name = wasOutgoingTell
Thread.Sleep(this.config.ChatDelayMs);
for (var chatTypeIndex = 0; chatTypeIndex < chatTypeConfigs.Count(); chatTypeIndex++) {
var embedBuilder = new EmbedBuilder
{
Author = new EmbedAuthorBuilder
{
IconUrl = avatarUrl,
Name = wasOutgoingTell
? "You"
: sender + (string.IsNullOrEmpty(world) || string.IsNullOrEmpty(sender)
? ""
: $" on {world}"),
Url = lodestoneId != 0 ? "https://eu.finalfantasyxiv.com/lodestone/character/" + lodestoneId : null
},
Description = message,
Timestamp = DateTimeOffset.Now,
Footer = new EmbedFooterBuilder {Text = type.GetDetails().FancyName},
Color = new Color((uint) (chatTypeConfig.Color & 0xFFFFFF))
};
Url = lodestoneId != 0 ? "https://eu.finalfantasyxiv.com/lodestone/character/" + lodestoneId : null
},
Description = message,
Timestamp = DateTimeOffset.Now,
Footer = new EmbedFooterBuilder { Text = type.GetDetails().FancyName },
Color = new Color((uint)(chatTypeConfigs.ElementAt(chatTypeIndex).Color & 0xFFFFFF))
};
if (this.config.CheckForDuplicateMessages) {
var recentMsg = this.recentMessages.FirstOrDefault(
msg => msg.Embeds.FirstOrDefault(
embed => embed.Description == embedBuilder.Description &&
embed.Author.HasValue &&
embed.Author.Value.Name == embedBuilder.Author.Name &&
embed.Timestamp.HasValue &&
Math.Abs(
(embed.Timestamp.Value.ToUniversalTime().Date -
embedBuilder
.Timestamp.Value.ToUniversalTime().Date)
.Milliseconds) < 15000)
!= null);
if (this.config.CheckForDuplicateMessages)
{
var recentMsg = this.recentMessages.FirstOrDefault(
msg => msg.Embeds.FirstOrDefault(
embed => embed.Description == embedBuilder.Description &&
embed.Author.HasValue &&
embed.Author.Value.Name == embedBuilder.Author.Name &&
embed.Timestamp.HasValue &&
Math.Abs(
(embed.Timestamp.Value.ToUniversalTime().Date -
embedBuilder
.Timestamp.Value.ToUniversalTime().Date)
.Milliseconds) < 15000)
!= null);
if (recentMsg != null) {
Log.Verbose("Duplicate message: [{0}] {1}", embedBuilder.Author.Name, embedBuilder.Description);
this.recentMessages.Remove(recentMsg);
return;
if (recentMsg != null)
{
Log.Verbose("Duplicate message: [{0}] {1}", embedBuilder.Author.Name, embedBuilder.Description);
this.recentMessages.Remove(recentMsg);
return;
}
}
await channels.ElementAt(chatTypeIndex).SendMessageAsync(embed: embedBuilder.Build());
}
Thread.Sleep(this.config.ChatDelayMs);
await channel.SendMessageAsync(embed: embedBuilder.Build());
}
private async Task<IMessageChannel> GetChannel(ChannelConfiguration channelConfig) {
@ -321,7 +326,7 @@ namespace Dalamud.DiscordBot {
}
private string RemoveAllNonLanguageCharacters(string input) {
return Regex.Replace(input, @"[^\p{L} ]", "");
return Regex.Replace(input, @"[^\p{L} ']", "");
}
public void Dispose() {