| @@ -858,45 +858,33 @@ namespace Discord | |||
| break; | |||
| case "MESSAGE_UPDATE": | |||
| { | |||
| if (Config.MessageCacheSize > 0) | |||
| var data = e.Payload.ToObject<MessageUpdateEvent>(_serializer); | |||
| var channel = GetChannel(data.ChannelId); | |||
| if (channel != null) | |||
| { | |||
| var data = e.Payload.ToObject<MessageUpdateEvent>(_serializer); | |||
| var channel = GetChannel(data.ChannelId); | |||
| if (channel != null) | |||
| { | |||
| var msg = channel.GetMessage(data.Id); | |||
| if (msg != null) | |||
| { | |||
| msg.Update(data); | |||
| if (Config.LogEvents) | |||
| Logger.Info($"Message Update: {channel.Server?.Name ?? "[Private]"}/{channel.Name}"); | |||
| OnMessageUpdated(msg); | |||
| } | |||
| } | |||
| else | |||
| Logger.Warning("MESSAGE_UPDATE referenced an unknown channel."); | |||
| var msg = channel.GetMessage(data.Id, data.Author.Id); | |||
| msg.Update(data); | |||
| if (Config.LogEvents) | |||
| Logger.Info($"Message Update: {channel.Server?.Name ?? "[Private]"}/{channel.Name}"); | |||
| OnMessageUpdated(msg); | |||
| } | |||
| else | |||
| Logger.Warning("MESSAGE_UPDATE referenced an unknown channel."); | |||
| } | |||
| break; | |||
| case "MESSAGE_DELETE": | |||
| { | |||
| if (Config.MessageCacheSize > 0) | |||
| var data = e.Payload.ToObject<MessageDeleteEvent>(_serializer); | |||
| var channel = GetChannel(data.ChannelId); | |||
| if (channel != null) | |||
| { | |||
| var data = e.Payload.ToObject<MessageDeleteEvent>(_serializer); | |||
| var channel = GetChannel(data.ChannelId); | |||
| if (channel != null) | |||
| { | |||
| var msg = channel.RemoveMessage(data.Id); | |||
| if (msg != null) | |||
| { | |||
| if (Config.LogEvents) | |||
| Logger.Info($"Message Deleted: {channel.Server?.Name ?? "[Private]"}/{channel.Name}"); | |||
| OnMessageDeleted(msg); | |||
| } | |||
| } | |||
| else | |||
| Logger.Warning("MESSAGE_DELETE referenced an unknown channel."); | |||
| var msg = channel.RemoveMessage(data.Id); | |||
| if (Config.LogEvents) | |||
| Logger.Info($"Message Deleted: {channel.Server?.Name ?? "[Private]"}/{channel.Name}"); | |||
| OnMessageDeleted(msg); | |||
| } | |||
| else | |||
| Logger.Warning("MESSAGE_DELETE referenced an unknown channel."); | |||
| } | |||
| break; | |||
| case "MESSAGE_ACK": | |||
| @@ -907,7 +895,7 @@ namespace Discord | |||
| var channel = GetChannel(data.ChannelId); | |||
| if (channel != null) | |||
| { | |||
| var msg = channel.GetMessage(data.MessageId); | |||
| var msg = channel.GetMessage(data.MessageId, null); | |||
| if (msg != null) | |||
| { | |||
| if (Config.LogEvents) | |||
| @@ -263,21 +263,25 @@ namespace Discord | |||
| if (Client.Config.MessageCacheSize > 0) | |||
| { | |||
| Message msg; | |||
| _messages.TryRemove(id, out msg); | |||
| return msg; | |||
| if (_messages.TryRemove(id, out msg)) | |||
| return msg; | |||
| } | |||
| return null; | |||
| return new Message(id, this, null); | |||
| } | |||
| public Message GetMessage(ulong id) | |||
| => GetMessage(id, null); | |||
| internal Message GetMessage(ulong id, ulong? userId) | |||
| { | |||
| if (Client.Config.MessageCacheSize <= 0) | |||
| throw new InvalidOperationException("Unable to retrieve a message when the message cache is disabled."); | |||
| Message result; | |||
| _messages.TryGetValue(id, out result); | |||
| return result; | |||
| if (Client.Config.MessageCacheSize > 0) | |||
| { | |||
| Message result; | |||
| if (_messages.TryGetValue(id, out result)) | |||
| return result; | |||
| } | |||
| return new Message(id, this, userId != null ? GetUser(userId.Value) : null); | |||
| } | |||
| public async Task<Message[]> DownloadMessages(int limit = 100, ulong? relativeMessageId = null, | |||
| RelativeDirection relativeDir = RelativeDirection.Before, bool useCache = true) | |||
| { | |||
| @@ -206,7 +206,7 @@ namespace Discord | |||
| /// <summary> Returns the server containing the channel this message was sent to. </summary> | |||
| public Server Server => Channel.Server; | |||
| /// <summary> Returns if this message was sent from the logged-in accounts. </summary> | |||
| public bool IsAuthor => User.Id == Client.CurrentUser?.Id; | |||
| public bool IsAuthor => User != null && User.Id == Client.CurrentUser?.Id; | |||
| internal Message(ulong id, Channel channel, User user) | |||
| { | |||
| @@ -279,7 +279,7 @@ namespace Discord | |||
| } | |||
| if (model.IsMentioningEveryone != null) | |||
| { | |||
| if (model.IsMentioningEveryone.Value && User.GetPermissions(channel).MentionEveryone) | |||
| if (model.IsMentioningEveryone.Value && User != null && User.GetPermissions(channel).MentionEveryone) | |||
| MentionedRoles = new Role[] { Server.EveryoneRole }; | |||
| else | |||
| MentionedRoles = new Role[0]; | |||
| @@ -360,6 +360,6 @@ namespace Discord | |||
| public override bool Equals(object obj) => obj is Message && (obj as Message).Id == Id; | |||
| public override int GetHashCode() => unchecked(Id.GetHashCode() + 9979); | |||
| public override string ToString() => $"{User}: {RawText}"; | |||
| public override string ToString() => $"{User?.Name ?? "Unknown User"}: {RawText}"; | |||
| } | |||
| } | |||