public Task HandleMessageCreateAsync(PayloadModel payload) { var model = payload.D.ToType <MessageModel>(); ICachedMessageChannel channel; CachedUser author; CachedGuild guild = null; var isWebhook = model.WebhookId.HasValue; if (model.GuildId != null) { guild = GetGuild(model.GuildId.Value); channel = guild.GetTextChannel(model.ChannelId); } else { channel = GetPrivateChannel(model.ChannelId); } if (isWebhook) { author = new CachedUnknownUser(_client, model.Author.Value); } else { if (model.GuildId != null) { author = model.Author.HasValue && model.Member.HasValue ? GetOrAddMember(guild, model.Member.Value, model.Author.Value) : guild.GetMember(model.Author.Value.Id); } else { author = GetUser(model.Author.Value.Id); } } if (author == null && !isWebhook) { Log(LogSeverity.Warning, $"Uncached author and/or guild == null in MESSAGE_CREATE.\n{payload.D}"); return(Task.CompletedTask); } var message = CachedMessage.Create(channel, author, model); if (message is CachedUserMessage userMessage) { _messageCache?.TryAddMessage(userMessage); } if (guild != null) { (channel as CachedTextChannel).LastMessageId = message.Id; } else { (channel as CachedPrivateChannel).LastMessageId = message.Id; } return(_client._messageReceived.InvokeAsync(new MessageReceivedEventArgs(message))); }
public Task HandleTypingStartedAsync(PayloadModel payload) { var model = payload.D.ToType <TypingStartModel>(); CachedGuild guild = null; ICachedMessageChannel channel; CachedUser user; if (model.GuildId != null) { guild = GetGuild(model.GuildId.Value); channel = guild.GetTextChannel(model.ChannelId); user = guild.GetMember(model.UserId); } else { channel = GetPrivateChannel(model.ChannelId); user = GetUser(model.UserId); } var userOptional = FetchableSnowflakeOptional.Create <CachedUser, RestUser, IUser>( model.UserId, channel is CachedTextChannel textChannel ? textChannel.Guild.GetMember(model.UserId) ?? GetUser(model.UserId) : GetUser(model.UserId), RestFetchable.Create((this, model), async(tuple, options) => { var(@this, model) = tuple; return(model.GuildId != null ? await @this._client.GetMemberAsync(model.GuildId.Value, model.UserId, options).ConfigureAwait(false) ?? await @this._client.GetUserAsync(model.UserId, options).ConfigureAwait(false) : await @this._client.GetUserAsync(model.UserId, options).ConfigureAwait(false)); })); return(_client._typingStarted.InvokeAsync(new TypingStartedEventArgs(_client, new SnowflakeOptional <ICachedMessageChannel>(channel, model.ChannelId), userOptional, DateTimeOffset.FromUnixTimeSeconds(model.Timestamp)))); }
public Task HandleMessageUpdateAsync(PayloadModel payload) { var model = payload.D.ToType <MessageModel>(); ICachedMessageChannel channel; CachedGuild guild = null; if (model.GuildId != null) { guild = GetGuild(model.GuildId.Value); channel = guild.GetTextChannel(model.ChannelId); } else { channel = GetPrivateChannel(model.ChannelId); } if (channel == null) { Log(LogSeverity.Warning, $"Uncached channel in MessageUpdated. Id: {model.ChannelId}"); return(Task.CompletedTask); } var message = channel.GetMessage(model.Id); var before = message?.Clone(); var isWebhook = model.WebhookId.HasValue; if (message == null) { CachedUser author = null; if (!model.Author.HasValue && !isWebhook) { Log(LogSeverity.Warning, "Unknown message and author has no value in MessageUpdated."); return(Task.CompletedTask); } else if (!isWebhook) { if (guild != null) { if (guild.Members.TryGetValue(model.Author.Value.Id, out var member)) { author = member; } else if (model.Member.HasValue) { author = GetOrAddMember(guild, model.Member.Value, model.Author.Value); } } else { author = GetUser(model.Author.Value.Id); } } else { // TODO? // (if isWebhook and no author) return(Task.CompletedTask); } if (author == null) { // TODO Log(LogSeverity.Error, "Author is still null in MessageUpdate."); return(Task.CompletedTask); } message = new CachedUserMessage(channel, author, model); } else { message.Update(model); } return(_client._messageUpdated.InvokeAsync(new MessageUpdatedEventArgs(channel, new SnowflakeOptional <CachedUserMessage>(before, model.Id), message))); }