add comments to command.go
This commit is contained in:
parent
d0246a6c4e
commit
c2722be0ce
37
command.go
37
command.go
@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
type CommandType int
|
type CommandType int
|
||||||
|
|
||||||
|
// These are used to specify Command.CommandType when registering new commands
|
||||||
const (
|
const (
|
||||||
CommandTypePrefix CommandType = 0
|
CommandTypePrefix CommandType = 0
|
||||||
CommandTypeFullMatch CommandType = 1
|
CommandTypeFullMatch CommandType = 1
|
||||||
@ -18,18 +19,25 @@ const (
|
|||||||
CommandTypeContains CommandType = 3
|
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 {
|
type Command struct {
|
||||||
Trigger string // must be specified
|
Trigger string // must be specified
|
||||||
Output string // no output if unspecified
|
Output string // no output if unspecified
|
||||||
OutputEmbed *discordgo.MessageEmbed // no embed output if unspecified
|
OutputEmbed *discordgo.MessageEmbed // no embed output if unspecified
|
||||||
Type CommandType // defaults to Prefix
|
Type CommandType // defaults to Prefix
|
||||||
Cooldown int // defaults to 0 (no cooldown)
|
Cooldown int // defaults to 0 (no cooldown)
|
||||||
OutputIsReply bool // defaults to false
|
OutputIsReply bool
|
||||||
RequiresMention bool // defaults to false
|
RequiresMention bool
|
||||||
DeleteInput bool // defaults to false
|
DeleteInput bool
|
||||||
DMOnly bool // defaults to false
|
DMOnly bool
|
||||||
AdminOnly bool // defaults to false
|
AdminOnly bool
|
||||||
IgnoreCase bool // defaults to false
|
IgnoreCase bool
|
||||||
// for custom commands that go beyond prints and deletions
|
// for custom commands that go beyond prints and deletions
|
||||||
Function func(*discordgo.Session, *discordgo.MessageCreate)
|
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) {
|
func registerCommand(command Command) {
|
||||||
if command.Trigger == "" {
|
if command.Trigger == "" {
|
||||||
fmt.Println("Cannot register a command with no trigger. Skipping.")
|
fmt.Println("Cannot register a command with no trigger. Skipping.")
|
||||||
@ -48,6 +57,13 @@ func registerCommand(command Command) {
|
|||||||
commands = append(commands, 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) {
|
func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
if m.Author.ID == s.State.User.ID {
|
if m.Author.ID == s.State.User.ID {
|
||||||
log.Printf("<Self> %s", m.Content)
|
log.Printf("<Self> %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) {
|
func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command Command, commandIndex int) {
|
||||||
if (!command.DMOnly || (getChannel(session.State, message.ChannelID).Type == discordgo.ChannelTypeDM)) &&
|
if (!command.DMOnly || (getChannel(session.State, message.ChannelID).Type == discordgo.ChannelTypeDM)) &&
|
||||||
(!command.AdminOnly || (message.Author.ID == config.AdminID)) && !command.IsOnCooldown {
|
(!command.AdminOnly || (message.Author.ID == config.AdminID)) && !command.IsOnCooldown {
|
||||||
@ -130,6 +150,10 @@ func generateReply(message *discordgo.MessageCreate, command Command) string {
|
|||||||
return output
|
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) {
|
func redirectComplaint(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
embed := &discordgo.MessageEmbed {
|
embed := &discordgo.MessageEmbed {
|
||||||
Author: &discordgo.MessageEmbedAuthor{},
|
Author: &discordgo.MessageEmbedAuthor{},
|
||||||
@ -142,3 +166,4 @@ func redirectComplaint(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
func echoMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func echoMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
s.ChannelMessageSend(m.ChannelID, m.Content)
|
s.ChannelMessageSend(m.ChannelID, m.Content)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user