Exemplo n.º 1
0
        internal CachedSystemMessage(ICachedMessageChannel channel, CachedUser author, MessageModel model) : base(channel, author, model)
        {
            Type       = (SystemMessageType)(model.Type - 1);
            RawContent = model.Content.GetValueOrDefault();

            Update(model);
        }
Exemplo n.º 2
0
 internal CachedMessage(ICachedMessageChannel channel, CachedUser author, MessageModel model) : base(channel.Client, model.Id)
 {
     Channel    = channel;
     Author     = author;
     _reactions = new LockedDictionary <IEmoji, ReactionData>(model.Reactions.HasValue
         ? model.Reactions.Value?.Length ?? 0
         : 0);
 }
Exemplo n.º 3
0
        internal CachedUserMessage(ICachedMessageChannel channel, CachedUser author, MessageModel model) : base(channel, author, model)
        {
            IsTextToSpeech = model.Tts.GetValueOrDefault();
            Nonce          = model.Nonce.GetValueOrDefault();
            WebhookId      = model.WebhookId.GetValueOrDefault();

            Update(model);
        }
Exemplo n.º 4
0
        public Task HandleInviteCreateAsync(PayloadModel payload)
        {
            var           model = payload.D.ToType <InviteCreateModel>();
            CachedGuild   guild = null;
            CachedChannel channel;
            CachedUser    inviter = null;

            if (model.GuildId != null)
            {
                guild   = GetGuild(model.GuildId.Value);
                channel = guild.GetChannel(model.ChannelId);

                if (model.Inviter.HasValue)
                {
                    inviter = guild.GetMember(model.Inviter.Value.Id);
                }
            }
            else
            {
                channel = GetPrivateChannel(model.ChannelId);

                if (model.Inviter.HasValue)
                {
                    inviter = GetUser(model.Inviter.Value.Id);
                }
            }

            if (inviter == null && model.Inviter.HasValue)
            {
                inviter = new CachedUnknownUser(_client, model.Inviter.Value);
            }

            return(_client._inviteCreated.InvokeAsync(new InviteCreatedEventArgs(
                                                          _client, guild, new SnowflakeOptional <CachedChannel>(channel, model.ChannelId),
                                                          inviter, model.Code, model.Temporary, model.MaxUses, model.MaxAge, model.CreatedAt)));
        }
Exemplo n.º 5
0
        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)));
        }
Exemplo n.º 6
0
 internal static CachedMessage Create(ICachedMessageChannel channel, CachedUser author, MessageModel model) => model.Type switch
 {
Exemplo n.º 7
0
 internal CachedDmChannel(DiscordClientBase client, ChannelModel model) : base(client, model)
 {
     Recipient = client.State.GetOrAddSharedUser(model.Recipients.Value[0]);
     Update(model);
 }