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 { Triggers []string `json:"triggers"` 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.Triggers == trigger { return false } } for _, c := range customCommands { if c.Triggers == trigger { 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.Triggers == 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.Triggers == trigger { return &c } } for _, c:= range customCommands { if c.Triggers == trigger { return &c } } return nil }