public void CanCreateFromTimestamp()
            {
                var yearOfHell = new DateTimeOffset(2020, 1, 1, 6, 0, 0, TimeSpan.Zero);
                var snowflake  = Snowflake.CreateTimestampSnowflake(yearOfHell);

                Assert.Equal(yearOfHell.ToUnixTimeMilliseconds(), snowflake.Timestamp.ToUnixTimeMilliseconds());
            }
示例#2
0
 public ButtonBuilder(ButtonComponentStyle style)
 {
     _snowflake          = Snowflake.CreateTimestampSnowflake(DateTimeOffset.UtcNow);
     _buttonComponent    = new(style);
     _handler            = null;
     _requiredPermission = new();
 }
            public void CreatesSnowflakeWithUtcNowTime()
            {
                var now       = DateTimeOffset.UtcNow;
                var snowflake = Snowflake.CreateTimestampSnowflake();

                // Some allowance is made here because of clock inaccuracies and the potential for scheduler differences
                // between the two above time measurements.
                var isWithinFiveSeconds = Math.Abs
                                          (
                    now.ToUnixTimeSeconds() - snowflake.Timestamp.ToUnixTimeSeconds()
                                          ) <= 5;

                Assert.True(isWithinFiveSeconds);
            }
    // ReSharper disable once UnusedParameter.Local
    private async Task <Result <Snowflake> > FindMostProbableDeleterAsync(IMessage message, Snowflake guildID)
    {
        var now    = Snowflake.CreateTimestampSnowflake(epoch: Constants.DiscordEpoch);
        var before = now;
        var after  = message.ID;

        while (true)
        {
            var getAuditLogEntries = await _auditLogAPI.GetAuditLogAsync
                                     (
                guildID,
                actionType : AuditLogEvent.MessageDelete,
                before : before
                                     );

            if (!getAuditLogEntries.IsSuccess)
            {
                return(Result <Snowflake> .FromError(getAuditLogEntries));
            }

            var entries = getAuditLogEntries.Entity;
            if (entries.AuditLogEntries.Count == 0)
            {
                break;
            }

            var match = entries.AuditLogEntries.OrderByDescending(i => i.ID.Timestamp).FirstOrDefault
                        (
                e =>
            {
                if (!e.Options.IsDefined(out var options))
                {
                    return(false);
                }

                if (!options.ChannelID.IsDefined(out var channelID))
                {
                    return(false);
                }

                if (channelID != message.ChannelID)
                {
                    return(false);
                }

                // Discard entries that are unreasonably old
                if (now.Timestamp - e.ID.Timestamp > TimeSpan.FromMinutes(1))
                {
                    return(false);
                }

                return(e.TargetID == message.Author.ID.ToString());
            }
                        );

            if (match?.UserID != null)
            {
                return(match.UserID.Value);
            }

            before = entries.AuditLogEntries.OrderBy(i => i.ID.Timestamp).First().ID;
            if (before.Timestamp < after.Timestamp)
            {
                // No more entries that are relevant
                break;
            }
        }

        // No audit entries are generated when the user deletes a message themselves
        return(message.Author.ID);
    }