From c2722be0ce3afac2d5320d49de556220ea0d2bc5 Mon Sep 17 00:00:00 2001 From: kageru Date: Tue, 5 Jun 2018 13:47:09 +0200 Subject: [PATCH] add comments to command.go --- command.go | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/command.go b/command.go index 2424610..cdc1147 100644 --- a/command.go +++ b/command.go @@ -11,6 +11,7 @@ import ( type CommandType int +// These are used to specify Command.CommandType when registering new commands const ( CommandTypePrefix CommandType = 0 CommandTypeFullMatch CommandType = 1 @@ -18,18 +19,25 @@ const ( CommandTypeContains CommandType = 3 ) + /* + This struct represents a command object. + The options should be self-explanatory, but they are also explained in the readme. + A struct can be initialized by passing any number of its attributes as parameters. + Everything not set will be set to the go-usual defaults ("" for string, 0 for int, false for bool, nil for the rest) + Any command that has a Trigger is valid (but useless if nothing else is specified) + */ type Command struct { Trigger string // must be specified Output string // no output if unspecified OutputEmbed *discordgo.MessageEmbed // no embed output if unspecified Type CommandType // defaults to Prefix Cooldown int // defaults to 0 (no cooldown) - OutputIsReply bool // defaults to false - RequiresMention bool // defaults to false - DeleteInput bool // defaults to false - DMOnly bool // defaults to false - AdminOnly bool // defaults to false - IgnoreCase bool // defaults to false + OutputIsReply bool + RequiresMention bool + DeleteInput bool + DMOnly bool + AdminOnly bool + IgnoreCase bool // for custom commands that go beyond prints and deletions Function func(*discordgo.Session, *discordgo.MessageCreate) @@ -37,6 +45,7 @@ type Command struct { } +// Performs basic input validation on a given command and adds it to the global command array func registerCommand(command Command) { if command.Trigger == "" { fmt.Println("Cannot register a command with no trigger. Skipping.") @@ -48,6 +57,13 @@ func registerCommand(command Command) { commands = append(commands, command) } + /* + Any message that the bot can read is evaluated here. + The message is matched against each of the command triggers depending on the respective match type. + If one of the commands matches, execute that command and return. + Only one command can be executed per message. Earlier defined commands take precedence. + This is a deliberate choice (for now). + */ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) { if m.Author.ID == s.State.User.ID { log.Printf(" %s", m.Content) @@ -88,6 +104,10 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) { } } + /* + Executes the given command on the given message and session. + Sets command cooldowns if necessary and also clears them again. + */ func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command Command, commandIndex int) { if (!command.DMOnly || (getChannel(session.State, message.ChannelID).Type == discordgo.ChannelTypeDM)) && (!command.AdminOnly || (message.Author.ID == config.AdminID)) && !command.IsOnCooldown { @@ -130,6 +150,10 @@ func generateReply(message *discordgo.MessageCreate, command Command) string { return output } + /* + Any message passed to this method will be redirected to config.ModChannel. + This is useful for anonymous complaints or similar messages. + */ func redirectComplaint(s *discordgo.Session, m *discordgo.MessageCreate) { embed := &discordgo.MessageEmbed { Author: &discordgo.MessageEmbedAuthor{}, @@ -142,3 +166,4 @@ func redirectComplaint(s *discordgo.Session, m *discordgo.MessageCreate) { func echoMessage(s *discordgo.Session, m *discordgo.MessageCreate) { s.ChannelMessageSend(m.ChannelID, m.Content) } +