-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from disgoorg/feature/pagination-helpers
- Loading branch information
Showing
6 changed files
with
184 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
_ "embed" | ||
"os" | ||
|
||
"github.com/disgoorg/disgo" | ||
"github.com/disgoorg/disgo/rest" | ||
"github.com/disgoorg/log" | ||
) | ||
|
||
var token = os.Getenv("disgo_token") | ||
|
||
func main() { | ||
log.SetFlags(log.LstdFlags | log.Lshortfile) | ||
log.SetLevel(log.LevelDebug) | ||
log.Info("starting example...") | ||
log.Info("bot version: ", disgo.Version) | ||
|
||
client := rest.New(rest.NewClient(token)) | ||
|
||
page := client.GetMessagesPage(817327182111571989, 1016790288607498240, 3) | ||
|
||
var i int | ||
for page.Next() { | ||
for _, m := range page.Items { | ||
println(m.ID) | ||
} | ||
println("---") | ||
i++ | ||
if i >= 3 { | ||
break | ||
} | ||
} | ||
if page.Err != nil { | ||
log.Error(page.Err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package rest | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/disgoorg/disgo/discord" | ||
"github.com/disgoorg/snowflake/v2" | ||
) | ||
|
||
var ErrNoMorePages = errors.New("no more pages") | ||
|
||
type Page[T any] struct { | ||
getItemsFunc func(before snowflake.ID, after snowflake.ID) ([]T, error) | ||
getIDFunc func(t T) snowflake.ID | ||
|
||
Items []T | ||
Err error | ||
|
||
ID snowflake.ID | ||
} | ||
|
||
func (p *Page[T]) Next() bool { | ||
if p.Err != nil { | ||
return false | ||
} | ||
|
||
if len(p.Items) > 0 { | ||
p.ID = p.getIDFunc(p.Items[0]) | ||
} | ||
|
||
p.Items, p.Err = p.getItemsFunc(0, p.ID) | ||
if p.Err == nil && len(p.Items) == 0 { | ||
p.Err = ErrNoMorePages | ||
} | ||
return p.Err == nil | ||
} | ||
|
||
func (p *Page[T]) Previous() bool { | ||
if p.Err != nil { | ||
return false | ||
} | ||
|
||
if len(p.Items) > 0 { | ||
p.ID = p.getIDFunc(p.Items[len(p.Items)-1]) | ||
} | ||
|
||
p.Items, p.Err = p.getItemsFunc(p.ID, 0) | ||
if p.Err == nil && len(p.Items) == 0 { | ||
p.Err = ErrNoMorePages | ||
} | ||
return p.Err == nil | ||
} | ||
|
||
type AuditLogPage struct { | ||
getItems func(before snowflake.ID) (discord.AuditLog, error) | ||
|
||
discord.AuditLog | ||
Err error | ||
|
||
ID snowflake.ID | ||
} | ||
|
||
func (p *AuditLogPage) Previous() bool { | ||
if p.Err != nil { | ||
return false | ||
} | ||
|
||
if len(p.AuditLogEntries) > 0 { | ||
p.ID = p.AuditLogEntries[len(p.AuditLogEntries)-1].ID | ||
} | ||
|
||
p.AuditLog, p.Err = p.getItems(p.ID) | ||
if p.Err == nil && len(p.AuditLogEntries) == 0 { | ||
p.Err = ErrNoMorePages | ||
} | ||
return p.Err == nil | ||
} |