mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-09-29 21:58:57 +00:00
API: Separated min function
This commit is contained in:
parent
088b304303
commit
d69d6750dc
|
@ -5,6 +5,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/diamondburned/arikawa/v2/discord" // for clarity
|
"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/httputil"
|
||||||
"github.com/diamondburned/arikawa/v2/utils/json/option"
|
"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 {
|
for limit > 0 || unlimited {
|
||||||
if limit > 0 {
|
if limit > 0 {
|
||||||
// Only fetch as much as we need. Since limit gradually decreases,
|
// 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).
|
||||||
fetch = uint(min(MaxGuildFetchLimit, int(limit)))
|
fetch = uint(intmath.Min(MaxGuildFetchLimit, int(limit)))
|
||||||
limit -= fetch
|
limit -= fetch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,8 +192,8 @@ func (c *Client) GuildsAfter(after discord.GuildID, limit uint) ([]discord.Guild
|
||||||
for limit > 0 || unlimited {
|
for limit > 0 || unlimited {
|
||||||
if limit > 0 {
|
if limit > 0 {
|
||||||
// Only fetch as much as we need. Since limit gradually decreases,
|
// 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).
|
||||||
fetch = uint(min(MaxGuildFetchLimit, int(limit)))
|
fetch = uint(intmath.Min(MaxGuildFetchLimit, int(limit)))
|
||||||
limit -= fetch
|
limit -= fetch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
api/math.go
10
api/math.go
|
@ -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
|
|
||||||
}
|
|
|
@ -2,6 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/diamondburned/arikawa/v2/discord"
|
"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/httputil"
|
||||||
"github.com/diamondburned/arikawa/v2/utils/json/option"
|
"github.com/diamondburned/arikawa/v2/utils/json/option"
|
||||||
)
|
)
|
||||||
|
@ -45,9 +46,9 @@ func (c *Client) MembersAfter(
|
||||||
|
|
||||||
for limit > 0 || unlimited {
|
for limit > 0 || unlimited {
|
||||||
// Only fetch as much as we need. Since limit gradually decreases,
|
// 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 {
|
if limit > 0 {
|
||||||
fetch = uint(min(MaxMemberFetchLimit, int(limit)))
|
fetch = uint(intmath.Min(MaxMemberFetchLimit, int(limit)))
|
||||||
limit -= fetch
|
limit -= fetch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/diamondburned/arikawa/v2/discord"
|
"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/httputil"
|
||||||
"github.com/diamondburned/arikawa/v2/utils/json/option"
|
"github.com/diamondburned/arikawa/v2/utils/json/option"
|
||||||
)
|
)
|
||||||
|
@ -66,8 +67,8 @@ func (c *Client) MessagesBefore(
|
||||||
for limit > 0 || unlimited {
|
for limit > 0 || unlimited {
|
||||||
if limit > 0 {
|
if limit > 0 {
|
||||||
// Only fetch as much as we need. Since limit gradually decreases,
|
// 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).
|
||||||
fetch = uint(min(maxMessageFetchLimit, int(limit)))
|
fetch = uint(intmath.Min(maxMessageFetchLimit, int(limit)))
|
||||||
limit -= maxMessageFetchLimit
|
limit -= maxMessageFetchLimit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,8 +124,8 @@ func (c *Client) MessagesAfter(
|
||||||
for limit > 0 || unlimited {
|
for limit > 0 || unlimited {
|
||||||
if limit > 0 {
|
if limit > 0 {
|
||||||
// Only fetch as much as we need. Since limit gradually decreases,
|
// 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).
|
||||||
fetch = uint(min(maxMessageFetchLimit, int(limit)))
|
fetch = uint(intmath.Min(maxMessageFetchLimit, int(limit)))
|
||||||
limit -= maxMessageFetchLimit
|
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
|
// 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
|
// to accept at one time then batches of messages will be deleted
|
||||||
for start := 0; start < len(messageIDs); start += maxMessageDeleteLimit {
|
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])
|
err := c.deleteMessages(channelID, messageIDs[start:end])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -2,6 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/diamondburned/arikawa/v2/discord"
|
"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/httputil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -73,8 +74,8 @@ func (c *Client) ReactionsBefore(
|
||||||
for limit > 0 || unlimited {
|
for limit > 0 || unlimited {
|
||||||
if limit > 0 {
|
if limit > 0 {
|
||||||
// Only fetch as much as we need. Since limit gradually decreases,
|
// 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).
|
||||||
fetch = uint(min(MaxMessageReactionFetchLimit, int(limit)))
|
fetch = uint(intmath.Min(MaxMessageReactionFetchLimit, int(limit)))
|
||||||
limit -= fetch
|
limit -= fetch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,8 +122,8 @@ func (c *Client) ReactionsAfter(
|
||||||
for limit > 0 || unlimited {
|
for limit > 0 || unlimited {
|
||||||
if limit > 0 {
|
if limit > 0 {
|
||||||
// Only fetch as much as we need. Since limit gradually decreases,
|
// 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).
|
||||||
fetch = uint(min(MaxMessageReactionFetchLimit, int(limit)))
|
fetch = uint(intmath.Min(MaxMessageReactionFetchLimit, int(limit)))
|
||||||
limit -= fetch
|
limit -= fetch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
internal/intmath/math.go
Normal file
10
internal/intmath/math.go
Normal 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
|
||||||
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
package api
|
package intmath
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func Test_min(t *testing.T) {
|
func TestMin(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
a, b int
|
a, b int
|
||||||
|
@ -30,7 +30,7 @@ func Test_min(t *testing.T) {
|
||||||
|
|
||||||
for _, c := range testCases {
|
for _, c := range testCases {
|
||||||
t.Run(c.name, func(t *testing.T) {
|
t.Run(c.name, func(t *testing.T) {
|
||||||
actual := min(c.a, c.b)
|
actual := Min(c.a, c.b)
|
||||||
if c.expect != actual {
|
if c.expect != actual {
|
||||||
t.Errorf("expected min(%d, %d) to return %d, but did %d", c.a, c.b, c.expect, actual)
|
t.Errorf("expected min(%d, %d) to return %d, but did %d", c.a, c.b, c.expect, actual)
|
||||||
}
|
}
|
Loading…
Reference in a new issue