forked from kageru/discord-selphybot
refactor, breaking: added wrapper to easily create text commands
parent
93014d944b
commit
a7d6298746
@ -0,0 +1,96 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"strings"
|
||||
"log"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type CommandType int
|
||||
|
||||
const (
|
||||
CommandTypePrefix CommandType = 0
|
||||
CommandTypeFullMatch CommandType = 1
|
||||
CommandTypeRegex CommandType = 2
|
||||
)
|
||||
|
||||
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
|
||||
OutputIsReply bool // defaults to false
|
||||
DeleteInput bool // defaults to false
|
||||
DMOnly bool // defaults to false
|
||||
AdminOnly bool // defaults to false
|
||||
IsEmbed bool // defaults to false
|
||||
// for custom commands that go beyond prints and deletions
|
||||
Function func(*discordgo.Session, *discordgo.MessageCreate)
|
||||
}
|
||||
|
||||
|
||||
func registerCommand(command Command) {
|
||||
if command.Trigger == "" {
|
||||
fmt.Println("Cannot register a command with no trigger. Skipping.")
|
||||
return
|
||||
}
|
||||
commands = append(commands, command)
|
||||
}
|
||||
|
||||
func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
log.Printf("<Self> %s", m.Content)
|
||||
return
|
||||
}
|
||||
for _, command := range commands {
|
||||
switch command.Type {
|
||||
case CommandTypePrefix:
|
||||
if strings.HasPrefix(m.Content, command.Trigger) {
|
||||
executeCommand(s, m, command)
|
||||
return
|
||||
}
|
||||
case CommandTypeFullMatch:
|
||||
if m.Content == command.Trigger {
|
||||
executeCommand(s, m, command)
|
||||
return
|
||||
}
|
||||
case CommandTypeRegex:
|
||||
match, _ := regexp.MatchString(command.Trigger, m.Content)
|
||||
if match {
|
||||
executeCommand(s, m, command)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command Command) {
|
||||
if (!command.DMOnly || (getChannel(session.State, message.ChannelID).Type == discordgo.ChannelTypeDM)) &&
|
||||
(!command.AdminOnly || (message.Author.ID == config.AdminID)) {
|
||||
if command.Function == nil {
|
||||
// simple reply
|
||||
if command.OutputEmbed == nil {
|
||||
messageContent := generateReply(message, command)
|
||||
session.ChannelMessageSend(message.ChannelID, messageContent)
|
||||
} else {
|
||||
session.ChannelMessageSendEmbed(message.ChannelID, command.OutputEmbed)
|
||||
}
|
||||
if command.DeleteInput {
|
||||
session.ChannelMessageDelete(message.ChannelID, message.ID)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.Printf("Denied command %s to user %s.", command.Trigger, userToString(message.Author))
|
||||
}
|
||||
}
|
||||
|
||||
func generateReply(message *discordgo.MessageCreate, command Command) string {
|
||||
output := command.Output
|
||||
if command.OutputIsReply {
|
||||
output = fmt.Sprintf(output, message.Author.ID)
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"log"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func unlockUser(s *discordgo.Session, id string) {
|
||||
s.GuildMemberRoleRemove(config.ServerID, id, config.LockedRoleID)
|
||||
log.Printf("Removed lock from user: %s", userToString(getUser(s, id)))
|
||||
}
|
||||
|
||||
func userToString(u *discordgo.User) string {
|
||||
return fmt.Sprintf("%s#%s (ID: %s)", u.Username, u.Discriminator, u.ID)
|
||||
}
|
||||
|
||||
func roleName(s *discordgo.State, rid string) string {
|
||||
role, _ := s.Role(config.ServerID, rid)
|
||||
return role.Name
|
||||
}
|
||||
|
||||
func channelToString(c *discordgo.Channel) string {
|
||||
return fmt.Sprintf("%s (ID: %s) on %s", c.Name, c.ID, c.GuildID)
|
||||
}
|
||||
|
||||
func messageToString(m *discordgo.Message) string {
|
||||
return fmt.Sprintf("<%s#%s>: %s", m.Author.Username, m.Author.Discriminator, m.Content)
|
||||
}
|
||||
|
||||
func getChannel(s *discordgo.State, cid string) *discordgo.Channel {
|
||||
channel, _ := s.Channel(cid)
|
||||
return channel
|
||||
}
|
||||
|
||||
func getUser(s *discordgo.Session, uid string) *discordgo.User {
|
||||
user, _ := s.User(uid)
|
||||
return user
|
||||
}
|
||||
|
@ -1,20 +0,0 @@
|
||||
package main
|
||||
|
||||
type CommandType int
|
||||
|
||||
const (
|
||||
CommandTypePrefix CommandType = 0
|
||||
CommandTypeFullMatch CommandType = 1
|
||||
CommandTypeRegex CommandType = 2
|
||||
)
|
||||
|
||||
type Command struct {
|
||||
Input string
|
||||
Output string
|
||||
Type CommandType
|
||||
OutputIsReply bool
|
||||
DeleteInput bool
|
||||
DMOnly bool
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func getWelcomeEmbed() *discordgo.MessageEmbed {
|
||||
return &discordgo.MessageEmbed {
|
||||
Author: &discordgo.MessageEmbedAuthor{},
|
||||
Color: 0xffb90f,
|
||||
Description: config.WelcomeEmbed.Message,
|
||||
Fields: []*discordgo.MessageEmbedField {
|
||||
&discordgo.MessageEmbedField {
|
||||
Name: config.WelcomeEmbed.QuestionsTitle,
|
||||
Value: config.WelcomeEmbed.QuestionsText,
|
||||
Inline: true,
|
||||
},
|
||||
&discordgo.MessageEmbedField {
|
||||
Name: config.WelcomeEmbed.BugsTitle,
|
||||
Value: fmt.Sprintf(config.WelcomeEmbed.BugsText, config.AdminID),
|
||||
Inline: true,
|
||||
},
|
||||
},
|
||||
Thumbnail: &discordgo.MessageEmbedThumbnail{
|
||||
URL: config.WelcomeEmbed.Image,
|
||||
},
|
||||
}
|
||||
}
|
Loading…
Reference in new issue