1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-11-01 12:24:15 +00:00
cchat-discord/internal/discord/channel/commander.go
2020-12-18 21:46:12 -08:00

78 lines
1.7 KiB
Go

package channel
import (
"strings"
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-discord/internal/discord/channel/commands"
"github.com/diamondburned/cchat-discord/internal/discord/channel/message/send/complete"
"github.com/diamondburned/cchat-discord/internal/discord/channel/shared"
"github.com/diamondburned/cchat/text"
)
type Commander struct {
shared.Channel
msgCompl complete.ChannelCompleter
}
func NewCommander(ch shared.Channel) cchat.Commander {
return Commander{
Channel: ch,
msgCompl: complete.ChannelCompleter{
Channel: ch,
},
}
}
func (ch Commander) AsCompleter() cchat.Completer { return ch }
func (ch Commander) Run(words []string) ([]byte, error) {
return commands.World.Run(ch.Channel, words)
}
func (ch Commander) Complete(words []string, i int64) []cchat.CompletionEntry {
if i == 0 {
commands := commands.World.Find(words[0])
var entries = make([]cchat.CompletionEntry, 0, len(commands))
if strings.HasPrefix(words[0], "help") {
entries = append(entries, cchat.CompletionEntry{
Raw: "help",
Text: text.Plain("help"),
Secondary: text.Plain("Prints the help message"),
})
}
for _, cmd := range commands {
entries = append(entries, cchat.CompletionEntry{
Raw: cmd.Name,
Text: text.Plain(cmd.Name),
Secondary: text.Plain(cmd.Desc),
})
}
return entries
}
cmd := commands.World.FindExact(words[0])
if cmd == nil {
return nil
}
name, _ := cmd.Args.At(int(i) - 1)
if name == "" {
return nil
}
switch name {
case "mention:user":
return ch.msgCompl.CompleteMentions(words[i])
case "mention:emoji":
return ch.msgCompl.CompleteEmojis(words[i])
case "mention:channel":
return ch.msgCompl.CompleteChannels(words[i])
}
return nil
}