From a72d52d68500ad3bf1eef2f01665e727d6c72d51 Mon Sep 17 00:00:00 2001 From: Ash Keel Date: Tue, 30 Nov 2021 17:37:33 +0100 Subject: [PATCH] Add subscriber ACL level and make them work on bot commands --- frontend/src/locale/en/translation.json | 1 + frontend/src/store/api/types.ts | 7 ++++++- frontend/src/ui/pages/twitch/Commands.tsx | 20 +++++++++++++++--- modules/twitch/bot.go | 24 ++++++++++++++++++++++ modules/twitch/commands.go | 25 +++++++++++++++++++---- 5 files changed, 69 insertions(+), 8 deletions(-) diff --git a/frontend/src/locale/en/translation.json b/frontend/src/locale/en/translation.json index 659dbc7..624bb21 100644 --- a/frontend/src/locale/en/translation.json +++ b/frontend/src/locale/en/translation.json @@ -201,6 +201,7 @@ "description-help": "What does this command do?", "access-level": "Access level", "access-everyone": "Everyone", + "access-subscribers": "Subscribers", "access-vips": "VIPs", "access-moderators": "Moderators", "access-streamer": "Streamer only", diff --git a/frontend/src/store/api/types.ts b/frontend/src/store/api/types.ts index c3bad03..6c4f4e6 100644 --- a/frontend/src/store/api/types.ts +++ b/frontend/src/store/api/types.ts @@ -35,7 +35,12 @@ interface TwitchModulesConfig { enable_timers: boolean; } -type AccessLevelType = 'everyone' | 'vip' | 'moderators' | 'streamer'; +export type AccessLevelType = + | 'everyone' + | 'subscribers' + | 'vip' + | 'moderators' + | 'streamer'; export interface TwitchBotCustomCommand { description: string; diff --git a/frontend/src/ui/pages/twitch/Commands.tsx b/frontend/src/ui/pages/twitch/Commands.tsx index 72644e3..8cc53b9 100644 --- a/frontend/src/ui/pages/twitch/Commands.tsx +++ b/frontend/src/ui/pages/twitch/Commands.tsx @@ -5,7 +5,10 @@ import { useDispatch } from 'react-redux'; import { useModule } from '../../../lib/react-utils'; import { modules } from '../../../store/api/reducer'; import Modal from '../../components/Modal'; -import { TwitchBotCustomCommand } from '../../../store/api/types'; +import { + AccessLevelType, + TwitchBotCustomCommand, +} from '../../../store/api/types'; import Field from '../../components/Field'; interface CommandItemProps { @@ -92,6 +95,9 @@ function CommandModal({ const [description, setDescription] = useState( initialData?.description ?? '', ); + const [accessLevel, setAccessLevel] = useState( + initialData?.access_level ?? 'everyone', + ); const [response, setResponse] = useState(initialData?.response ?? ''); const { t } = useTranslation(); @@ -105,7 +111,7 @@ function CommandModal({ description, response, enabled: initialData?.enabled ?? false, - access_level: 'everyone', + access_level: accessLevel, }); } }; @@ -171,10 +177,18 @@ function CommandModal({

- + setAccessLevel(ev.target.value as AccessLevelType) + } + > + diff --git a/modules/twitch/bot.go b/modules/twitch/bot.go index 704c0a2..3c59db8 100644 --- a/modules/twitch/bot.go +++ b/modules/twitch/bot.go @@ -205,3 +205,27 @@ func (b *Bot) Connect() error { func (b *Bot) WriteMessage(message string) { b.Client.Say(b.config.Channel, message) } + +func getUserAccessLevel(user irc.User) AccessLevelType { + // Check broadcaster + if _, ok := user.Badges["broadcaster"]; ok { + return ALTStreamer + } + + // Check mods + if _, ok := user.Badges["moderator"]; ok { + return ALTModerators + } + + // Check VIP + if _, ok := user.Badges["vip"]; ok { + return ALTVIP + } + + // Check subscribers + if _, ok := user.Badges["subscriber"]; ok { + return ALTSubscribers + } + + return ALTEveryone +} diff --git a/modules/twitch/commands.go b/modules/twitch/commands.go index e7e2c3e..8300886 100644 --- a/modules/twitch/commands.go +++ b/modules/twitch/commands.go @@ -14,12 +14,21 @@ import ( type AccessLevelType string const ( - ALTEveryone AccessLevelType = "everyone" - ALTVIP AccessLevelType = "vip" - ALTModerators AccessLevelType = "moderators" - ALTStreamer AccessLevelType = "streamer" + ALTEveryone AccessLevelType = "everyone" + ALTSubscribers AccessLevelType = "subscriber" + ALTVIP AccessLevelType = "vip" + ALTModerators AccessLevelType = "moderators" + ALTStreamer AccessLevelType = "streamer" ) +var accessLevels = map[AccessLevelType]int{ + ALTEveryone: 0, + ALTSubscribers: 1, + ALTVIP: 2, + ALTModerators: 3, + ALTStreamer: 999, +} + type BotCommandHandler func(bot *Bot, message irc.PrivateMessage) type BotCommand struct { @@ -31,6 +40,14 @@ type BotCommand struct { } func cmdCustom(bot *Bot, cmd string, data BotCustomCommand, message irc.PrivateMessage) { + // Check access level + accessLevel := getUserAccessLevel(message.User) + + // Ensure that access level is high enough + if accessLevels[accessLevel] < accessLevels[data.AccessLevel] { + return + } + // Add future logic (like counters etc.) here, for now it's just fixed messages var buf bytes.Buffer err := bot.customTemplates[cmd].Execute(&buf, message)