canopeas/discord/help.go

89 lines
1.8 KiB
Go
Raw Normal View History

package discord
import (
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
)
func init() {
commands = append(commands, &command{
deleteInvocation: true,
name: "help",
validate: func(cmd commandInit) bool {
return true
},
run: func(cmd commandInit) (string, error) {
args := strings.Fields(cmd.message.Content)[1:]
var resp *discordgo.MessageEmbed
if len(args) == 1 {
resp = singleHelp(cmd, args[0])
} else {
resp = allHelp(cmd)
}
channel, err := cmd.session.UserChannelCreate(cmd.message.Author.ID)
if err != nil {
return "", err
}
sendEmbed(cmd.session, channel.ID, resp)
return "", nil
},
help: "HELP! HEEEEEEEEEELP!",
})
}
func singleHelp(cmd commandInit, arg string) *discordgo.MessageEmbed {
embed := &discordgo.MessageEmbed{
Title: "Unkown Command",
Color: 0x007D96,
}
c, ok := commandMap[arg]
if !ok {
return embed
}
if c.staffOnly && !isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles) {
return embed
}
embed.Title = c.name
embed.Description = c.help
return embed
}
func allHelp(cmd commandInit) *discordgo.MessageEmbed {
staff := isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles)
embed := &discordgo.MessageEmbed{
Title: "SedBot Help",
Fields: make([]*discordgo.MessageEmbedField, 0),
}
if staff {
embed.Description = "Commands with an asterisk (*) are staff-only"
}
for _, c := range commands {
if c.echo {
continue
}
cmdName := c.name
if len(c.aliases) > 0 {
cmdName += fmt.Sprintf(" (%s)", strings.Join(c.aliases, ", "))
}
if c.staffOnly {
cmdName = fmt.Sprintf("*%s", cmdName)
}
if !c.staffOnly || staff {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
Name: cmdName,
Value: c.help,
Inline: true,
})
}
}
return embed
}