1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-17 12:23:08 +00:00

API: Separated min function

This commit is contained in:
diamondburned 2020-12-26 16:08:41 -08:00
parent 088b304303
commit d69d6750dc
7 changed files with 32 additions and 28 deletions

View file

@ -5,6 +5,7 @@ import (
"net/url"
"github.com/diamondburned/arikawa/v2/discord" // for clarity
"github.com/diamondburned/arikawa/v2/internal/intmath"
"github.com/diamondburned/arikawa/v2/utils/httputil"
"github.com/diamondburned/arikawa/v2/utils/json/option"
)
@ -146,8 +147,8 @@ func (c *Client) GuildsBefore(before discord.GuildID, limit uint) ([]discord.Gui
for limit > 0 || unlimited {
if limit > 0 {
// Only fetch as much as we need. Since limit gradually decreases,
// we only need to fetch min(fetch, limit).
fetch = uint(min(MaxGuildFetchLimit, int(limit)))
// we only need to fetch intmath.Min(fetch, limit).
fetch = uint(intmath.Min(MaxGuildFetchLimit, int(limit)))
limit -= fetch
}
@ -191,8 +192,8 @@ func (c *Client) GuildsAfter(after discord.GuildID, limit uint) ([]discord.Guild
for limit > 0 || unlimited {
if limit > 0 {
// Only fetch as much as we need. Since limit gradually decreases,
// we only need to fetch min(fetch, limit).
fetch = uint(min(MaxGuildFetchLimit, int(limit)))
// we only need to fetch intmath.Min(fetch, limit).
fetch = uint(intmath.Min(MaxGuildFetchLimit, int(limit)))
limit -= fetch
}

View file

@ -1,10 +0,0 @@
package api
// min returns the smaller of the two passed numbers.
func min(a, b int) int {
if a < b {
return a
}
return b
}

View file

@ -2,6 +2,7 @@ package api
import (
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/internal/intmath"
"github.com/diamondburned/arikawa/v2/utils/httputil"
"github.com/diamondburned/arikawa/v2/utils/json/option"
)
@ -45,9 +46,9 @@ func (c *Client) MembersAfter(
for limit > 0 || unlimited {
// Only fetch as much as we need. Since limit gradually decreases,
// we only need to fetch min(fetch, limit).
// we only need to fetch intmath.Min(fetch, limit).
if limit > 0 {
fetch = uint(min(MaxMemberFetchLimit, int(limit)))
fetch = uint(intmath.Min(MaxMemberFetchLimit, int(limit)))
limit -= fetch
}

View file

@ -4,6 +4,7 @@ import (
"github.com/pkg/errors"
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/internal/intmath"
"github.com/diamondburned/arikawa/v2/utils/httputil"
"github.com/diamondburned/arikawa/v2/utils/json/option"
)
@ -66,8 +67,8 @@ func (c *Client) MessagesBefore(
for limit > 0 || unlimited {
if limit > 0 {
// Only fetch as much as we need. Since limit gradually decreases,
// we only need to fetch min(fetch, limit).
fetch = uint(min(maxMessageFetchLimit, int(limit)))
// we only need to fetch intmath.Min(fetch, limit).
fetch = uint(intmath.Min(maxMessageFetchLimit, int(limit)))
limit -= maxMessageFetchLimit
}
@ -123,8 +124,8 @@ func (c *Client) MessagesAfter(
for limit > 0 || unlimited {
if limit > 0 {
// Only fetch as much as we need. Since limit gradually decreases,
// we only need to fetch min(fetch, limit).
fetch = uint(min(maxMessageFetchLimit, int(limit)))
// we only need to fetch intmath.Min(fetch, limit).
fetch = uint(intmath.Min(maxMessageFetchLimit, int(limit)))
limit -= maxMessageFetchLimit
}
@ -350,7 +351,7 @@ func (c *Client) DeleteMessages(channelID discord.ChannelID, messageIDs []discor
// If the number of messages to be deleted exceeds the amount discord is willing
// to accept at one time then batches of messages will be deleted
for start := 0; start < len(messageIDs); start += maxMessageDeleteLimit {
end := min(len(messageIDs), start+maxMessageDeleteLimit)
end := intmath.Min(len(messageIDs), start+maxMessageDeleteLimit)
err := c.deleteMessages(channelID, messageIDs[start:end])
if err != nil {
return err

View file

@ -2,6 +2,7 @@ package api
import (
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/internal/intmath"
"github.com/diamondburned/arikawa/v2/utils/httputil"
)
@ -73,8 +74,8 @@ func (c *Client) ReactionsBefore(
for limit > 0 || unlimited {
if limit > 0 {
// Only fetch as much as we need. Since limit gradually decreases,
// we only need to fetch min(fetch, limit).
fetch = uint(min(MaxMessageReactionFetchLimit, int(limit)))
// we only need to fetch intmath.Min(fetch, limit).
fetch = uint(intmath.Min(MaxMessageReactionFetchLimit, int(limit)))
limit -= fetch
}
@ -121,8 +122,8 @@ func (c *Client) ReactionsAfter(
for limit > 0 || unlimited {
if limit > 0 {
// Only fetch as much as we need. Since limit gradually decreases,
// we only need to fetch min(fetch, limit).
fetch = uint(min(MaxMessageReactionFetchLimit, int(limit)))
// we only need to fetch intmath.Min(fetch, limit).
fetch = uint(intmath.Min(MaxMessageReactionFetchLimit, int(limit)))
limit -= fetch
}

10
internal/intmath/math.go Normal file
View file

@ -0,0 +1,10 @@
package intmath
// Min returns the smaller of the two passed numbers.
func Min(a, b int) int {
if a < b {
return a
}
return b
}

View file

@ -1,8 +1,8 @@
package api
package intmath
import "testing"
func Test_min(t *testing.T) {
func TestMin(t *testing.T) {
testCases := []struct {
name string
a, b int
@ -30,7 +30,7 @@ func Test_min(t *testing.T) {
for _, c := range testCases {
t.Run(c.name, func(t *testing.T) {
actual := min(c.a, c.b)
actual := Min(c.a, c.b)
if c.expect != actual {
t.Errorf("expected min(%d, %d) to return %d, but did %d", c.a, c.b, c.expect, actual)
}