From c0d2203099424b46302b3355c8f87ca275f87ce3 Mon Sep 17 00:00:00 2001 From: Madpeterz <57852827+Madpeterz@users.noreply.github.com> Date: Tue, 5 Jan 2021 23:08:37 +0000 Subject: [PATCH] groupchat / im chat fixs and improvements to discord-v --- BSBshared/BotTypes/BasicBot.cs | 2 +- CLI/DiscordSupervisor/ChannelControl.cs | 24 +++++-- CLI/DiscordSupervisor/Discord-super.cs | 4 ++ CLI/DiscordSupervisor/DiscordBot.cs | 2 + CLI/DiscordSupervisor/DiscordEvents.cs | 35 ++++++++++ .../DiscordTTS/DiscordTTS.cs | 8 +++ CLI/DiscordSupervisor/Status.cs | 4 +- Core/bottypes/AnimationsBot.cs | 2 +- Core/bottypes/AwaitReplyEventBot.cs | 2 +- Core/bottypes/CommandsBot.cs | 2 +- Core/bottypes/EventsBot.cs | 67 ++++++++++++++++++- Core/bottypes/MessageSwitcherBot.cs | 2 +- Core/bottypes/athome.cs | 2 +- 13 files changed, 138 insertions(+), 18 deletions(-) diff --git a/BSBshared/BotTypes/BasicBot.cs b/BSBshared/BotTypes/BasicBot.cs index e9b08ba..8fd91d0 100644 --- a/BSBshared/BotTypes/BasicBot.cs +++ b/BSBshared/BotTypes/BasicBot.cs @@ -124,7 +124,7 @@ namespace BetterSecondBotShared.bottypes public bool GetAllowFunds { get { return myconfig.Setting_AllowFunds; } } public string LastStatusMessage { get; set; } - public bool KillMe { get { return killMe; } } + public virtual bool KillMe { get { return killMe; } } protected bool teleported; public void SetTeleported() diff --git a/CLI/DiscordSupervisor/ChannelControl.cs b/CLI/DiscordSupervisor/ChannelControl.cs index 28f06f8..9330e02 100644 --- a/CLI/DiscordSupervisor/ChannelControl.cs +++ b/CLI/DiscordSupervisor/ChannelControl.cs @@ -214,10 +214,17 @@ namespace BetterSecondBot.DiscordSupervisor IReadOnlyCollection found_cats = await DiscordServer.GetCategoriesAsync(CacheMode.AllowDownload); foreach (ICategoryChannel fcat in found_cats) { - if (required_cats.Contains(fcat.Name) == true) - { - required_cats.Remove(fcat.Name); - catmap.Add(fcat.Name, fcat); + if (catmap.ContainsKey(fcat.Name) == false) + { + if (required_cats.Contains(fcat.Name) == true) + { + required_cats.Remove(fcat.Name); + catmap.Add(fcat.Name, fcat); + } + } + else + { + required_cats.Remove(fcat.Name); } } foreach (string A in required_cats) @@ -236,9 +243,12 @@ namespace BetterSecondBot.DiscordSupervisor } else { - if (chan.CategoryId == catmap["group"].Id) - { - GroupChannels.Add(chan.Name); + if (GroupChannels.Contains(chan.Name) == false) + { + if (chan.CategoryId == catmap["group"].Id) + { + GroupChannels.Add(chan.Name); + } } } } diff --git a/CLI/DiscordSupervisor/Discord-super.cs b/CLI/DiscordSupervisor/Discord-super.cs index 60adcd5..cd91add 100644 --- a/CLI/DiscordSupervisor/Discord-super.cs +++ b/CLI/DiscordSupervisor/Discord-super.cs @@ -14,6 +14,10 @@ namespace BetterSecondBot.DiscordSupervisor { while (exit_super == false) { + if (HasBasicBot() == false) + { + StatusMessageHandler(null, new BSB.bottypes.StatusMessageEvent(false, "None")); + } string NewStatusMessage = GetStatus(); if (NewStatusMessage != LastStatusMessage) { diff --git a/CLI/DiscordSupervisor/DiscordBot.cs b/CLI/DiscordSupervisor/DiscordBot.cs index c531f68..bf17776 100644 --- a/CLI/DiscordSupervisor/DiscordBot.cs +++ b/CLI/DiscordSupervisor/DiscordBot.cs @@ -23,6 +23,8 @@ namespace BetterSecondBot.DiscordSupervisor protected virtual async Task DiscordClientReady() { + await DiscordClient.SetStatusAsync(Discord.UserStatus.Idle); + await DiscordClient.SetGameAsync("Not connected", null, ActivityType.CustomStatus); DiscordServer = DiscordClient.GetGuild(myconfig.DiscordFull_ServerID); if (DiscordServer != null) { diff --git a/CLI/DiscordSupervisor/DiscordEvents.cs b/CLI/DiscordSupervisor/DiscordEvents.cs index c6751b4..b8b36a7 100644 --- a/CLI/DiscordSupervisor/DiscordEvents.cs +++ b/CLI/DiscordSupervisor/DiscordEvents.cs @@ -7,14 +7,49 @@ namespace BetterSecondBot.DiscordSupervisor { public class DiscordEvents : DiscordBot { + protected StatusMessageEvent LastNetworkEventArgs = null; protected void attach_events() { controler.Bot.MessageEvent += BotChatControlerHandler; + controler.Bot.StatusMessageEvent += StatusMessageHandler; + controler.Bot.GroupsReadyEvent += GroupsReady; + } + + protected async void GroupsReady(object sender, GroupEventArgs e) + { + await DiscordRebuildChannels(); } protected void BotChatControlerHandler(object sender, MessageEventArgs e) { BotChatControler(e.message, e.sender_name, e.sender_uuid, e.avatar, e.group, e.group_uuid, e.localchat, e.fromme); } + + protected async void StatusMessageHandler(object sender, StatusMessageEvent e) + { + bool apply_update = true; + if(LastNetworkEventArgs != null) + { + if((LastNetworkEventArgs.sim == e.sim) && (LastNetworkEventArgs.connected == e.connected)) + { + apply_update = false; + } + } + if (apply_update == true) + { + LastNetworkEventArgs = e; + if (e.connected == true) + { + await DiscordClient.SetGameAsync("Sim:" + e.sim); + await DiscordClient.SetStatusAsync(Discord.UserStatus.Online); + + } + else + { + await DiscordClient.SetGameAsync("Logged out"); + await DiscordClient.SetStatusAsync(Discord.UserStatus.Idle); + } + } + } } } diff --git a/CLI/DiscordSupervisor/DiscordTTS/DiscordTTS.cs b/CLI/DiscordSupervisor/DiscordTTS/DiscordTTS.cs index ff6e79e..626dab0 100644 --- a/CLI/DiscordSupervisor/DiscordTTS/DiscordTTS.cs +++ b/CLI/DiscordSupervisor/DiscordTTS/DiscordTTS.cs @@ -62,8 +62,16 @@ namespace BetterSecondBot.DiscordSupervisor await TTSchannel.SendMessageAsync(message, true); } } + else + { + await base.DiscordIMMessage(sender_uuid, sender_name, message); + } } } + else + { + await base.DiscordIMMessage(sender_uuid, sender_name, message); + } } } } diff --git a/CLI/DiscordSupervisor/Status.cs b/CLI/DiscordSupervisor/Status.cs index be0652c..72d965e 100644 --- a/CLI/DiscordSupervisor/Status.cs +++ b/CLI/DiscordSupervisor/Status.cs @@ -1,5 +1,6 @@ using OpenMetaverse; - +using System; + namespace BetterSecondBot.DiscordSupervisor { public abstract class DiscordStatus : DiscordChannelControl @@ -67,6 +68,5 @@ namespace BetterSecondBot.DiscordSupervisor } return reply; } - } } diff --git a/Core/bottypes/AnimationsBot.cs b/Core/bottypes/AnimationsBot.cs index a0405b6..3a35b63 100644 --- a/Core/bottypes/AnimationsBot.cs +++ b/Core/bottypes/AnimationsBot.cs @@ -11,11 +11,11 @@ namespace BSB.bottypes protected override void AfterBotLoginHandler() { - base.AfterBotLoginHandler(); if (reconnect == false) { Client.Avatars.AvatarAnimation += AvatarAnimationHandler; } + base.AfterBotLoginHandler(); } public override void ResetAnimations() diff --git a/Core/bottypes/AwaitReplyEventBot.cs b/Core/bottypes/AwaitReplyEventBot.cs index ae68957..e22a4f1 100644 --- a/Core/bottypes/AwaitReplyEventBot.cs +++ b/Core/bottypes/AwaitReplyEventBot.cs @@ -12,7 +12,6 @@ namespace BSB.bottypes protected override void AfterBotLoginHandler() { - base.AfterBotLoginHandler(); if (reconnect == false) { Client.Groups.GroupRoleDataReply += GroupRolesData; @@ -20,6 +19,7 @@ namespace BSB.bottypes Client.Groups.GroupMembersReply += GroupMembersReplyHandler; Client.Parcels.ParcelObjectOwnersReply += ObjectOwnersHandler; } + base.AfterBotLoginHandler(); } protected override void GroupMembersReplyHandler(object sender, GroupMembersReplyEventArgs e) { diff --git a/Core/bottypes/CommandsBot.cs b/Core/bottypes/CommandsBot.cs index b4a34aa..9581bd4 100644 --- a/Core/bottypes/CommandsBot.cs +++ b/Core/bottypes/CommandsBot.cs @@ -191,12 +191,12 @@ namespace BSB.bottypes protected override void AfterBotLoginHandler() { - base.AfterBotLoginHandler(); if (reconnect == false) { CommandsInterface = new Commands.CoreCommandsInterface(this); } Client.Self.RequestBalance(); + base.AfterBotLoginHandler(); } protected override void CoreCommandLib(UUID fromUUID, bool from_accepted_avatar, string command, string arg, string signing_code, string signed_with) diff --git a/Core/bottypes/EventsBot.cs b/Core/bottypes/EventsBot.cs index 1b98023..c0456d0 100644 --- a/Core/bottypes/EventsBot.cs +++ b/Core/bottypes/EventsBot.cs @@ -45,12 +45,12 @@ namespace BSB.bottypes protected override void AfterBotLoginHandler() { - base.AfterBotLoginHandler(); if (reconnect == false) { Client.Network.RegisterCallback(PacketType.SetFollowCamProperties, SetFollowCamPropertiesPacketHandler); } delay_appearence_update = 0; // reset Appearance helper + base.AfterBotLoginHandler(); } protected void SetFollowCamPropertiesPacketHandler(object sender, PacketReceivedEventArgs e) @@ -60,6 +60,26 @@ namespace BSB.bottypes } + public class StatusMessageEvent : EventArgs + { + public bool connected { get; } + public string sim { get; } + + public StatusMessageEvent(bool connected,string sim) + { + this.connected = connected; + this.sim = sim; + } + } + public class GroupEventArgs : EventArgs + { + public bool ready { get; } + + public GroupEventArgs(bool ready) + { + this.ready = ready; + } + } public class MessageEventArgs : EventArgs { @@ -88,15 +108,28 @@ namespace BSB.bottypes public class EventsBot : VoidEventBot { private EventHandler _MessageEvent; + private EventHandler _StatusMessageEvent; + private EventHandler _GroupsEvent; private readonly object _MessageEventLock = new object(); + private readonly object _StatusMessageEventlock = new object(); + private readonly object _GroupsEventLock = new object(); - /// Raised when a request we sent to friend another agent is accepted or declined public event EventHandler MessageEvent { add { lock (_MessageEventLock) { _MessageEvent += value; } } remove { lock (_MessageEventLock) { _MessageEvent -= value; } } } + public event EventHandler StatusMessageEvent + { + add { lock (_StatusMessageEventlock) { _StatusMessageEvent += value; } } + remove { lock (_StatusMessageEventlock) { _StatusMessageEvent -= value; } } + } + public event EventHandler GroupsReadyEvent + { + add { lock (_GroupsEventLock) { _GroupsEvent += value; } } + remove { lock (_GroupsEventLock) { _GroupsEvent -= value; } } + } protected void On_MessageEvent(MessageEventArgs e) { @@ -104,6 +137,18 @@ namespace BSB.bottypes handler?.Invoke(this, e); } + protected void On_StatusMessageEvent(StatusMessageEvent e) + { + EventHandler handler = _StatusMessageEvent; + handler?.Invoke(this, e); + } + + protected void On_GroupsReadyEvent(GroupEventArgs e) + { + EventHandler handler = _GroupsEvent; + handler?.Invoke(this, e); + } + protected virtual void BotChatControler(string message, string sender_name, UUID sender_uuid, bool avatar, bool group, UUID group_uuid, bool localchat, bool fromme) { if (_MessageEvent != null) @@ -201,8 +246,22 @@ namespace BSB.bottypes } } } + if (Client.Network.CurrentSim != null) + { + On_StatusMessageEvent(new StatusMessageEvent(true, Client.Network.CurrentSim.Name)); + } + else + { + On_StatusMessageEvent(new StatusMessageEvent(true, "None")); + } return base.GetStatus(); } + public override void KillMePlease() + { + On_StatusMessageEvent(new StatusMessageEvent(false, "None")); + base.KillMePlease(); + } + protected string login_status = "Waiting to login"; protected override void LoginHandler(object o, LoginProgressEventArgs e) { @@ -252,11 +311,12 @@ namespace BSB.bottypes { Client.Self.RequestJoinGroupChat(entry.Value.ID); } + On_GroupsReadyEvent(new GroupEventArgs(true)); + } protected override void AfterBotLoginHandler() { - base.AfterBotLoginHandler(); if (reconnect == false) { Client.Self.ChatFromSimulator += ChatInputHandler; @@ -266,6 +326,7 @@ namespace BSB.bottypes Client.Friends.FriendshipResponse += FriendshipResponse; delay_group_fetch = helpers.UnixTimeNow() + 10; } + base.AfterBotLoginHandler(); } protected virtual void FriendshipResponse(object o, FriendshipResponseEventArgs E) diff --git a/Core/bottypes/MessageSwitcherBot.cs b/Core/bottypes/MessageSwitcherBot.cs index c542bf0..c97a3dd 100644 --- a/Core/bottypes/MessageSwitcherBot.cs +++ b/Core/bottypes/MessageSwitcherBot.cs @@ -27,8 +27,8 @@ namespace BSB.bottypes protected override void AfterBotLoginHandler() { - base.AfterBotLoginHandler(); connected_to_groups = false; + base.AfterBotLoginHandler(); } readonly string[] hard_blocked_agents = new string[] { "secondlife", "second life"}; diff --git a/Core/bottypes/athome.cs b/Core/bottypes/athome.cs index c9b849d..d8c5cfe 100644 --- a/Core/bottypes/athome.cs +++ b/Core/bottypes/athome.cs @@ -18,7 +18,7 @@ namespace BSB.bottypes protected override void AfterBotLoginHandler() { - base.AfterBotLoginHandler(); + base.AfterBotLoginHandler(); last_tested_home_id = -1; last_tp_attempt_unixtime = helpers.UnixTimeNow() + 30; if (after_login_fired == false)