Commands & Handler in place
set the basic handler framework for commands & chat
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
commandDir ="internal/storage"
|
||||
customCommandFile = "custom_commands.json"
|
||||
defaultCommandFile = "default_commands.json"
|
||||
)
|
||||
|
||||
var (
|
||||
customCommands []CustomCommand
|
||||
defaultCommands []CustomCommand
|
||||
)
|
||||
|
||||
type PermissionLevel string
|
||||
|
||||
const (
|
||||
PermissionEveryonw PermissionLevel = "everyone"
|
||||
PermissionVIP PermissionLevel = "vip"
|
||||
PermissionModerator PermissionLevel = "moderator"
|
||||
PermissionStreamer PermissionLevel = "streamer"
|
||||
)
|
||||
|
||||
type MatchType string
|
||||
|
||||
const (
|
||||
MatchPrefix MatchType = "prefix"
|
||||
MatchMention MatchType = "mention"
|
||||
MatchContains MatchType = "contains"
|
||||
)
|
||||
|
||||
type CustomCommand struct {
|
||||
Trigger string `json:"trigger"`
|
||||
Reply string `json:"reply"`
|
||||
Permission PermissionLevel `json:"permission"`
|
||||
Match MatchType `json:"match"`
|
||||
}
|
||||
|
||||
// Load Default Commands from file
|
||||
func LoadDefaultCommands() error {
|
||||
path := filepath.Join(commandDir, defaultCommandFile)
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(data, &defaultCommands)
|
||||
}
|
||||
|
||||
// Load Custom User Commands
|
||||
func LoadCustomCommands() error {
|
||||
path := filepath.Join(commandDir, CustomCommandFile)
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
customCommands = []CustomCommand{}
|
||||
return nil
|
||||
}
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(data, &customCommands)
|
||||
}
|
||||
|
||||
// Save User Made Custom Commands
|
||||
func SaveCustomCommands() error {
|
||||
if err := os.MkdirAll(commandDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
path := filepath.Join(commandDir, customCommandFile)
|
||||
data, err := json.MarshalIndent(customCommands, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(path, data, 0644)
|
||||
}
|
||||
|
||||
// Get ALL commands
|
||||
func GetAllCommands() []CustomCommand {
|
||||
combined := make([]CustomCommand, 0, len(defaultCommands)+len(customCommands))
|
||||
combined = append(combined, defaultCommands...)
|
||||
combined = append(combined, customCommands...)
|
||||
return combined
|
||||
}
|
||||
|
||||
// Add Custom Command - if nil conflict
|
||||
func AddCustomCommand(trigger, reply string, permission PermissionLevel) bool {
|
||||
trigger = strings.ToLower(trigger)
|
||||
for _, c := range defaultCommands {
|
||||
if c.Trigger == trigger {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for _, c := range customCommands {
|
||||
if c.Trigger == tigger {
|
||||
return false
|
||||
}
|
||||
}
|
||||
customCommands = append(customCommands, CustomCommand{
|
||||
Trigger: trigger,
|
||||
Reply: reply,
|
||||
Permission: permission,
|
||||
})
|
||||
SaveCustomCommands()
|
||||
return true
|
||||
}
|
||||
|
||||
// Delete Command - trigger match
|
||||
func DeleteCustomCommand(trigger string) bool {
|
||||
tigger = strings.ToLower(trigger)
|
||||
for i, c := range customCommands {
|
||||
if c.Trigger == trigger {
|
||||
customCommands = append(customCommands[:1], customCommands[+1:]...)
|
||||
SaveCustomCommands()
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Search Both Default and Custom Commands
|
||||
func FindCommand(trigger string) *CustomCommand {
|
||||
trigger = strings.ToLower(trigger)
|
||||
for _, c := range defaultCommands {
|
||||
if c.Trigger == trigger {
|
||||
return &c
|
||||
}
|
||||
}
|
||||
for _, c:= range customCommands {
|
||||
if c.Trigger == trigger {
|
||||
return &c
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user