forked from adam/discord-bot-shtik
Compare commits
1 Commits
i-fucking-
...
master
Author | SHA1 | Date | |
---|---|---|---|
6298b037b6 |
42
Behaver.cs
42
Behaver.cs
@ -72,31 +72,55 @@ public class Behaver
|
||||
|
||||
public void MarkSelf(Account selfAccount)
|
||||
{
|
||||
var db = new ChattingContext();
|
||||
if(SelfUser == null)
|
||||
{
|
||||
SelfUser = selfAccount.IsUser;
|
||||
}
|
||||
else if (SelfUser != selfAccount.IsUser)
|
||||
{
|
||||
CollapseUsers(SelfUser, selfAccount.IsUser);
|
||||
CollapseUsers(SelfUser, selfAccount.IsUser, db);
|
||||
}
|
||||
SelfAccounts = Rememberer.SearchAccounts(a => a.IsUser == SelfUser);
|
||||
Rememberer.RememberAccount(selfAccount);
|
||||
SelfAccounts = db.Accounts.Where(a => a.IsUser == SelfUser).ToList();
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
public bool CollapseUsers(User primary, User secondary)
|
||||
public bool CollapseUsers(User primary, User secondary, ChattingContext db)
|
||||
{
|
||||
if(primary.Accounts == null)
|
||||
primary.Accounts = new List<Account>();
|
||||
if(secondary.Accounts != null)
|
||||
Console.WriteLine($"{secondary.Id} is being consumed into {primary.Id}");
|
||||
primary.Accounts.AddRange(secondary.Accounts);
|
||||
foreach(var a in secondary.Accounts)
|
||||
{
|
||||
a.IsUser = primary;
|
||||
}
|
||||
secondary.Accounts.Clear();
|
||||
Rememberer.ForgetUser(secondary);
|
||||
Rememberer.RememberUser(primary);
|
||||
Console.WriteLine("accounts transferred");
|
||||
try
|
||||
{
|
||||
db.SaveChangesAsync().Wait();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Console.WriteLine("First save exception.");
|
||||
Console.Error.WriteLine(e);
|
||||
return false;
|
||||
}
|
||||
Console.WriteLine("saved");
|
||||
|
||||
|
||||
db.Users.Remove(secondary);
|
||||
Console.WriteLine("old account cleaned up");
|
||||
try
|
||||
{
|
||||
db.SaveChangesAsync().Wait();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Console.WriteLine("Second save exception.");
|
||||
Console.Error.WriteLine(e);
|
||||
return false;
|
||||
}
|
||||
Console.WriteLine("saved, again, separately");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ public class LinkClose : Behavior
|
||||
return true;
|
||||
}
|
||||
|
||||
if(Behaver.Instance.CollapseUsers(_primary.IsUser, secondary))
|
||||
if(Behaver.Instance.CollapseUsers(_primary.IsUser, secondary, new ChattingContext()))
|
||||
{
|
||||
await message.Channel.SendMessage("done :)");
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ namespace vassago
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var dbc = new ChattingContext();
|
||||
await dbc.Database.MigrateAsync();
|
||||
await dbc.Database.MigrateAsync(cancellationToken);
|
||||
|
||||
if (DiscordTokens?.Any() ?? false)
|
||||
foreach (var dt in DiscordTokens)
|
||||
@ -39,6 +39,7 @@ namespace vassago
|
||||
await t.Init(tc);
|
||||
ProtocolInterfaces.ProtocolList.twitchs.Add(t);
|
||||
}
|
||||
Console.WriteLine("survived initting");
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
|
@ -20,9 +20,15 @@ public class DiscordInterface
|
||||
internal const string PROTOCOL = "discord";
|
||||
internal DiscordSocketClient client;
|
||||
private bool eventsSignedUp = false;
|
||||
private ChattingContext _db;
|
||||
private static SemaphoreSlim discordChannelSetup = new SemaphoreSlim(1, 1);
|
||||
private Channel protocolAsChannel;
|
||||
|
||||
public DiscordInterface()
|
||||
{
|
||||
_db = new ChattingContext();
|
||||
}
|
||||
|
||||
public async Task Init(string token)
|
||||
{
|
||||
await SetupDiscordChannel();
|
||||
@ -46,7 +52,7 @@ public class DiscordInterface
|
||||
|
||||
try
|
||||
{
|
||||
protocolAsChannel = Rememberer.SearchChannel(c => c.ParentChannel == null && c.Protocol == PROTOCOL);
|
||||
protocolAsChannel = _db.Channels.FirstOrDefault(c => c.ParentChannel == null && c.Protocol == PROTOCOL);
|
||||
if (protocolAsChannel == null)
|
||||
{
|
||||
protocolAsChannel = new Channel()
|
||||
@ -62,15 +68,11 @@ public class DiscordInterface
|
||||
Protocol = PROTOCOL,
|
||||
SubChannels = new List<Channel>()
|
||||
};
|
||||
protocolAsChannel.SendMessage = (t) => { throw new InvalidOperationException($"discord itself cannot accept text"); };
|
||||
protocolAsChannel.SendFile = (f, t) => { throw new InvalidOperationException($"discord itself cannot send file"); };
|
||||
_db.Channels.Add(protocolAsChannel);
|
||||
_db.SaveChanges();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"discord, channel with id {protocolAsChannel.Id}, already exists");
|
||||
}
|
||||
protocolAsChannel.DisplayName = "discord (itself)";
|
||||
protocolAsChannel.SendMessage = (t) => { throw new InvalidOperationException($"protocol isn't a real channel, cannot accept text"); };
|
||||
protocolAsChannel.SendFile = (f, t) => { throw new InvalidOperationException($"protocol isn't a real channel, cannot send file"); };
|
||||
Rememberer.RememberChannel(protocolAsChannel);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@ -114,6 +116,8 @@ public class DiscordInterface
|
||||
{
|
||||
var selfAccount = UpsertAccount(client.CurrentUser, protocolAsChannel);
|
||||
selfAccount.DisplayName = client.CurrentUser.Username;
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
Behaver.Instance.MarkSelf(selfAccount);
|
||||
}
|
||||
|
||||
@ -136,6 +140,8 @@ public class DiscordInterface
|
||||
}
|
||||
await Behaver.Instance.ActOn(m);
|
||||
m.ActedOn = true; // for its own ruposess it might act on it later, but either way, fuck it, we checked.
|
||||
|
||||
_db.SaveChanges();
|
||||
}
|
||||
|
||||
private void UserJoined(SocketGuildUser arg)
|
||||
@ -183,26 +189,28 @@ public class DiscordInterface
|
||||
}
|
||||
internal vassago.Models.Attachment UpsertAttachment(IAttachment dAttachment)
|
||||
{
|
||||
var a = Rememberer.SearchAttachment(ai => ai.ExternalId == dAttachment.Id);
|
||||
var a = _db.Attachments.FirstOrDefault(ai => ai.ExternalId == dAttachment.Id);
|
||||
if (a == null)
|
||||
{
|
||||
a = new vassago.Models.Attachment();
|
||||
_db.Attachments.Add(a);
|
||||
}
|
||||
a.ContentType = dAttachment.ContentType;
|
||||
a.Description = dAttachment.Description;
|
||||
a.Filename = dAttachment.Filename;
|
||||
a.Size = dAttachment.Size;
|
||||
a.Source = new Uri(dAttachment.Url);
|
||||
Rememberer.RememberAttachment(a);
|
||||
|
||||
return a;
|
||||
}
|
||||
internal Message UpsertMessage(IUserMessage dMessage)
|
||||
{
|
||||
var m = Rememberer.SearchMessage(mi => mi.ExternalId == dMessage.Id.ToString() && mi.Protocol == PROTOCOL);
|
||||
var m = _db.Messages.FirstOrDefault(mi => mi.ExternalId == dMessage.Id.ToString() && mi.Protocol == PROTOCOL);
|
||||
if (m == null)
|
||||
{
|
||||
m = new Message();
|
||||
m.Protocol = PROTOCOL;
|
||||
_db.Messages.Add(m);
|
||||
}
|
||||
m.Attachments = m.Attachments ?? new List<vassago.Models.Attachment>();
|
||||
if (dMessage.Attachments?.Any() == true)
|
||||
@ -218,7 +226,7 @@ public class DiscordInterface
|
||||
m.Timestamp = dMessage.EditedTimestamp ?? dMessage.CreatedAt;
|
||||
m.Channel = UpsertChannel(dMessage.Channel);
|
||||
m.Author = UpsertAccount(dMessage.Author, m.Channel);
|
||||
if (dMessage.Channel is IGuildChannel)
|
||||
if(dMessage.Channel is IGuildChannel)
|
||||
{
|
||||
m.Author.DisplayName = (dMessage.Author as IGuildUser).DisplayName;//discord forgot how display names work.
|
||||
}
|
||||
@ -227,16 +235,15 @@ public class DiscordInterface
|
||||
|
||||
m.Reply = (t) => { return dMessage.ReplyAsync(t); };
|
||||
m.React = (e) => { return attemptReact(dMessage, e); };
|
||||
Rememberer.RememberChannel(m.Channel);
|
||||
return m;
|
||||
}
|
||||
internal Channel UpsertChannel(IMessageChannel channel)
|
||||
{
|
||||
Channel c = Rememberer.SearchChannel(ci => ci.ExternalId == channel.Id.ToString() && ci.Protocol == PROTOCOL);
|
||||
Channel c = _db.Channels.FirstOrDefault(ci => ci.ExternalId == channel.Id.ToString() && ci.Protocol == PROTOCOL);
|
||||
if (c == null)
|
||||
{
|
||||
c = new Channel();
|
||||
Console.WriteLine($"adding channel {channel.Name}");
|
||||
_db.Channels.Add(c);
|
||||
}
|
||||
|
||||
c.DisplayName = channel.Name;
|
||||
@ -246,7 +253,8 @@ public class DiscordInterface
|
||||
c.Protocol = PROTOCOL;
|
||||
if (channel is IGuildChannel)
|
||||
{
|
||||
UpsertChannel((channel as IGuildChannel).Guild);
|
||||
c.ParentChannel = UpsertChannel((channel as IGuildChannel).Guild);
|
||||
c.ParentChannel.SubChannels.Add(c);
|
||||
}
|
||||
else if (channel is IPrivateChannel)
|
||||
{
|
||||
@ -257,51 +265,30 @@ public class DiscordInterface
|
||||
c.ParentChannel = protocolAsChannel;
|
||||
Console.Error.WriteLine($"trying to upsert channel {channel.Id}/{channel.Name}, but it's neither guildchannel nor private channel. shrug.jpg");
|
||||
}
|
||||
c.SubChannels = c.SubChannels ?? new List<Channel>();
|
||||
c.SendMessage = (t) => { return channel.SendMessageAsync(t); };
|
||||
c.SendFile = (f, t) => { return channel.SendFileAsync(f, t); };
|
||||
|
||||
switch (c.ChannelType)
|
||||
switch(c.ChannelType)
|
||||
{
|
||||
case vassago.Models.Enumerations.ChannelType.DM:
|
||||
c.DisplayName = "DM: " + (channel as IPrivateChannel).Recipients?.FirstOrDefault(u => u.Id != client.CurrentUser.Id).Username;
|
||||
break;
|
||||
}
|
||||
Rememberer.RememberChannel(c);
|
||||
|
||||
Channel parentChannel = null;
|
||||
if (channel is IGuildChannel)
|
||||
{
|
||||
parentChannel = Rememberer.SearchChannel(c => c.ExternalId == (channel as IGuildChannel).Guild.Id.ToString() && c.Protocol == PROTOCOL);
|
||||
|
||||
}
|
||||
else if (channel is IPrivateChannel)
|
||||
{
|
||||
parentChannel = protocolAsChannel;
|
||||
}
|
||||
else
|
||||
{
|
||||
parentChannel = protocolAsChannel;
|
||||
Console.Error.WriteLine($"trying to upsert channel {channel.Id}/{channel.Name}, but it's neither guildchannel nor private channel. shrug.jpg");
|
||||
}
|
||||
if (parentChannel.SubChannels == null)
|
||||
parentChannel.SubChannels = new List<Channel>();
|
||||
parentChannel.SubChannels.Add(c);
|
||||
Rememberer.RememberChannel(parentChannel);
|
||||
|
||||
c.SendMessage = (t) => { return channel.SendMessageAsync(t); };
|
||||
c.SendFile = (f, t) => { return channel.SendFileAsync(f, t); };
|
||||
return c;
|
||||
}
|
||||
internal Channel UpsertChannel(IGuild channel)
|
||||
{
|
||||
Channel c = Rememberer.SearchChannel(ci => ci.ExternalId == channel.Id.ToString() && ci.Protocol == PROTOCOL);
|
||||
Channel c = _db.Channels.FirstOrDefault(ci => ci.ExternalId == channel.Id.ToString() && ci.Protocol == PROTOCOL);
|
||||
if (c == null)
|
||||
{
|
||||
c = new Channel();
|
||||
Rememberer.RememberChannel(c);
|
||||
_db.Channels.Add(c);
|
||||
}
|
||||
|
||||
c.DisplayName = channel.Name;
|
||||
c.ExternalId = channel.Id.ToString();
|
||||
c.ChannelType = vassago.Models.Enumerations.ChannelType.OU;
|
||||
c.ChannelType = vassago.Models.Enumerations.ChannelType.Normal;
|
||||
c.Messages = c.Messages ?? new List<Message>();
|
||||
c.Protocol = protocolAsChannel.Protocol;
|
||||
c.ParentChannel = protocolAsChannel;
|
||||
@ -310,15 +297,15 @@ public class DiscordInterface
|
||||
|
||||
c.SendMessage = (t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; cannot accept text"); };
|
||||
c.SendFile = (f, t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; send file"); };
|
||||
Rememberer.RememberChannel(c);
|
||||
return c;
|
||||
}
|
||||
internal Account UpsertAccount(IUser user, Channel inChannel)
|
||||
{
|
||||
var acc = Rememberer.SearchAccount(ui => ui.ExternalId == user.Id.ToString() && ui.SeenInChannel.Id == inChannel.Id);
|
||||
var acc = _db.Accounts.FirstOrDefault(ui => ui.ExternalId == user.Id.ToString() && ui.SeenInChannel.Id == inChannel.Id);
|
||||
if (acc == null)
|
||||
{
|
||||
acc = new Account();
|
||||
_db.Accounts.Add(acc);
|
||||
}
|
||||
acc.Username = user.Username;
|
||||
acc.ExternalId = user.Id.ToString();
|
||||
@ -326,19 +313,18 @@ public class DiscordInterface
|
||||
acc.Protocol = PROTOCOL;
|
||||
acc.SeenInChannel = inChannel;
|
||||
|
||||
acc.IsUser = Rememberer.SearchUser(u => u.Accounts.Any(a => a.ExternalId == acc.ExternalId && a.Protocol == acc.Protocol));
|
||||
//db.Users.FirstOrDefault(u => u.Accounts.Any(a => a.ExternalId == acc.ExternalId && a.Protocol == acc.Protocol));
|
||||
if (acc.IsUser == null)
|
||||
acc.IsUser = _db.Users.FirstOrDefault(u => u.Accounts.Any(a => a.ExternalId == acc.ExternalId && a.Protocol == acc.Protocol));
|
||||
if(acc.IsUser == null)
|
||||
{
|
||||
acc.IsUser = new User() { Accounts = new List<Account>() { acc } };
|
||||
_db.Users.Add(acc.IsUser);
|
||||
}
|
||||
Rememberer.RememberAccount(acc);
|
||||
return acc;
|
||||
}
|
||||
|
||||
private Task attemptReact(IUserMessage msg, string e)
|
||||
{
|
||||
var c = Rememberer.SearchChannel(c => c.ExternalId == msg.Channel.Id.ToString());// db.Channels.FirstOrDefault(c => c.ExternalId == msg.Channel.Id.ToString());
|
||||
var c = _db.Channels.FirstOrDefault(c => c.ExternalId == msg.Channel.Id.ToString());
|
||||
//var preferredEmote = c.EmoteOverrides?[e] ?? e; //TODO: emote overrides
|
||||
var preferredEmote = e;
|
||||
if (Emoji.TryParse(preferredEmote, out Emoji emoji))
|
||||
|
@ -1,79 +0,0 @@
|
||||
namespace vassago;
|
||||
|
||||
using System.Linq.Expressions;
|
||||
using vassago.Models;
|
||||
|
||||
public static class Rememberer
|
||||
{
|
||||
public static Account SearchAccount(Expression<Func<Account, bool>> predicate)
|
||||
{
|
||||
return (new ChattingContext()).Accounts.FirstOrDefault(predicate);
|
||||
}
|
||||
public static List<Account> SearchAccounts(Expression<Func<Account, bool>> predicate)
|
||||
{
|
||||
return (new ChattingContext()).Accounts.Where(predicate).ToList();
|
||||
}
|
||||
public static Attachment SearchAttachment(Expression<Func<Attachment, bool>> predicate)
|
||||
{
|
||||
return (new ChattingContext()).Attachments.FirstOrDefault(predicate);
|
||||
}
|
||||
public static Channel SearchChannel(Expression<Func<Channel, bool>> predicate)
|
||||
{
|
||||
return (new ChattingContext()).Channels.FirstOrDefault(predicate);
|
||||
}
|
||||
public static Message SearchMessage(Expression<Func<Message, bool>> predicate)
|
||||
{
|
||||
return (new ChattingContext()).Messages.FirstOrDefault(predicate);
|
||||
}
|
||||
public static User SearchUser(Expression<Func<User, bool>> predicate)
|
||||
{
|
||||
return (new ChattingContext()).Users.FirstOrDefault(predicate);
|
||||
}
|
||||
public static void RememberAccount(Account toRemember)
|
||||
{
|
||||
var db = new ChattingContext();
|
||||
if (toRemember.Id == Guid.Empty)
|
||||
db.Accounts.Add(toRemember);
|
||||
|
||||
db.SaveChanges();
|
||||
}
|
||||
public static void RememberAttachment(Attachment toRemember)
|
||||
{
|
||||
var db = new ChattingContext();
|
||||
if (toRemember.Id == Guid.Empty)
|
||||
db.Attachments.Add(toRemember);
|
||||
|
||||
db.SaveChanges();
|
||||
}
|
||||
public static void RememberChannel(Channel toRemember)
|
||||
{
|
||||
var db = new ChattingContext();
|
||||
if (toRemember.Id == Guid.Empty)
|
||||
db.Channels.Add(toRemember);
|
||||
|
||||
db.SaveChanges();
|
||||
}
|
||||
public static void RememberMessage(Message toRemember)
|
||||
{
|
||||
var db = new ChattingContext();
|
||||
if (toRemember.Id == Guid.Empty)
|
||||
{
|
||||
db.Messages.Add(toRemember);
|
||||
}
|
||||
db.SaveChanges();
|
||||
}
|
||||
public static void RememberUser(User toRemember)
|
||||
{
|
||||
var db = new ChattingContext();
|
||||
if (toRemember.Id == Guid.Empty)
|
||||
db.Users.Add(toRemember);
|
||||
|
||||
db.SaveChanges();
|
||||
}
|
||||
public static void ForgetUser(User toForget)
|
||||
{
|
||||
var db = new ChattingContext();
|
||||
db.Users.Remove(toForget);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
@ -166,6 +166,7 @@ public class HomeController : Controller
|
||||
}
|
||||
private void serializeUser(ref StringBuilder sb, ref List<Account> allAccounts, User currentUser)
|
||||
{
|
||||
Console.WriteLine(currentUser);
|
||||
sb.Append($"{{\"text\": \"<a href=\\\"{Url.ActionLink(action: "Details", controller: "Users", values: new {id = currentUser.Id})}\\\">");
|
||||
sb.Append(currentUser.DisplayName);
|
||||
sb.Append("</a>\", ");
|
||||
|
Loading…
Reference in New Issue
Block a user