コード例 #1
0
ファイル: ChannelHelper.cs プロジェクト: Neuheit/Discord.Net
        public static async Task <RestMessage> GetMessageAsync(IMessageChannel channel, BaseDiscordClient client,
                                                               ulong id, RequestOptions options)
        {
            var guildId = (channel as IGuildChannel)?.GuildId;
            var guild   = guildId != null?await(client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).ConfigureAwait(false) : null;

            var model = await client.ApiClient.GetChannelMessageAsync(channel.Id, id, options).ConfigureAwait(false);

            if (model == null)
            {
                return(null);
            }
            var author = MessageHelper.GetAuthor(client, guild, model.Author.Value, model.WebhookId.ToNullable());

            return(RestMessage.Create(client, channel, author, model));
        }
コード例 #2
0
ファイル: ChannelHelper.cs プロジェクト: Neuheit/Discord.Net
        public static async Task <IReadOnlyCollection <RestMessage> > GetPinnedMessagesAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                                             RequestOptions options)
        {
            var guildId = (channel as IGuildChannel)?.GuildId;
            var guild   = guildId != null?await(client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).ConfigureAwait(false) : null;

            var models = await client.ApiClient.GetPinsAsync(channel.Id, options).ConfigureAwait(false);

            var builder = ImmutableArray.CreateBuilder <RestMessage>();

            foreach (var model in models)
            {
                var author = MessageHelper.GetAuthor(client, guild, model.Author.Value, model.WebhookId.ToNullable());
                builder.Add(RestMessage.Create(client, channel, author, model));
            }
            return(builder.ToImmutable());
        }
コード例 #3
0
ファイル: ChannelHelper.cs プロジェクト: Neuheit/Discord.Net
        public static IAsyncEnumerable <IReadOnlyCollection <RestMessage> > GetMessagesAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                                             ulong?fromMessageId, Direction dir, int limit, RequestOptions options)
        {
            var guildId = (channel as IGuildChannel)?.GuildId;
            var guild   = guildId != null ? (client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).Result : null;

            if (dir == Direction.Around && limit > DiscordConfig.MaxMessagesPerBatch)
            {
                int around = limit / 2;
                if (fromMessageId.HasValue)
                {
                    return(GetMessagesAsync(channel, client, fromMessageId.Value + 1, Direction.Before, around + 1, options) //Need to include the message itself
                           .Concat(GetMessagesAsync(channel, client, fromMessageId, Direction.After, around, options)));
                }
                else //Shouldn't happen since there's no public overload for ulong? and Direction
                {
                    return(GetMessagesAsync(channel, client, null, Direction.Before, around + 1, options));
                }
            }

            return(new PagedAsyncEnumerable <RestMessage>(
                       DiscordConfig.MaxMessagesPerBatch,
                       async(info, ct) =>
            {
                var args = new GetChannelMessagesParams
                {
                    RelativeDirection = dir,
                    Limit = info.PageSize
                };
                if (info.Position != null)
                {
                    args.RelativeMessageId = info.Position.Value;
                }

                var models = await client.ApiClient.GetChannelMessagesAsync(channel.Id, args, options).ConfigureAwait(false);
                var builder = ImmutableArray.CreateBuilder <RestMessage>();
                foreach (var model in models)
                {
                    var author = MessageHelper.GetAuthor(client, guild, model.Author.Value, model.WebhookId.ToNullable());
                    builder.Add(RestMessage.Create(client, channel, author, model));
                }
                return builder.ToImmutable();
            },
                       nextPage: (info, lastPage) =>
            {
                if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
                {
                    return false;
                }
                if (dir == Direction.Before)
                {
                    info.Position = lastPage.Min(x => x.Id);
                }
                else
                {
                    info.Position = lastPage.Max(x => x.Id);
                }
                return true;
            },
                       start: fromMessageId,
                       count: limit
                       ));
        }
コード例 #4
0
        internal override void Update(Model model)
        {
            base.Update(model);

            if (model.IsTextToSpeech.IsSpecified)
            {
                _isTTS = model.IsTextToSpeech.Value;
            }
            if (model.Pinned.IsSpecified)
            {
                _isPinned = model.Pinned.Value;
            }
            if (model.EditedTimestamp.IsSpecified)
            {
                _editedTimestampTicks = model.EditedTimestamp.Value?.UtcTicks;
            }
            if (model.MentionEveryone.IsSpecified)
            {
                _isMentioningEveryone = model.MentionEveryone.Value;
            }
            if (model.RoleMentions.IsSpecified)
            {
                _roleMentionIds = model.RoleMentions.Value.ToImmutableArray();
            }

            if (model.Attachments.IsSpecified)
            {
                var value = model.Attachments.Value;
                if (value.Length > 0)
                {
                    var attachments = ImmutableArray.CreateBuilder <Attachment>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        attachments.Add(Attachment.Create(value[i]));
                    }
                    _attachments = attachments.ToImmutable();
                }
                else
                {
                    _attachments = ImmutableArray.Create <Attachment>();
                }
            }

            if (model.Embeds.IsSpecified)
            {
                var value = model.Embeds.Value;
                if (value.Length > 0)
                {
                    var embeds = ImmutableArray.CreateBuilder <Embed>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        embeds.Add(value[i].ToEntity());
                    }
                    _embeds = embeds.ToImmutable();
                }
                else
                {
                    _embeds = ImmutableArray.Create <Embed>();
                }
            }

            if (model.UserMentions.IsSpecified)
            {
                var value = model.UserMentions.Value;
                if (value.Length > 0)
                {
                    var newMentions = ImmutableArray.CreateBuilder <RestUser>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        var val = value[i];
                        if (val.Object != null)
                        {
                            newMentions.Add(RestUser.Create(Discord, val.Object));
                        }
                    }
                    _userMentions = newMentions.ToImmutable();
                }
            }

            var guildId = (Channel as IGuildChannel)?.GuildId;
            var guild   = guildId != null ? (Discord as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).Result : null;

            if (model.Content.IsSpecified)
            {
                var text = model.Content.Value;
                _tags         = MessageHelper.ParseTags(text, null, guild, _userMentions);
                model.Content = text;
            }

            if (model.ReferencedMessage.IsSpecified && model.ReferencedMessage.Value != null)
            {
                var   refMsg       = model.ReferencedMessage.Value;
                IUser refMsgAuthor = MessageHelper.GetAuthor(Discord, guild, refMsg.Author.Value, refMsg.WebhookId.ToNullable());
                _referencedMessage = RestUserMessage.Create(Discord, Channel, refMsgAuthor, refMsg);
            }

            if (model.Stickers.IsSpecified)
            {
                var value = model.Stickers.Value;
                if (value.Length > 0)
                {
                    var stickers = ImmutableArray.CreateBuilder <Sticker>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        stickers.Add(Sticker.Create(value[i]));
                    }
                    _stickers = stickers.ToImmutable();
                }
                else
                {
                    _stickers = ImmutableArray.Create <Sticker>();
                }
            }
        }