60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"git.xeserv.us/xena/vyvanse/internal/dao"
|
|
"github.com/bwmarrin/discordgo"
|
|
"github.com/olekukonko/tablewriter"
|
|
opentracing "github.com/opentracing/opentracing-go"
|
|
)
|
|
|
|
func top10(us *dao.Users) func(context.Context, *discordgo.Session, *discordgo.Message, []string) error {
|
|
return func(ctx context.Context, s *discordgo.Session, m *discordgo.Message, parv []string) error {
|
|
sp, ctx := opentracing.StartSpanFromContext(ctx, "top10")
|
|
defer sp.Finish()
|
|
|
|
users, err := us.Top10(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
table := tablewriter.NewWriter(buf)
|
|
table.SetHeader([]string{"Rank", "Nick", "Score"})
|
|
|
|
for i, u := range users {
|
|
apd := []string{}
|
|
apd = append(apd, strconv.Itoa(i+1))
|
|
|
|
ch, err := s.Channel(m.ChannelID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
mem, err := s.GuildMember(ch.GuildID, u.DiscordID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
apd = append(apd, mem.Nick, strconv.Itoa(u.Score))
|
|
|
|
table.Append(apd)
|
|
}
|
|
|
|
table.Render()
|
|
|
|
txt := fmt.Sprintf("```\n%s\n```", buf)
|
|
_, err = s.ChannelMessageSend(m.ChannelID, txt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|