forked from adam/discord-bot-shtik
Compare commits
No commits in common. "115035eb914b6d0804c3beda043df894729fbcf8" and "66b425cd39c05587e21799b8ed356f3a3195d568" have entirely different histories.
115035eb91
...
66b425cd39
@ -10,22 +10,17 @@ using System.Collections.Generic;
|
||||
|
||||
public class Behaver
|
||||
{
|
||||
private ChattingContext _db;
|
||||
public List<Account> Selves { get; internal set; } = new List<Account>();
|
||||
public static List<Behavior> Behaviors { get; private set; } = new List<Behavior>();
|
||||
private static List<Behavior> behaviors { get; set; } = new List<Behavior>();
|
||||
internal Behaver()
|
||||
{
|
||||
_db = new ChattingContext();
|
||||
|
||||
var subtypes = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(domainAssembly => domainAssembly.GetTypes())
|
||||
.Where(type => type.IsSubclassOf(typeof(Behavior)) && !type.IsAbstract &&
|
||||
type.GetCustomAttributes(typeof(StaticPlzAttribute),false)?.Any() == true)
|
||||
.Where(type => type.IsSubclassOf(typeof(Behavior)) && !type.IsAbstract)
|
||||
.ToList();
|
||||
|
||||
foreach (var subtype in subtypes)
|
||||
{
|
||||
Behaviors.Add((Behavior)Activator.CreateInstance(subtype));
|
||||
behaviors.Add((Behavior)Activator.CreateInstance(subtype));
|
||||
}
|
||||
}
|
||||
static Behaver() { }
|
||||
@ -39,7 +34,7 @@ public class Behaver
|
||||
|
||||
public async Task<bool> ActOn(Message message)
|
||||
{
|
||||
foreach (var behavior in Behaviors)
|
||||
foreach (var behavior in behaviors)
|
||||
{
|
||||
if (behavior.ShouldAct(message))
|
||||
{
|
||||
@ -47,7 +42,7 @@ public class Behaver
|
||||
message.ActedOn = true;
|
||||
}
|
||||
}
|
||||
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('?'))
|
||||
{
|
||||
Console.WriteLine("providing bullshit nonanswer / admitting uselessness");
|
||||
var responses = new List<string>(){
|
||||
@ -59,7 +54,7 @@ public class Behaver
|
||||
}
|
||||
if (message.ActedOn)
|
||||
{
|
||||
_db.SaveChanges();
|
||||
Shared.dbContext.SaveChanges();
|
||||
}
|
||||
return message.ActedOn;
|
||||
}
|
||||
|
@ -8,14 +8,13 @@ using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
|
||||
//expect a behavior to be created per mesage
|
||||
public abstract class Behavior
|
||||
{
|
||||
public abstract Task<bool> ActOn(Message message);
|
||||
|
||||
public virtual bool ShouldAct(Message message)
|
||||
{
|
||||
if(Behaver.Instance.Selves.Any(acc => acc.Id == message.Author.Id))
|
||||
return false;
|
||||
return Regex.IsMatch(message.Content, $"{Trigger}\\b", RegexOptions.IgnoreCase);
|
||||
}
|
||||
|
||||
@ -23,6 +22,3 @@ public abstract class Behavior
|
||||
public abstract string Trigger { get; }
|
||||
public virtual string Description => Name;
|
||||
}
|
||||
|
||||
|
||||
public class StaticPlzAttribute : Attribute {}
|
@ -8,7 +8,6 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using vassago.Models;
|
||||
|
||||
[StaticPlz]
|
||||
public class ChatGPTSnark : Behavior
|
||||
{
|
||||
public override string Name => "ChatGPTSnark";
|
||||
|
@ -8,7 +8,6 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using vassago.Models;
|
||||
|
||||
[StaticPlz]
|
||||
public class DefinitionSnarkCogDiss : Behavior
|
||||
{
|
||||
public override string Name => "Definition Snarkiness: cognitivie dissonance";
|
||||
|
@ -8,7 +8,6 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using vassago.Models;
|
||||
|
||||
[StaticPlz]
|
||||
public class DefinitionSnarkGaslight : Behavior
|
||||
{
|
||||
public override string Name => "Definition Snarkiness: gaslighting";
|
||||
|
@ -6,8 +6,6 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using vassago.Models;
|
||||
|
||||
[StaticPlz]
|
||||
public class Detiktokify : Behavior
|
||||
{
|
||||
public override string Name { get => "Detiktokify"; }
|
||||
@ -81,7 +79,6 @@ public class Detiktokify : Behavior
|
||||
}
|
||||
else
|
||||
{
|
||||
message.ActedOn = true;
|
||||
Console.WriteLine($"file appears too big ({bytesize} bytes ({bytesize / (1024 * 1024)}MB)), not posting");
|
||||
}
|
||||
File.Delete(path);
|
||||
|
@ -9,7 +9,6 @@ using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using vassago.Models;
|
||||
|
||||
[StaticPlz]
|
||||
public class FiximageHeic : Behavior
|
||||
{
|
||||
public override string Name => "deheic";
|
||||
@ -21,8 +20,6 @@ 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))
|
||||
return false;
|
||||
if (message.Attachments?.Count() > 0)
|
||||
{
|
||||
foreach (var att in message.Attachments)
|
||||
|
@ -10,7 +10,6 @@ using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using vassago.Models;
|
||||
|
||||
[StaticPlz]
|
||||
public class GeneralSnarkCloudNative : Behavior
|
||||
{
|
||||
public override string Name => "general snarkiness: cloud native";
|
||||
@ -18,8 +17,6 @@ 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))
|
||||
return false;
|
||||
return Regex.IsMatch(message.Content, "\\bcloud( |-)?native\\b", RegexOptions.IgnoreCase) ||
|
||||
Regex.IsMatch(message.Content, "\\benterprise( |-)?(level|solution)\\b", RegexOptions.IgnoreCase);
|
||||
}
|
||||
|
@ -9,7 +9,6 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using vassago.Models;
|
||||
|
||||
[StaticPlz]
|
||||
public class GeneralSnarkPlaying : Behavior
|
||||
{
|
||||
public override string Name => "playin Snarkiness";
|
||||
@ -20,8 +19,6 @@ public class GeneralSnarkPlaying : Behavior
|
||||
|
||||
public override bool ShouldAct(Message message)
|
||||
{
|
||||
if(Behaver.Instance.Selves.Any(acc => acc.Id == message.Author.Id))
|
||||
return false;
|
||||
return Regex.IsMatch(message.Content, "^(s?he|(yo)?u|y'?all|they) thinks? i'?m (playin|jokin|kiddin)g?$", RegexOptions.IgnoreCase);
|
||||
}
|
||||
public override async Task<bool> ActOn(Message message)
|
||||
|
@ -8,7 +8,6 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using vassago.Models;
|
||||
|
||||
[StaticPlz]
|
||||
public class GeneralSnarkSkynet : Behavior
|
||||
{
|
||||
public override string Name => "Skynet Snarkiness";
|
||||
|
@ -9,7 +9,6 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using vassago.Models;
|
||||
|
||||
[StaticPlz]
|
||||
public class Gratitude : Behavior
|
||||
{
|
||||
public override string Name => "Gratitude";
|
||||
@ -18,8 +17,6 @@ public class Gratitude : Behavior
|
||||
|
||||
public override bool ShouldAct(Message message)
|
||||
{
|
||||
if(Behaver.Instance.Selves.Any(acc => acc.Id == 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)
|
||||
@ -31,7 +28,7 @@ public class Gratitude : Behavior
|
||||
await message.Channel.SendMessage("you're welcome, citizen!");
|
||||
break;
|
||||
case 1:
|
||||
await message.React(":)");
|
||||
await message.React("☺");
|
||||
break;
|
||||
case 2:
|
||||
await message.React("\U0001F607"); //smiling face with halo
|
||||
@ -40,7 +37,7 @@ public class Gratitude : Behavior
|
||||
switch (Shared.r.Next(9))
|
||||
{
|
||||
case 0:
|
||||
await message.React("<3"); //normal heart, usually rendered red
|
||||
await message.React("❤"); //normal heart, usually rendered red
|
||||
break;
|
||||
case 1:
|
||||
await message.React("\U0001F9E1"); //orange heart
|
||||
|
@ -9,7 +9,6 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using vassago.Models;
|
||||
|
||||
[StaticPlz]
|
||||
public class Joke : Behavior
|
||||
{
|
||||
public override string Name => "Joke";
|
||||
@ -35,12 +34,12 @@ public class Joke : Behavior
|
||||
{
|
||||
var firstIndexAfterQuestionMark = thisJoke.LastIndexOf('?') + 1;
|
||||
var straightline = thisJoke.Substring(0, firstIndexAfterQuestionMark);
|
||||
var punchline = thisJoke.Substring(firstIndexAfterQuestionMark, thisJoke.Length - firstIndexAfterQuestionMark).Trim();
|
||||
var punchline = thisJoke.Substring(firstIndexAfterQuestionMark, thisJoke.Length - firstIndexAfterQuestionMark);
|
||||
Task.WaitAll(message.Channel.SendMessage(straightline));
|
||||
Thread.Sleep(TimeSpan.FromSeconds(Shared.r.Next(5, 30)));
|
||||
//if (Shared.r.Next(8) == 0)
|
||||
{
|
||||
Behaver.Behaviors.Add(new LaughAtOwnJoke(punchline));
|
||||
LaughAtOwnJoke.punchlinesAwaitingReaction.Add(punchline);
|
||||
}
|
||||
await message.Channel.SendMessage(punchline);
|
||||
// var myOwnMsg = await message.Channel.SendMessage(punchline);
|
||||
|
@ -9,7 +9,6 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using vassago.Models;
|
||||
|
||||
|
||||
public class LaughAtOwnJoke : Behavior
|
||||
{
|
||||
public override string Name => "Laugh at own jokes";
|
||||
@ -17,23 +16,20 @@ public class LaughAtOwnJoke : Behavior
|
||||
public override string Trigger => "1 in 8";
|
||||
|
||||
public override string Description => Name;
|
||||
private string _punchline{get;set;}
|
||||
public static List<string> punchlinesAwaitingReaction = new List<string>();
|
||||
|
||||
public LaughAtOwnJoke(string punchline)
|
||||
{
|
||||
_punchline = punchline;
|
||||
}
|
||||
public override bool ShouldAct(Message message)
|
||||
{
|
||||
Console.WriteLine($"{message.Content} == {_punchline}");
|
||||
return message.Content == _punchline
|
||||
&& Behaver.Instance.Selves.Any(acc => acc.Id == message.Author.Id);
|
||||
|
||||
//TODO: i need to keep track of myself from here somehow
|
||||
//return false;
|
||||
return /*message.Author == me &&*/ punchlinesAwaitingReaction.Contains(message.Content);
|
||||
}
|
||||
|
||||
public override async Task<bool> ActOn(Message message)
|
||||
{
|
||||
punchlinesAwaitingReaction.Remove(message.Content);
|
||||
await message.React("\U0001F60E"); //smiling face with sunglasses
|
||||
Behaver.Behaviors.Remove(this);
|
||||
return true;
|
||||
}
|
||||
}
|
@ -10,7 +10,6 @@ using Newtonsoft.Json;
|
||||
using vassago.Models;
|
||||
using QRCoder;
|
||||
|
||||
[StaticPlz]
|
||||
public class PepTalk : Behavior
|
||||
{
|
||||
public override string Name => "PepTalk";
|
||||
|
@ -8,12 +8,11 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using vassago.Models;
|
||||
|
||||
[StaticPlz]
|
||||
public class PulseCheck : Behavior
|
||||
{
|
||||
public override string Name => "pulse check";
|
||||
|
||||
public override string Trigger => "!pulse ?check";
|
||||
public override string Trigger => "!pluse ?check";
|
||||
|
||||
public override async Task<bool> ActOn(Message message)
|
||||
{
|
||||
|
@ -10,7 +10,6 @@ using Newtonsoft.Json;
|
||||
using vassago.Models;
|
||||
using QRCoder;
|
||||
|
||||
[StaticPlz]
|
||||
public class QRify : Behavior
|
||||
{
|
||||
public override string Name => "qr-ify";
|
||||
|
@ -3,8 +3,6 @@ namespace vassago.Behavior;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using vassago.Models;
|
||||
|
||||
[StaticPlz]
|
||||
public class UnitConvert : Behavior
|
||||
{
|
||||
public override string Name => "Unit conversion";
|
||||
@ -23,12 +21,11 @@ public class UnitConvert : Behavior
|
||||
if (decimal.TryParse(theseMatches[0].Groups[1].Value, out asNumeric))
|
||||
{
|
||||
await message.Channel.SendMessage(Conversion.Converter.Convert(asNumeric, theseMatches[0].Groups[2].Value, theseMatches[0].Groups[4].Value.ToLower()));
|
||||
return true;
|
||||
}
|
||||
await message.Channel.SendMessage("mysteriously semi-parsable");
|
||||
return true;
|
||||
}
|
||||
await message.Channel.SendMessage( "unparsable");
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -8,7 +8,6 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using vassago.Models;
|
||||
|
||||
[StaticPlz]
|
||||
public class WishLuck : Behavior
|
||||
{
|
||||
public override string Name => "wish me luck";
|
||||
|
@ -29,7 +29,7 @@ public class DiscordInterface
|
||||
};
|
||||
public DiscordInterface()
|
||||
{
|
||||
_db = new ChattingContext();
|
||||
_db = Shared.dbContext;
|
||||
}
|
||||
|
||||
public async Task Init(string token)
|
||||
@ -41,7 +41,6 @@ public class DiscordInterface
|
||||
Console.WriteLine(msg.ToString());
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
client.Connected += SelfConnected;
|
||||
|
||||
client.Ready += () => Task.Run(() =>
|
||||
{
|
||||
@ -79,18 +78,10 @@ public class DiscordInterface
|
||||
await client.StartAsync();
|
||||
}
|
||||
|
||||
private async Task SelfConnected()
|
||||
{
|
||||
|
||||
var selfUser = UpsertAccount(client.CurrentUser);
|
||||
await _db.SaveChangesAsync();
|
||||
Behaver.Instance.Selves.Add(selfUser);
|
||||
}
|
||||
|
||||
#pragma warning disable 4014 //the "you're not awaiting this" warning. yeah I know, that's the beauty of an async method lol
|
||||
#pragma warning disable 1998 //the "it's async but you're not awaiting anything".
|
||||
#pragma warning disable 4014 //the "you're not awaiting this" warning. yeah I know, that's the beauty of an async method lol
|
||||
#pragma warning disable 1998 //the "it's async but you're not awaiting anything".
|
||||
private async Task MessageReceived(SocketMessage messageParam)
|
||||
#pragma warning restore 1998
|
||||
#pragma warning restore 1998
|
||||
{
|
||||
var suMessage = messageParam as SocketUserMessage;
|
||||
if (suMessage == null)
|
||||
@ -108,7 +99,7 @@ public class DiscordInterface
|
||||
m.MentionsMe = true;
|
||||
}
|
||||
|
||||
// if ((suMessage.Author.Id != client.CurrentUser.Id))
|
||||
// if ((suMessage.Author.Id != client.CurrentUser.Id))
|
||||
{
|
||||
if (await Behaver.Instance.ActOn(m))
|
||||
{
|
||||
@ -121,10 +112,10 @@ public class DiscordInterface
|
||||
private void UserJoined(SocketGuildUser arg)
|
||||
{
|
||||
|
||||
var guild = UpsertChannel(arg.Guild);
|
||||
var defaultChannel = UpsertChannel(arg.Guild.DefaultChannel);
|
||||
defaultChannel.ParentChannel = guild;
|
||||
var u = UpsertAccount(arg);
|
||||
var guild = UpsertChannel(arg.Guild);
|
||||
var defaultChannel = UpsertChannel(arg.Guild.DefaultChannel);
|
||||
defaultChannel.ParentChannel = guild;
|
||||
var u = UpsertUser(arg);
|
||||
}
|
||||
private async Task ButtonHandler(SocketMessageComponent component)
|
||||
{
|
||||
@ -201,14 +192,14 @@ public class DiscordInterface
|
||||
m.Attachments.Add(UpsertAttachment(da));
|
||||
}
|
||||
}
|
||||
m.Author = UpsertAccount(dMessage.Author);
|
||||
m.Author = UpsertUser(dMessage.Author);
|
||||
m.Channel = UpsertChannel(dMessage.Channel);
|
||||
m.Content = dMessage.Content;
|
||||
m.ExternalId = dMessage.Id;
|
||||
m.Timestamp = dMessage.EditedTimestamp ?? dMessage.CreatedAt;
|
||||
|
||||
|
||||
if(dMessage.Author.Id != client.CurrentUser.Id && dMessage.MentionedUserIds?.FirstOrDefault(muid => muid == client.CurrentUser.Id) != null)
|
||||
if (dMessage.MentionedUserIds?.FirstOrDefault(muid => muid == client.CurrentUser.Id) != null)
|
||||
{
|
||||
m.MentionsMe = true;
|
||||
}
|
||||
@ -227,17 +218,12 @@ public class DiscordInterface
|
||||
var c = _db.Channels.FirstOrDefault(c => c.ExternalId == msg.Channel.Id);
|
||||
//var preferredEmote = c.EmoteOverrides?[e] ?? e; //TODO: emote overrides
|
||||
var preferredEmote = e;
|
||||
Emoji emoji;
|
||||
if (Emoji.TryParse(preferredEmote, out emoji))
|
||||
{
|
||||
return msg.AddReactionAsync(emoji);
|
||||
}
|
||||
Emote emote;
|
||||
if (!Emote.TryParse(preferredEmote, out emote))
|
||||
if(!Emote.TryParse(preferredEmote, out emote))
|
||||
{
|
||||
if (preferredEmote == e)
|
||||
if(preferredEmote == e)
|
||||
Console.Error.WriteLine($"never heard of emote {e}");
|
||||
return Task.CompletedTask;
|
||||
return null;
|
||||
}
|
||||
|
||||
return msg.AddReactionAsync(emote);
|
||||
@ -308,7 +294,7 @@ public class DiscordInterface
|
||||
c.SendFile = (f, t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; send file"); };
|
||||
return c;
|
||||
}
|
||||
internal Account UpsertAccount(IUser user)
|
||||
internal Account UpsertUser(IUser user)
|
||||
{
|
||||
var addPlease = false;
|
||||
var u = _db.Users.FirstOrDefault(ui => ui.ExternalId == user.Id);
|
||||
@ -336,11 +322,11 @@ public class DiscordInterface
|
||||
//c.OtherUsers = c.OtherUsers ?? new List<User>();
|
||||
//c.OtherUsers = await channel.GetUsersAsync();
|
||||
var dChannel = client.GetChannel(channel.ExternalId.Value);
|
||||
if (dChannel is IGuild)
|
||||
if(dChannel is IGuild)
|
||||
{
|
||||
var guild = channel as IGuild;
|
||||
}
|
||||
else if (dChannel is IGuildChannel)
|
||||
else if(dChannel is IGuildChannel)
|
||||
{
|
||||
var gc = dChannel as IGuildChannel;
|
||||
}
|
||||
|
@ -23,9 +23,10 @@ namespace vassago
|
||||
public async Task MainAsync()
|
||||
{
|
||||
Shared.DBConnectionString = config.DBConnectionString;
|
||||
var dbc = new ChattingContext();
|
||||
dbc.Database.EnsureCreated();
|
||||
|
||||
Shared.dbContext = new ChattingContext();
|
||||
{
|
||||
Shared.dbContext.Database.EnsureCreated();
|
||||
}
|
||||
Conversion.Converter.Load(config.ExchangePairsLocation);
|
||||
if(config.DiscordTokens.Any())
|
||||
foreach(var dt in config.DiscordTokens)
|
||||
|
Loading…
Reference in New Issue
Block a user