コード例 #1
0
ファイル: MessageCreate.cs プロジェクト: k-boyle/Disqord
        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)));
        }
コード例 #2
0
        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))));
        }
コード例 #3
0
        public Task HandleInviteCreateAsync(PayloadModel payload)
        {
            var           model = Serializer.ToObject <InviteCreateModel>(payload.D);
            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)));
        }