/// <summary> /// Called when we have joined a new chat channel. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void OnJoinedChatChannel(object sender, JoinedChatChannelEventArgs e) { // Find an existing channel with the same name var channel = AvailableChatChannels.Find(x => x.Name == e.Channel); // If the channel doesn't actually exist in our available ones, that must mean the server is placing // us in one that we don't know about, so we'll have to create a new chat channel object. if (channel == null) { var newChannel = new ChatChannel { Name = e.Channel, Description = "No Description", AllowedUserGroups = UserGroups.Normal }; JoinedChatChannels.Add(newChannel); Logger.Important($"Joined ChatChannel: {e.Channel} which was previously unknown", LogType.Network); Dialog.ChatChannelList.InitializeChannel(newChannel); return; } if (JoinedChatChannels.All(x => x.Name != channel.Name)) { JoinedChatChannels.Add(channel); Dialog.ChatChannelList.InitializeChannel(channel); Logger.Important($"Joined chat channel: {e.Channel}", LogType.Network); } }
/// <summary> /// Called when the server reports that the client has left a chat channel. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void OnLeftChatChannel(object sender, LeftChatChannelEventArgs e) { var channel = JoinedChatChannels.Find(x => x.Name == e.ChannelName); // If the channel no longer exists in the joined chat channels, then don't do anything with it. if (channel == null) { return; } JoinedChatChannels.Remove(channel); // The previous selected button. var oldSelected = Dialog.ChatChannelList.SelectedButton; // The channel exists, so we have to kick them out in the overlay. var channelButton = Dialog.ChatChannelList.Buttons.Find(x => x.Channel == channel); // Make sure the channel is selected temporarily. if (channelButton != oldSelected) { channelButton.SelectChatChannel(); } // Leave the channel Dialog.CurrentTopic.CloseActiveChatChannel(); // Select the old channel that was originally there. if (channelButton != oldSelected) { oldSelected.SelectChatChannel(); } }
/// <summary> /// Sends a chat message to the server. /// </summary> public static void SendMessage(ChatChannel chan, ChatMessage message) { // Add the message to the chat channel. // Find the channel the message is for. var channel = JoinedChatChannels.Find(x => x.Name == chan.Name); // Make sure the sender is actual valid. message.Sender = OnlineManager.Self; message.Time = TimeHelper.GetUnixTimestampMilliseconds(); // Handle forward slash commands (client sided) and not send to the server. if (message.Message.StartsWith("/")) { HandleClientSideCommands(message); return; } // Check if the sender is muted. if (OnlineManager.Self.IsMuted) { QuaverBot.SendMutedMessage(); return; } // Add the message to the appropriate channel. channel.Messages.Add(message); // Add the message to the container. Dialog.ChannelMessageContainers[channel].AddMessage(channel, message); // Send the message to the server. OnlineManager.Client.SendMessage(chan.Name, message.Message); // If the channel is private, check if the message receiver is muted. if (!chan.IsPrivate) { return; } // Get the user if they're online. var receiver = OnlineManager.OnlineUsers.Values.ToList().Find(x => x.OnlineUser.Username == chan.Name); // Don't bother sending the message if they're not online. if (receiver == null) { QuaverBot.SendMessage(chan, "This user is not online."); return; } // If the user is muted, then let the user know this. if (receiver.IsMuted) { QuaverBot.SendMessage(chan, $"Oops! {receiver.OnlineUser.Username} is muted for another: {receiver.GetMuteTimeLeftString()}."); } }
/// <summary> /// Called whenever we receive new chat messages. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void OnChatMessageReceived(object sender, ChatMessageEventArgs e) { // Don't handle messages from non-online users. We should never get this since the packets are in order. if (!OnlineManager.OnlineUsers.ContainsKey(e.Message.SenderId)) { return; } e.Message.Sender = OnlineManager.OnlineUsers[e.Message.SenderId]; if (e.Message.Sender == OnlineManager.Self) { return; } // Determine if the channel is a private chat or not. var isPrivate = !e.Message.Channel.StartsWith("#"); // If the channel is private, then we want to use the sender's username, // otherwise we should use the channel's name. var channel = isPrivate ? JoinedChatChannels.Find(x => x.Name == e.Message.SenderName) : JoinedChatChannels.Find(x => x.Name == e.Message.Channel); // In the event that the chat channel doesn't already exist, we'll want to add a new one in. // (This is for private messages) if (channel == null) { channel = new ChatChannel { Name = e.Message.SenderName, Description = "Private Message" }; // Don't add non-private channels here. if (!channel.IsPrivate) { return; } JoinedChatChannels.Add(channel); Dialog.ChatChannelList.InitializeChannel(channel, false); Logger.Important($"Added ChatChannel: {channel.Name}, as we have received a message and it did not exist", LogType.Network); } // Add the message to the appropriate channel. channel.Messages.Add(e.Message); // Add the message to the container. Dialog.ChannelMessageContainers[channel].AddMessage(channel, e.Message); // Determine if the channel is unread. foreach (var channelButton in Dialog.ChatChannelList.Buttons) { if (channelButton.Channel == channel && Dialog.ActiveChannel != channel) { channelButton.IsUnread = true; } } // Private message notification. if (channel.IsPrivate) { var avatar = e.Message.SenderName == OnlineManager.Self.OnlineUser.Username ? SteamManager.UserAvatars[SteamUser.GetSteamID().m_SteamID] : UserInterface.UnknownAvatar; // Only show notification if the chat window isn't open. if (!IsActive) { // Play sound when reciving private message. // TODO: Add better sound for this. SkinManager.Skin.SoundClick.CreateChannel()?.Play(); NotificationManager.Show(avatar, Colors.Swan, $"{e.Message.SenderName} has sent you a message. Click here to read it.", (o, args) => { while (!IsActive) { ToggleChatOverlay(true); } Dialog.ChatChannelList.Buttons.Find(x => x.Channel == channel)?.SelectChatChannel(); }); } } }