go: add help command and refactor commands.go

pull/13/head
ALI Hamza 2019-10-14 19:28:46 +07:00
parent 5a31d1fe1e
commit 42ef07a1b4
No known key found for this signature in database
GPG Key ID: BCA8A46C87798C4C
4 changed files with 84 additions and 11 deletions

@ -1,5 +1,10 @@
package config package config
//==Help Command==
const HelpTitle = "指令列表 Command List"
const HelpColour = 0x00E58D
const HelpDescription = "这个信息包含了所有指令的资料。\nThis message contains all the information about available commands."
//==Message Util== //==Message Util==
const UsageTitle = "使用方法 Command Usage" const UsageTitle = "使用方法 Command Usage"
const UsageColour = 0xF00000 const UsageColour = 0xF00000
@ -61,6 +66,7 @@ const EditAsUsage = "!editas <频道ID> <信息ID> <信息>\n!editas <channel ID
const EditAsSuccessfulDescription = "成功编辑该信息。\nMessage edited successfully." const EditAsSuccessfulDescription = "成功编辑该信息。\nMessage edited successfully."
//VTL is view trust level //VTL is view trust level
const VTLUsage = "!peektrust [目标]\n!peektrust [target]"
const VTLColour = 0xE0E000 const VTLColour = 0xE0E000
const VTLSingleUserTitle = "<@%s>的信誉分 <@%s>'s Trust Score" const VTLSingleUserTitle = "<@%s>的信誉分 <@%s>'s Trust Score"
const VTLSingleUserValue = "分数 Trust Score" const VTLSingleUserValue = "分数 Trust Score"

@ -25,6 +25,7 @@ func StartBot(token string, kill chan bool) {
} }
fmt.Println("Bot is now running. Press CTRL-C to exit.") fmt.Println("Bot is now running. Press CTRL-C to exit.")
modules.GenerateHelpEmbed()
go modules.ListenToVoteFinishes(dg) go modules.ListenToVoteFinishes(dg)
go backend.UpdateVoiceChannelState(dg) go backend.UpdateVoiceChannelState(dg)

@ -1,44 +1,75 @@
package modules package modules
import "github.com/bwmarrin/discordgo" import (
"TerraOceanBot/discord/config"
"github.com/bwmarrin/discordgo"
)
type CommandHandler func(s *discordgo.Session, m *discordgo.MessageCreate, commands []string) type CommandHandler func(s *discordgo.Session, m *discordgo.MessageCreate, commands []string)
type Command struct { type Command struct {
Name string Name string
Handler CommandHandler Handler CommandHandler
Description string
Usage string
Admin bool
} }
var Commands = []Command{ var Commands = []Command{
Command{ //Admin Commands
Name: "!sendas",
Handler: sendAs,
},
Command{ Command{
Name: "!editas", Name: "!editas",
Handler: editAs, Handler: editAs,
Usage: config.EditAsUsage,
Admin: true,
}, },
Command{ Command{
Name: "!peektrust", Name: "!peektrust",
Handler: viewTrustLevel, Handler: viewTrustLevel,
Usage: config.VTLUsage,
Admin: true,
},
Command{
Name: "!sendas",
Handler: sendAs,
Usage: config.SendAsUsage,
Admin: true,
},
//Regular Commands
Command{
Name: "!help",
Handler: showHelp,
Usage: "!help",
Admin: false,
}, },
Command{ Command{
Name: "!invite", Name: "!invite",
Handler: createInvite, Handler: createInvite,
Usage: config.InviteUsage,
Admin: false,
}, },
Command{ Command{
Name: "!validate", Name: "!setmcusername",
Handler: checkUseInvite, Handler: updateMinecraftUsername,
Usage: config.SetMCUsernameUsage,
Admin: false,
}, },
Command{ Command{
Name: "!trust", Name: "!trust",
Handler: changeTrust, Handler: changeTrust,
Usage: config.ChangeTrustUsage,
Admin: false,
}, },
Command{ Command{
Name: "!setmcusername", Name: "!validate",
Handler: updateMinecraftUsername, Handler: checkUseInvite,
Usage: config.ValidateUsage,
Admin: false,
}, },
Command{ Command{
Name: "!votesuggest", Name: "!votesuggest",
Handler: voteSuggestion, Handler: voteSuggestion,
Usage: config.VoteSuggestionUsage,
Admin: false,
}, },
} }

@ -0,0 +1,35 @@
package modules
import (
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
"github.com/bwmarrin/discordgo"
)
var embed *discordgo.MessageEmbed
//GenerateHelpEmbed sets up the embed variable above to be used to display the !help command.
func GenerateHelpEmbed() {
emb := message.NewEmbed().
SetTitle(config.HelpTitle).
SetColour(config.HelpColour).
SetDescription(config.HelpDescription)
for _, cmd := range Commands {
usage := cmd.Usage
if usage == "" {
usage = "No usage provided"
}
emb.AddField(cmd.Name, usage)
}
embed = emb.Build()
}
func showHelp(s *discordgo.Session, m *discordgo.MessageCreate, commands []string) {
_, err := s.ChannelMessageSendEmbed(m.ChannelID, embed)
if err != nil {
message.AuditError(s, m.ChannelID, err)
}
}