distinguish users and accounts

This commit is contained in:
Adam R Grey 2023-06-19 12:56:40 -04:00
parent e433e56fec
commit 66b425cd39
15 changed files with 474 additions and 93 deletions

View File

@ -34,9 +34,6 @@ public class Behaver
public async Task<bool> ActOn(Message message) public async Task<bool> ActOn(Message message)
{ {
var permissions = new PermissionSettings(); //TODO: get combined permissions for author and channel
var contentWithoutMention = message.Content;
foreach (var behavior in behaviors) foreach (var behavior in behaviors)
{ {
if (behavior.ShouldAct(message)) if (behavior.ShouldAct(message))
@ -45,7 +42,7 @@ public class Behaver
message.ActedOn = true; message.ActedOn = true;
} }
} }
if (message.ActedOn == false && message.MentionsMe && contentWithoutMention.Contains('?')) if (message.ActedOn == false && message.MentionsMe && message.Content.Contains('?'))
{ {
Console.WriteLine("providing bullshit nonanswer / admitting uselessness"); Console.WriteLine("providing bullshit nonanswer / admitting uselessness");
var responses = new List<string>(){ var responses = new List<string>(){

View File

@ -45,12 +45,13 @@ public class Detiktokify : Behavior
{ {
foreach(var link in tiktokLinks) foreach(var link in tiktokLinks)
{ {
#pragma warning disable 4014
message.React("tiktokbad");
#pragma warning restore 4014
try try
{ {
Console.WriteLine("detiktokifying");
#pragma warning disable 4014
await message.React("<:tiktok:1070038619584200884>");
#pragma warning restore 4014
var res = await ytdl.RunVideoDownload(link.ToString()); var res = await ytdl.RunVideoDownload(link.ToString());
if (!res.Success) if (!res.Success)
{ {

View File

@ -37,12 +37,12 @@ public class Joke : Behavior
var punchline = thisJoke.Substring(firstIndexAfterQuestionMark, thisJoke.Length - firstIndexAfterQuestionMark); var punchline = thisJoke.Substring(firstIndexAfterQuestionMark, thisJoke.Length - firstIndexAfterQuestionMark);
Task.WaitAll(message.Channel.SendMessage(straightline)); Task.WaitAll(message.Channel.SendMessage(straightline));
Thread.Sleep(TimeSpan.FromSeconds(Shared.r.Next(5, 30))); Thread.Sleep(TimeSpan.FromSeconds(Shared.r.Next(5, 30)));
await message.Channel.SendMessage(punchline); //if (Shared.r.Next(8) == 0)
// var myOwnMsg = await message.Channel.SendMessage(punchline);
if (Shared.r.Next(8) == 0)
{ {
LaughAtOwnJoke.punchlinesAwaitingReaction.Add(punchline); LaughAtOwnJoke.punchlinesAwaitingReaction.Add(punchline);
} }
await message.Channel.SendMessage(punchline);
// var myOwnMsg = await message.Channel.SendMessage(punchline);
}); });
#pragma warning restore 4014 #pragma warning restore 4014
} }

View File

@ -20,9 +20,10 @@ public class LaughAtOwnJoke : Behavior
public override bool ShouldAct(Message message) public override bool ShouldAct(Message message)
{ {
//TODO: i need to keep track of myself from here somehow //TODO: i need to keep track of myself from here somehow
return false; //return false;
//return message.Author == me && punchlinesAwaitingReaction.Contains(message.Content); return /*message.Author == me &&*/ punchlinesAwaitingReaction.Contains(message.Content);
} }
public override async Task<bool> ActOn(Message message) public override async Task<bool> ActOn(Message message)

View File

@ -47,7 +47,7 @@ public class DiscordInterface
if (!eventsSignedUp) if (!eventsSignedUp)
{ {
eventsSignedUp = true; eventsSignedUp = true;
Console.WriteLine("Bot is connected! going to sign up for message received and user joined in client ready"); Console.WriteLine($"Bot is connected {client.CurrentUser.Mention}! going to sign up for message received and user joined in client ready");
client.MessageReceived += MessageReceived; client.MessageReceived += MessageReceived;
// _client.MessageUpdated += // _client.MessageUpdated +=
@ -99,7 +99,7 @@ public class DiscordInterface
m.MentionsMe = true; m.MentionsMe = true;
} }
if ((suMessage.Author.Id != client.CurrentUser.Id)) // if ((suMessage.Author.Id != client.CurrentUser.Id))
{ {
if (await Behaver.Instance.ActOn(m)) if (await Behaver.Instance.ActOn(m))
{ {
@ -198,6 +198,7 @@ public class DiscordInterface
m.ExternalId = dMessage.Id; m.ExternalId = dMessage.Id;
m.Timestamp = dMessage.EditedTimestamp ?? dMessage.CreatedAt; m.Timestamp = dMessage.EditedTimestamp ?? dMessage.CreatedAt;
if (dMessage.MentionedUserIds?.FirstOrDefault(muid => muid == client.CurrentUser.Id) != null) if (dMessage.MentionedUserIds?.FirstOrDefault(muid => muid == client.CurrentUser.Id) != null)
{ {
m.MentionsMe = true; m.MentionsMe = true;
@ -208,9 +209,26 @@ public class DiscordInterface
} }
m.Reply = (t) => { return dMessage.ReplyAsync(t); }; m.Reply = (t) => { return dMessage.ReplyAsync(t); };
m.React = (e) => { return dMessage.AddReactionAsync(Emote.Parse(e)); }; m.React = (e) => { return attemptReact(dMessage, e); };
return m; return m;
} }
private Task attemptReact(IUserMessage msg, string e)
{
var c = _db.Channels.FirstOrDefault(c => c.ExternalId == msg.Channel.Id);
//var preferredEmote = c.EmoteOverrides?[e] ?? e; //TODO: emote overrides
var preferredEmote = e;
Emote emote;
if(!Emote.TryParse(preferredEmote, out emote))
{
if(preferredEmote == e)
Console.Error.WriteLine($"never heard of emote {e}");
return null;
}
return msg.AddReactionAsync(emote);
}
internal Channel UpsertChannel(IMessageChannel channel) internal Channel UpsertChannel(IMessageChannel channel)
{ {
var addPlease = false; var addPlease = false;
@ -276,14 +294,14 @@ public class DiscordInterface
c.SendFile = (f, t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; send file"); }; c.SendFile = (f, t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; send file"); };
return c; return c;
} }
internal User UpsertUser(IUser user) internal Account UpsertUser(IUser user)
{ {
var addPlease = false; var addPlease = false;
var u = _db.Users.FirstOrDefault(ui => ui.ExternalId == user.Id); var u = _db.Users.FirstOrDefault(ui => ui.ExternalId == user.Id);
if (u == null) if (u == null)
{ {
addPlease = true; addPlease = true;
u = new User(); u = new Account();
} }
u.Username = user.Username; u.Username = user.Username;
u.ExternalId = user.Id; u.ExternalId = user.Id;

View File

@ -23,6 +23,7 @@ namespace vassago.DiscordInterface
}; };
public static async Task Register(DiscordSocketClient client) public static async Task Register(DiscordSocketClient client)
{ {
return;
var commandsInContext = await client.GetGlobalApplicationCommandsAsync(); var commandsInContext = await client.GetGlobalApplicationCommandsAsync();
await Register(client, commandsInContext, null); await Register(client, commandsInContext, null);
foreach (var guild in client.Guilds) foreach (var guild in client.Guilds)

View File

@ -0,0 +1,263 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using vassago.Models;
#nullable disable
namespace vassago.Migrations
{
[DbContext(typeof(ChattingContext))]
[Migration("20230619155657_DistinguishUsersAndAccounts")]
partial class DistinguishUsersAndAccounts
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.5")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("vassago.Models.Account", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("DisplayName")
.HasColumnType("text");
b.Property<decimal?>("ExternalId")
.HasColumnType("numeric(20,0)");
b.Property<bool>("IsBot")
.HasColumnType("boolean");
b.Property<int[]>("PermissionTags")
.HasColumnType("integer[]");
b.Property<string>("Protocol")
.HasColumnType("text");
b.Property<Guid?>("SeenInChannelId")
.HasColumnType("uuid");
b.Property<string>("Username")
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("SeenInChannelId");
b.ToTable("Users");
});
modelBuilder.Entity("vassago.Models.Attachment", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<byte[]>("Content")
.HasColumnType("bytea");
b.Property<string>("ContentType")
.HasColumnType("text");
b.Property<string>("Description")
.HasColumnType("text");
b.Property<decimal?>("ExternalId")
.HasColumnType("numeric(20,0)");
b.Property<string>("Filename")
.HasColumnType("text");
b.Property<Guid?>("MessageId")
.HasColumnType("uuid");
b.Property<int>("Size")
.HasColumnType("integer");
b.Property<string>("Source")
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("MessageId");
b.ToTable("Attachments");
});
modelBuilder.Entity("vassago.Models.Channel", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("DisplayName")
.HasColumnType("text");
b.Property<decimal?>("ExternalId")
.HasColumnType("numeric(20,0)");
b.Property<bool>("IsDM")
.HasColumnType("boolean");
b.Property<Guid?>("ParentChannelId")
.HasColumnType("uuid");
b.Property<int?>("PermissionsId")
.HasColumnType("integer");
b.Property<string>("Protocol")
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("ParentChannelId");
b.HasIndex("PermissionsId");
b.ToTable("Channels");
});
modelBuilder.Entity("vassago.Models.Message", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<bool>("ActedOn")
.HasColumnType("boolean");
b.Property<Guid?>("AuthorId")
.HasColumnType("uuid");
b.Property<Guid?>("ChannelId")
.HasColumnType("uuid");
b.Property<string>("Content")
.HasColumnType("text");
b.Property<decimal?>("ExternalId")
.HasColumnType("numeric(20,0)");
b.Property<bool>("MentionsMe")
.HasColumnType("boolean");
b.Property<DateTimeOffset>("Timestamp")
.HasColumnType("timestamp with time zone");
b.HasKey("Id");
b.HasIndex("AuthorId");
b.HasIndex("ChannelId");
b.ToTable("Messages");
});
modelBuilder.Entity("vassago.Models.PermissionSettings", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int?>("LewdnessFilterLevel")
.HasColumnType("integer");
b.Property<bool?>("LinksAllowed")
.HasColumnType("boolean");
b.Property<long?>("MaxAttachmentBytes")
.HasColumnType("bigint");
b.Property<long?>("MaxTextChars")
.HasColumnType("bigint");
b.Property<int?>("MeannessFilterLevel")
.HasColumnType("integer");
b.Property<bool?>("ReactionsPossible")
.HasColumnType("boolean");
b.HasKey("Id");
b.ToTable("PermissionSettings");
});
modelBuilder.Entity("vassago.Models.Account", b =>
{
b.HasOne("vassago.Models.Channel", "SeenInChannel")
.WithMany("Users")
.HasForeignKey("SeenInChannelId");
b.Navigation("SeenInChannel");
});
modelBuilder.Entity("vassago.Models.Attachment", b =>
{
b.HasOne("vassago.Models.Message", "Message")
.WithMany("Attachments")
.HasForeignKey("MessageId");
b.Navigation("Message");
});
modelBuilder.Entity("vassago.Models.Channel", b =>
{
b.HasOne("vassago.Models.Channel", "ParentChannel")
.WithMany("SubChannels")
.HasForeignKey("ParentChannelId");
b.HasOne("vassago.Models.PermissionSettings", "Permissions")
.WithMany()
.HasForeignKey("PermissionsId");
b.Navigation("ParentChannel");
b.Navigation("Permissions");
});
modelBuilder.Entity("vassago.Models.Message", b =>
{
b.HasOne("vassago.Models.Account", "Author")
.WithMany()
.HasForeignKey("AuthorId");
b.HasOne("vassago.Models.Channel", "Channel")
.WithMany("Messages")
.HasForeignKey("ChannelId");
b.Navigation("Author");
b.Navigation("Channel");
});
modelBuilder.Entity("vassago.Models.Channel", b =>
{
b.Navigation("Messages");
b.Navigation("SubChannels");
b.Navigation("Users");
});
modelBuilder.Entity("vassago.Models.Message", b =>
{
b.Navigation("Attachments");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,38 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace vassago.Migrations
{
/// <inheritdoc />
public partial class DistinguishUsersAndAccounts : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "DisplayName",
table: "Users",
type: "text",
nullable: true);
migrationBuilder.AddColumn<int[]>(
name: "PermissionTags",
table: "Users",
type: "integer[]",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "DisplayName",
table: "Users");
migrationBuilder.DropColumn(
name: "PermissionTags",
table: "Users");
}
}
}

View File

@ -22,6 +22,40 @@ namespace vassago.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("vassago.Models.Account", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("DisplayName")
.HasColumnType("text");
b.Property<decimal?>("ExternalId")
.HasColumnType("numeric(20,0)");
b.Property<bool>("IsBot")
.HasColumnType("boolean");
b.Property<int[]>("PermissionTags")
.HasColumnType("integer[]");
b.Property<string>("Protocol")
.HasColumnType("text");
b.Property<Guid?>("SeenInChannelId")
.HasColumnType("uuid");
b.Property<string>("Username")
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("SeenInChannelId");
b.ToTable("Users");
});
modelBuilder.Entity("vassago.Models.Attachment", b => modelBuilder.Entity("vassago.Models.Attachment", b =>
{ {
b.Property<Guid>("Id") b.Property<Guid>("Id")
@ -159,32 +193,13 @@ namespace vassago.Migrations
b.ToTable("PermissionSettings"); b.ToTable("PermissionSettings");
}); });
modelBuilder.Entity("vassago.Models.User", b => modelBuilder.Entity("vassago.Models.Account", b =>
{ {
b.Property<Guid>("Id") b.HasOne("vassago.Models.Channel", "SeenInChannel")
.ValueGeneratedOnAdd() .WithMany("Users")
.HasColumnType("uuid"); .HasForeignKey("SeenInChannelId");
b.Property<decimal?>("ExternalId") b.Navigation("SeenInChannel");
.HasColumnType("numeric(20,0)");
b.Property<bool>("IsBot")
.HasColumnType("boolean");
b.Property<string>("Protocol")
.HasColumnType("text");
b.Property<Guid?>("SeenInChannelId")
.HasColumnType("uuid");
b.Property<string>("Username")
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("SeenInChannelId");
b.ToTable("Users");
}); });
modelBuilder.Entity("vassago.Models.Attachment", b => modelBuilder.Entity("vassago.Models.Attachment", b =>
@ -213,7 +228,7 @@ namespace vassago.Migrations
modelBuilder.Entity("vassago.Models.Message", b => modelBuilder.Entity("vassago.Models.Message", b =>
{ {
b.HasOne("vassago.Models.User", "Author") b.HasOne("vassago.Models.Account", "Author")
.WithMany() .WithMany()
.HasForeignKey("AuthorId"); .HasForeignKey("AuthorId");
@ -226,20 +241,13 @@ namespace vassago.Migrations
b.Navigation("Channel"); b.Navigation("Channel");
}); });
modelBuilder.Entity("vassago.Models.User", b =>
{
b.HasOne("vassago.Models.Channel", "SeenInChannel")
.WithMany()
.HasForeignKey("SeenInChannelId");
b.Navigation("SeenInChannel");
});
modelBuilder.Entity("vassago.Models.Channel", b => modelBuilder.Entity("vassago.Models.Channel", b =>
{ {
b.Navigation("Messages"); b.Navigation("Messages");
b.Navigation("SubChannels"); b.Navigation("SubChannels");
b.Navigation("Users");
}); });
modelBuilder.Entity("vassago.Models.Message", b => modelBuilder.Entity("vassago.Models.Message", b =>

31
Models/Account.cs Normal file
View File

@ -0,0 +1,31 @@
namespace vassago.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
public class Account
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public ulong? ExternalId { get; set; }
public string Username { get; set; }
private string _displayName = null;
public string DisplayName //TODO: fill
{
get
{
return _displayName ?? Username;
}
set
{
_displayName = value;
}
}
public bool IsBot { get; set; } //webhook counts
public Channel SeenInChannel { get; set; }
//permissions are per account-in-channel, and always propagate down. and since protocol will be a channel, I'll set the "is adam" permission on myself 1x/protocol.
public List<Enumerations.WellknownPermissions> PermissionTags{get;set;}
public string Protocol { get; set; }
}

View File

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

View File

@ -10,7 +10,7 @@ public class ChattingContext : DbContext
//public DbSet<Emoji> Emoji {get;set;} //public DbSet<Emoji> Emoji {get;set;}
public DbSet<Message> Messages { get; set; } public DbSet<Message> Messages { get; set; }
public DbSet<PermissionSettings> PermissionSettings{get;set;} public DbSet<PermissionSettings> PermissionSettings{get;set;}
public DbSet<User> Users { get; set; } public DbSet<Account> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseNpgsql(Shared.DBConnectionString) => optionsBuilder.UseNpgsql(Shared.DBConnectionString)

View File

@ -1,33 +1,58 @@
using System;
using System.ComponentModel;
using System.Reflection;
namespace vassago.Models; namespace vassago.Models;
public static class Enumerations public static class Enumerations
{ {
public static string LewdnessFilterLevel(int level) public enum LewdnessFilterLevel
{ {
switch (level) [Description("this is a christian minecraft server 🙏")]
{ Strict,
case 0: [Description("G-Rated")]
return "this is a christian minecraft server 🙏"; G,
case 1: [Description("polite company")]
return "G-Rated"; Moderate,
case 2: [Description(";) ;) ;)")]
return "polite company"; unrestricted
case 3:
return ";) ;) ;)";
default:
return "ERROR";
}
} }
public static string MeannessFilterLevel(int level) public enum MeannessFilterLevel
{ {
switch (level) [Description("good vibes only")]
Strict,
[Description("387.44 million miles of printed circuits, etc")]
Unrestricted
}
public enum WellknownPermissions
{
Master, //e.g., me. not that I think this would ever be released?
ChannelModerator,
}
public static string GetDescription<T>(this T enumerationValue)
where T : struct
{
Type type = enumerationValue.GetType();
if (!type.IsEnum)
{ {
case 0: throw new ArgumentException("EnumerationValue must be of Enum type", "enumerationValue");
return "good vibes only";
case 1:
return "387.44 million miles of printed circuits, etc";
default:
return "ERROR";
} }
//Tries to find a DescriptionAttribute for a potential friendly name
//for the enum
MemberInfo[] memberInfo = type.GetMember(enumerationValue.ToString());
if (memberInfo != null && memberInfo.Length > 0)
{
object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
//Pull out the description value
return ((DescriptionAttribute)attrs[0]).Description;
}
}
//If we have no description attribute, just return the ToString of the enum
return enumerationValue.ToString();
} }
} }

View File

@ -21,7 +21,7 @@ public class Message
public DateTimeOffset Timestamp { get; set; } public DateTimeOffset Timestamp { get; set; }
public bool ActedOn { get; set; } public bool ActedOn { get; set; }
public List<Attachment> Attachments { get; set; } public List<Attachment> Attachments { get; set; }
public User Author { get; set; } public Account Author { get; set; }
public Channel Channel { get; set; } public Channel Channel { get; set; }

View File

@ -5,13 +5,9 @@ using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection; using System.Reflection;
public class User //TODO: distinguish the user and their account public class User
{ {
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; } public Guid Id { get; set; }
public ulong? ExternalId { get; set; } public List<Account> Accounts { get; set; }
public string Username { get; set; } //TODO: display names. many protocols support this feature.
public bool IsBot { get; set; } //webhook counts
public Channel SeenInChannel { get; set; }
public string Protocol { get; set; }
} }