vyvanse: add command top10
This commit is contained in:
parent
6766fb450a
commit
b98574f0be
|
@ -0,0 +1,59 @@
|
|||
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
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ package dao
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"git.xeserv.us/xena/gorqlite"
|
||||
"github.com/opentracing/opentracing-go"
|
||||
|
@ -79,3 +80,44 @@ func (u *Users) IncScore(ctx context.Context, discordID string) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID int `json:"id"`
|
||||
DiscordID string `json:"discord_id"`
|
||||
Score int `json:"score"`
|
||||
}
|
||||
|
||||
func (u *Users) Top10(ctx context.Context) ([]User, error) {
|
||||
sp, ctx := opentracing.StartSpanFromContext(ctx, "users.top10")
|
||||
defer sp.Finish()
|
||||
|
||||
qr, err := u.conn.QueryOne("SELECT (id, discord_id, score) FROM users WHERE score > 0 ORDER BY score LIMIT 10")
|
||||
if err != nil {
|
||||
sp.LogFields(splog.Error(err))
|
||||
}
|
||||
|
||||
if qr.Err != nil {
|
||||
sp.LogFields(splog.Error(qr.Err))
|
||||
return nil, qr.Err
|
||||
}
|
||||
|
||||
var result []User
|
||||
|
||||
if qr.NumRows() == 0 {
|
||||
sp.LogFields(splog.Error(sql.ErrNoRows))
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
|
||||
for qr.Next() {
|
||||
var u User
|
||||
err = qr.Scan(&u.ID, &u.DiscordID, &u.Score)
|
||||
if err != nil {
|
||||
sp.LogFields(splog.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result = append(result, u)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue