vyvanse: fix top10 command, known unicode issue

This commit is contained in:
Cadey Ratio 2017-08-29 11:41:38 -07:00
parent b98574f0be
commit adcc28d417
No known key found for this signature in database
GPG Key ID: D607EE27C2E7F89A
3 changed files with 20 additions and 10 deletions

View File

@ -132,8 +132,13 @@ func main() {
cs.AddCmd("printerfact", "facts about printers", bot.NoPermissions, printerFact)
cs.AddCmd("dice", "roll the dice", bot.NoPermissions, roll)
cs.AddCmd("splattus", "splatoon 2 map rotation status", bot.NoPermissions, spla2nMaps)
cs.AddCmd("top10", "shows the top 10 chatters on this server", bot.NoPermissions, top10(us))
dg.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
sp, ctx := opentracing.StartSpanFromContext(context.Background(), "discordgo.message.create")
defer sp.Finish()

View File

@ -4,12 +4,12 @@ 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"
splog "github.com/opentracing/opentracing-go/log"
)
func top10(us *dao.Users) func(context.Context, *discordgo.Session, *discordgo.Message, []string) error {
@ -25,23 +25,28 @@ func top10(us *dao.Users) func(context.Context, *discordgo.Session, *discordgo.M
buf := &bytes.Buffer{}
table := tablewriter.NewWriter(buf)
table.SetHeader([]string{"Rank", "Nick", "Score"})
table.SetHeader([]string{"Nick", "Score"})
for i, u := range users {
for _, u := range users {
apd := []string{}
apd = append(apd, strconv.Itoa(i+1))
ch, err := s.Channel(m.ChannelID)
if err != nil {
return err
sp.LogFields(splog.Error(err))
continue
}
mem, err := s.GuildMember(ch.GuildID, u.DiscordID)
if err != nil {
return err
sp.LogFields(splog.Error(err))
continue
}
apd = append(apd, mem.Nick, strconv.Itoa(u.Score))
if mem.Nick == "" {
mem.Nick = mem.User.Username
}
apd = append(apd, mem.Nick, fmt.Sprint(u.Score))
table.Append(apd)
}

View File

@ -82,16 +82,16 @@ func (u *Users) IncScore(ctx context.Context, discordID string) error {
}
type User struct {
ID int `json:"id"`
ID int64 `json:"id"`
DiscordID string `json:"discord_id"`
Score int `json:"score"`
Score int64 `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")
qr, err := u.conn.QueryOne("SELECT id, discord_id, score FROM users WHERE score > 0 ORDER BY score DESC LIMIT 10")
if err != nil {
sp.LogFields(splog.Error(err))
}