IsSelf fix for Definitely snarkiness

notes to self. 1) trust in upsert. an account has an external ID, a channel has an external ID w.r.t. its protocol. 2) as long as you can collapse a User, collapse Self.
This commit is contained in:
Adam R Grey 2024-01-05 21:39:31 -05:00
parent 451ace753d
commit efb4ab00d2
17 changed files with 124 additions and 29 deletions

View File

@ -11,7 +11,8 @@ using System.Collections.Generic;
public class Behaver
{
private ChattingContext _db;
public List<Account> Selves { get; internal set; } = new List<Account>();
private List<Account> SelfAccounts { get; set; } = new List<Account>();
private User SelfUser { get; set; }
public static List<Behavior> Behaviors { get; private set; } = new List<Behavior>();
internal Behaver()
{
@ -48,7 +49,7 @@ public class Behaver
Console.WriteLine("acted on, moving forward");
}
}
if (message.ActedOn == false && message.MentionsMe && message.Content.Contains('?') && !Behaver.Instance.Selves.Any(acc => acc.Id == message.Author.Id))
if (message.ActedOn == false && message.MentionsMe && message.Content.Contains('?') && !Behaver.Instance.SelfAccounts.Any(acc => acc.Id == message.Author.Id))
{
Console.WriteLine("providing bullshit nonanswer / admitting uselessness");
var responses = new List<string>(){
@ -60,5 +61,25 @@ public class Behaver
}
return message.ActedOn;
}
internal bool IsSelf(Guid AccountId)
{
var acc = _db.Accounts.Find(AccountId);
return SelfAccounts.Any(acc => acc.Id == AccountId);
}
public void MarkSelf(Account selfAccount)
{
if(SelfUser == null)
{
SelfUser = selfAccount.IsUser;
}
else if (SelfUser != selfAccount.IsUser)
{
//TODO: collapse
}
SelfAccounts = _db.Accounts.Where(a => a.IsUser == SelfUser).ToList();
}
}
#pragma warning restore 4014 //the "async not awaited" error

View File

@ -14,7 +14,7 @@ public abstract class Behavior
public virtual bool ShouldAct(Message message)
{
if(Behaver.Instance.Selves.Any(acc => acc.Id == message.Author.Id))
if(Behaver.Instance.IsSelf(message.Author.Id))
return false;
return Regex.IsMatch(message.Content, $"{Trigger}\\b", RegexOptions.IgnoreCase);
}

View File

@ -26,10 +26,11 @@ public class Detiktokify : Behavior
}
public override bool ShouldAct(Message message)
{
if(message.Channel.EffectivePermissions.MaxAttachmentBytes == 0)
if(Behaver.Instance.IsSelf(message.Author.Id))
return false;
if(Behaver.Instance.Selves.Any(acc => acc.Id == message.Author.Id))
if(message.Channel.EffectivePermissions.MaxAttachmentBytes == 0)
return false;
var wordLikes = message.Content.Split(' ', StringSplitOptions.TrimEntries);

View File

@ -21,8 +21,9 @@ public class FiximageHeic : Behavior
private List<Attachment> heics = new List<Attachment>();
public override bool ShouldAct(Message message)
{
if(Behaver.Instance.Selves.Any(acc => acc.Id == message.Author.Id))
if(Behaver.Instance.IsSelf(message.Author.Id))
return false;
if (message.Attachments?.Count() > 0)
{
foreach (var att in message.Attachments)

View File

@ -19,7 +19,7 @@ public class GeneralSnarkCloudNative : Behavior
public override string Trigger => "certain tech buzzwords that no human uses in normal conversation";
public override bool ShouldAct(Message message)
{
if(Behaver.Instance.Selves.Any(acc => acc.Id == message.Author.Id))
if(Behaver.Instance.IsSelf(message.Author.Id))
return false;
if(message.Channel.EffectivePermissions.ReactionsPossible)

View File

@ -20,8 +20,9 @@ public class GeneralSnarkGooglit : Behavior
public override bool ShouldAct(Message message)
{
if(Behaver.Instance.Selves.Any(acc => acc.Id == message.Author.Id))
if(Behaver.Instance.IsSelf(message.Author.Id))
return false;
return Regex.IsMatch(message.Content, $"(just )?google( (it|that|things|before))?\\b", RegexOptions.IgnoreCase);
}

View File

@ -0,0 +1,62 @@
namespace vassago.Behavior;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using vassago.Models;
using static vassago.Models.Enumerations;
[StaticPlz]
public class GeneralSnarkMisspellDefinitely : Behavior
{
public override string Name => "Snarkiness: misspell definitely";
public override string Trigger => "definitely but not";
public override string Description => "https://xkcd.com/2871/";
private Dictionary<string, string> snarkmap = new Dictionary<string, string>()
{
{"definetly", "*almost* definitely"},
{"definately", "probably"},
{"definatly", "probably not"},
{"defenitely", "not telling (it's a surprise)"},
{"defintely", "per the propheecy"},
{"definetely", "definitely, maybe"},
{"definantly", "to be decided by coin toss"},
{"defanitely", "in one universe out of 14 million"},
{"defineatly", "only the gods know"},
{"definitly", "unless someone cute shows up"}
};
public override bool ShouldAct(Message message)
{
if(Behaver.Instance.IsSelf(message.Author.Id))
return false;
// if((MeannessFilterLevel)message.Channel.EffectivePermissions.MeannessFilterLevel < MeannessFilterLevel.Medium)
// return false;
foreach(var k in snarkmap.Keys)
{
if( Regex.IsMatch(message.Content, "\\b"+k+"\\b", RegexOptions.IgnoreCase))
return true;
}
return false;
}
public override async Task<bool> ActOn(Message message)
{
foreach(var k in snarkmap.Keys)
{
if( Regex.IsMatch(message.Content, "\\b"+k+"\\b", RegexOptions.IgnoreCase))
{
await message.Reply(k + "? so... " + snarkmap[k] + "?");
return true;
}
}
return true;
}
}

View File

@ -21,7 +21,7 @@ public class GeneralSnarkPlaying : Behavior
public override bool ShouldAct(Message message)
{
if(Behaver.Instance.Selves.Any(acc => acc.Id == message.Author.Id))
if(Behaver.Instance.IsSelf(message.Author.Id))
return false;
if((MeannessFilterLevel)message.Channel.EffectivePermissions.MeannessFilterLevel < MeannessFilterLevel.Medium ||

View File

@ -19,6 +19,10 @@ public class GeneralSnarkSkynet : Behavior
public override async Task<bool> ActOn(Message message)
{
if(Behaver.Instance.IsSelf(message.Author.Id))
return false;
switch (Shared.r.Next(5))
{
default:

View File

@ -18,8 +18,9 @@ public class Gratitude : Behavior
public override bool ShouldAct(Message message)
{
if(Behaver.Instance.Selves.Any(acc => acc.Id == message.Author.Id))
if(Behaver.Instance.IsSelf(message.Author.Id))
return false;
return Regex.IsMatch(message.Content, "\\bthank (yo)?u\\b", RegexOptions.IgnoreCase) && message.MentionsMe;
}
public override async Task<bool> ActOn(Message message)

View File

@ -69,9 +69,12 @@ public class LaughAtOwnJoke : Behavior
}
public override bool ShouldAct(Message message)
{
if(Behaver.Instance.IsSelf(message.Author.Id))
return false;
Console.WriteLine($"{message.Content} == {_punchline}");
return message.Content == _punchline
&& Behaver.Instance.Selves.Any(acc => acc.Id == message.Author.Id);
&& Behaver.Instance.IsSelf(message.Author.Id);
}
public override async Task<bool> ActOn(Message message)

View File

@ -59,6 +59,9 @@ public class LinkClose : Behavior
public override async Task<bool> ActOn(Message message)
{
if(Behaver.Instance.IsSelf(message.Author.Id))
return false;
var secondary = message.Author.IsUser;
if(_primary.IsUser.Id == secondary.Id)
{

View File

@ -15,12 +15,13 @@ public class PepTalk : Behavior
{
public override string Name => "PepTalk";
public override string Trigger => "i need (an? )?(peptalk|inspiration|ego-?boost)";
public override string Trigger => "\\bneeds? (an? )?(peptalk|inspiration|ego-?boost)";
public override string Description => "assembles a pep talk from a few pieces";
public override async Task<bool> ActOn(Message message)
{var piece1 = new List<string>{
{
var piece1 = new List<string>{
"Champ, ",
"Fact: ",
"Everybody says ",

View File

@ -20,7 +20,6 @@ public class Channel
public List<Message> Messages { get; set; }
public List<Account> Users { get; set; }
public ChannelType ChannelType {get; set; }
//public Dictionary<string, string> EmoteOverrides{get;set;}
[NonSerialized]
public Func<string, string, Task> SendFile;

View File

@ -117,11 +117,11 @@ public class DiscordInterface
private async Task SelfConnected()
{
var selfUser = UpsertAccount(client.CurrentUser, protocolAsChannel.Id);
selfUser.DisplayName = client.CurrentUser.Username;
var selfAccount = UpsertAccount(client.CurrentUser, protocolAsChannel.Id);
selfAccount.DisplayName = client.CurrentUser.Username;
await _db.SaveChangesAsync();
Behaver.Instance.Selves.Add(selfUser);
Behaver.Instance.MarkSelf(selfAccount);
}
private async Task MessageReceived(SocketMessage messageParam)
@ -332,13 +332,11 @@ public class DiscordInterface
var c = _db.Channels.FirstOrDefault(c => c.ExternalId == msg.Channel.Id.ToString());
//var preferredEmote = c.EmoteOverrides?[e] ?? e; //TODO: emote overrides
var preferredEmote = e;
Emoji emoji;
if (Emoji.TryParse(preferredEmote, out emoji))
if (Emoji.TryParse(preferredEmote, out Emoji emoji))
{
return msg.AddReactionAsync(emoji);
}
Emote emote;
if (!Emote.TryParse(preferredEmote, out emote))
if (!Emote.TryParse(preferredEmote, out Emote emote))
{
if (preferredEmote == e)
Console.Error.WriteLine($"never heard of emote {e}");

View File

@ -2,6 +2,6 @@ namespace vassago.ProtocolInterfaces;
public static class ProtocolList
{
public static List<DiscordInterface.DiscordInterface> discords = new List<DiscordInterface.DiscordInterface>();
public static List<TwitchInterface.TwitchInterface> twitchs = new List<TwitchInterface.TwitchInterface>();
public static List<DiscordInterface.DiscordInterface> discords = new();
public static List<TwitchInterface.TwitchInterface> twitchs = new();
}

View File

@ -142,10 +142,10 @@ public class TwitchInterface
private async void Client_OnConnected(object sender, OnConnectedArgs e)
{
var selfUser = UpsertAccount(e.BotUsername, protocolAsChannel.Id);
var selfAccount = UpsertAccount(e.BotUsername, protocolAsChannel.Id);
await _db.SaveChangesAsync();
Behaver.Instance.Selves.Add(selfUser);
Behaver.Instance.MarkSelf(selfAccount);
Console.WriteLine($"Connected to {e.AutoJoinChannel}");
}