Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Commit

Permalink
command: help: sort commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sarisia committed May 15, 2020
1 parent 0f2d35c commit bfc7d47
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
10 changes: 5 additions & 5 deletions aria/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"nhooyr.io/websocket/wsjson"
)

const READ_LIMIT = 4194304
const TIMEOUT = 30 * time.Second
const wsReadLimit = 4194304
const clientTimeout = 30 * time.Second

type client struct {
sync.RWMutex
Expand Down Expand Up @@ -78,7 +78,7 @@ func (c *client) run(parent context.Context) {
return
}
defer conn.Close(websocket.StatusNormalClosure, "")
conn.SetReadLimit(READ_LIMIT)
conn.SetReadLimit(wsReadLimit)
c.conn = conn

if err := c.handleHelloPacket(ctx); err != nil {
Expand Down Expand Up @@ -135,7 +135,7 @@ func (c *client) recvLoop(ctx context.Context) {
// utils

func (c *client) sendRequest(parent context.Context, r *request) {
ctx, cancel := context.WithTimeout(parent, TIMEOUT)
ctx, cancel := context.WithTimeout(parent, clientTimeout)
defer cancel()

if err := wsjson.Write(ctx, c.conn, r); err != nil {
Expand All @@ -153,7 +153,7 @@ func (c *client) recvPacket(ctx context.Context) *packet {
}

func (c *client) handlePacket(parent context.Context, p *packet) {
ctx, cancel := context.WithTimeout(parent, TIMEOUT)
ctx, cancel := context.WithTimeout(parent, clientTimeout)
defer cancel()

// fill packet data
Expand Down
61 changes: 47 additions & 14 deletions aria/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"log"
"math/rand"
"net/url"
"sort"
"strconv"
"strings"
"time"

"github.com/bwmarrin/discordgo"
)

// since discordgo's functions does not provide context support,
// we cannot timeout command execution
var commandsString string
var aliasesString string

type cmdHandler func(*discordgo.Message, []string)
type cmdHelp struct {
desc string
Expand Down Expand Up @@ -401,28 +403,22 @@ func (b *bot) cmdHelp(m *discordgo.Message, args []string) {
e.Title = "Help"
e.Description = fmt.Sprintf("Type `%shelp [command]` to get command help", b.prefix)

cmds := []string{}
for c := range b.cmdHandlers {
cmds = append(cmds, fmt.Sprintf("`%s`", c))
if commandsString == "" {
b.updateCommandsString()
}
alines := []string{}
for c, as := range b.alias.Alias {
fa := []string{}
for _, a := range as {
fa = append(fa, fmt.Sprintf("`%s`", a))
}
alines = append(alines, fmt.Sprintf("`%s`: %s", c, strings.Join(fa, ", ")))
if aliasesString == "" {
b.updateAliasesString()
}

e.Fields = []*discordgo.MessageEmbedField{
{
Name: "Commands",
Value: strings.Join(cmds, ", "),
Value: commandsString,
Inline: false,
},
{
Name: "Alias",
Value: strings.Join(alines, "\n"),
Value: aliasesString,
Inline: false,
},
}
Expand All @@ -442,6 +438,43 @@ func (b *bot) cmdHelp(m *discordgo.Message, args []string) {

// utility functions

func (b *bot) updateCommandsString() {
log.Println("updating commandString")

cmds := []string{}
for c := range b.cmdHandlers {
cmds = append(cmds, fmt.Sprintf("`%s`", c))
}

sort.Slice(cmds, func(i, j int) bool {
return cmds[i] < cmds[j]
})

commandsString = strings.Join(cmds, ", ")
}

func (b *bot) updateAliasesString() {
log.Println("updating aliasesString")

alines := []string{}
for c, as := range b.alias.Alias {
fa := []string{}
for _, a := range as {
fa = append(fa, fmt.Sprintf("`%s`", a))
}

sort.Slice(fa, func(i, j int) bool {
return fa[i] < fa[j]
})
alines = append(alines, fmt.Sprintf("`%s`: %s", c, strings.Join(fa, ", ")))
}

sort.Slice(alines, func(i, j int) bool {
return alines[i] < alines[j]
})
aliasesString = strings.Join(alines, "\n")
}

func newEmbed() (e *discordgo.MessageEmbed) {
e = new(discordgo.MessageEmbed)
e.Footer = msgAuthor
Expand Down

0 comments on commit bfc7d47

Please sign in to comment.