diff --git a/src/WebsocketHandler.cs b/src/WebsocketHandler.cs index 1339def..dc6a321 100644 --- a/src/WebsocketHandler.cs +++ b/src/WebsocketHandler.cs @@ -74,11 +74,15 @@ public void Close(bool skipMessage = false) { if (Config.SendApiConnectEvents && !skipMessage) { - SendSystemMessage( - text: Config.TEXT_ServerStop, - @event: ApiMessage.EventJoinLeave, - gateway: Config.generalGateway - ); + + Api.Event.EnqueueMainThreadTask(() => + { + SendSystemMessage( + text: Config.TEXT_ServerStop, + @event: ApiMessage.EventJoinLeave, + gateway: Config.generalGateway + ); + }, "vs-matterbridge"); } _reconnectWebsocket = false; @@ -97,7 +101,8 @@ public void SendSystemMessage(string text, string gateway, string @event = "") ); } - public void SendUserMessage(IServerPlayer player, string text, string gateway, bool generateAvatar, string @event = "") + public void SendUserMessage(IServerPlayer player, string text, string gateway, bool generateAvatar, + string @event = "") { SendMessage( username: player.PlayerName, @@ -109,7 +114,8 @@ public void SendUserMessage(IServerPlayer player, string text, string gateway, b ); } - public void SendUserMessage(string playerName, string playerUid, string text, string gateway, bool generateAvatar, string @event = "") + public void SendUserMessage(string playerName, string playerUid, string text, string gateway, + bool generateAvatar, string @event = "") { SendMessage( username: playerName, @@ -249,33 +255,48 @@ private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e .ToArray() ); + if (groupUid == null) + { + Mod.Logger.Error("groupId was null"); + return; + } + switch (message.Event) { case ApiMessage.EventJoinLeave: { - Api.SendMessageToGroup( - groupUid, - $"{message.Username}: {cleanedMessageText}", - EnumChatType.OthersMessage - ); + Api.Event.EnqueueMainThreadTask(() => + { + Api.SendMessageToGroup( + groupUid, + $"{message.Username}: {cleanedMessageText}", + EnumChatType.OthersMessage + ); + }, "vs-matterbridge"); break; } case ApiMessage.EventUserAction: { - Api.SendMessageToGroup( - groupUid, - $"{message.Username} {cleanedMessageText}", - EnumChatType.OthersMessage - ); + Api.Event.EnqueueMainThreadTask(() => + { + Api.SendMessageToGroup( + groupUid, + $"{message.Username} {cleanedMessageText}", + EnumChatType.OthersMessage + ); + }, "vs-matterbridge"); break; } case "": { - Api.SendMessageToGroup( - groupUid, - $"{message.Username}: {cleanedMessageText}", - EnumChatType.OthersMessage - ); + Api.Event.EnqueueMainThreadTask(() => + { + Api.SendMessageToGroup( + groupUid, + $"{message.Username}: {cleanedMessageText}", + EnumChatType.OthersMessage + ); + }, "vs-matterbridge"); break; } default: