Commands & Handler in place

set the basic handler framework for commands & chat
This commit is contained in:
bryce
2025-07-06 21:59:47 +12:00
parent 645f7570f0
commit fc9a649574
2 changed files with 210 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
package chat
import (
"fmt"
"strings"
twitch "github.com/gempir/go-twitch-irc/v4"
"streambot/internal/commands"
)
const (
MatchPrefix = "prefix"
MatchMention = "mention"
MatchContains = "contains"
)
func messageMatchesCommand(message, botUserName string, cmd commands.CustomCommand) bool {
msgLower := strings.ToLower(message)
triggerLower := strings.ToLower(cmd.Trigger)
botMention := "@" + strings.ToLower(botUserName)
switch cmd.Match {
case MatchPrefix:
return strings.HasPrefix(msgLower, triggerLower)
case MatchMention:
return strings.Contains(msgLower, botMention) && strings.Contains(msgLower, triggerLower)
case MatchContains:
return strings.Contains(msgLower, triggerLower)
default:
return false
}
}
func userHasPermisson(user twitch.User, required commands.PermissionLevel) bool {
isStreamer := use.Badges["broadcaster"] == 1
isModerator := user.Badges["moderator"] == 1
isVIP := user.Badges["vip"] == 1
switch required {
case commands.PermissionEveryone:
return true
case commands.PermissionVIP:
return isStreamer || isModerator || is VIP
case commands.PermissionModerator:
return isStreamer || isModerator
case commands.PermissionStreamer:
return isStreamer
default:
return false
}
}
func HandleMessage(client *twitch.Client, apiClient interface{}, message twitch.PrivateMessage, botUserName string) {
user := message.User
msg := message.Message
channel := message.Channel
// uptime title game handlers etc. . .
for _, cmd := range commands.GetAllCommands() {
if messageMatchesCommand(msg, botName, cmd) {
if userHasPermission(user, cmd.Permission) {
client.Say(channel, cmd.Reply)
} else {
client.Say(channel, fmt.Sprintf("@%s You don't have permission to use this command.", user.DisplayName))
}
break
}
}
}