Skip to content

Commit 0b3f322

Browse files
committed
Don't pass the channel around, Why am I passing this around?
1 parent 2503902 commit 0b3f322

3 files changed

Lines changed: 30 additions & 36 deletions

File tree

src/Bot.hs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ import Transport
6363

6464
type Bot = InEvent -> Effect ()
6565

66-
builtinCommands :: ChannelName -> CommandTable
67-
builtinCommands channel =
66+
builtinCommands :: CommandTable
67+
builtinCommands =
6868
M.fromList
6969
[ ( "russify"
7070
, mkBuiltinCommand
@@ -114,7 +114,7 @@ builtinCommands channel =
114114
, mkBuiltinCommand
115115
( "Send help"
116116
, $githubLinkLocationStr
117-
, helpCommand $ builtinCommands channel))
117+
, helpCommand builtinCommands))
118118
, ( "poll"
119119
, mkBuiltinCommand
120120
( "Starts a poll. !poll <duration:secs> option1; option2; ...; option3"
@@ -227,14 +227,14 @@ builtinCommands channel =
227227
replyOnNothing "Only for mods" $
228228
regexArgs "([a-zA-Z0-9]+) ?(.*)" $
229229
replyLeft $
230-
pairArgs $ replyLeft $ addCustomCommand $ builtinCommands channel))
230+
pairArgs $ replyLeft $ addCustomCommand builtinCommands))
231231
, ( "delcmd"
232232
, mkBuiltinCommand
233233
( "Delete custom command"
234234
, $githubLinkLocationStr
235235
, authorizeSender senderAuthority $
236236
replyOnNothing "Only for mods" $
237-
deleteCustomCommand $ builtinCommands channel))
237+
deleteCustomCommand builtinCommands))
238238
, ( "updcmd"
239239
, mkBuiltinCommand
240240
( "Update custom command"
@@ -243,7 +243,7 @@ builtinCommands channel =
243243
replyOnNothing "Only for mods" $
244244
regexArgs "([a-zA-Z0-9]+) ?(.*)" $
245245
replyLeft $
246-
pairArgs $ replyLeft $ updateCustomCommand $ builtinCommands channel))
246+
pairArgs $ replyLeft $ updateCustomCommand builtinCommands))
247247
-- TODO(#337): use help instead of !showcmd
248248
, ( "showcmd"
249249
, mkBuiltinCommand
@@ -253,7 +253,7 @@ builtinCommands channel =
253253
replyLeft $
254254
cmapR headMay $
255255
replyOnNothing "Not enough arguments" $
256-
showCustomCommand $ builtinCommands channel))
256+
showCustomCommand builtinCommands))
257257
, ( "timescmd"
258258
, mkBuiltinCommand
259259
( "Show amount of times the custom commands was invoked"
@@ -262,7 +262,7 @@ builtinCommands channel =
262262
replyLeft $
263263
cmapR headMay $
264264
replyOnNothing "Not enough arguments" $
265-
timesCustomCommand $ builtinCommands channel))
265+
timesCustomCommand builtinCommands))
266266
, ( "song"
267267
, mkBuiltinCommand
268268
( "Print currently playing song"
@@ -407,9 +407,7 @@ builtinCommands channel =
407407
( "Suggest video for the friday stream"
408408
, $githubLinkLocationStr
409409
, nonEmptyRoles
410-
[qms|You have to be trusted to use this command.
411-
Subscribe to gain the trust instantly:
412-
https://twitch.tv/{channel'}/subscribe|]
410+
413411
fridayCommand))
414412
, ( "videoq"
415413
, mkBuiltinCommand
@@ -511,14 +509,8 @@ builtinCommands channel =
511509
, mkBuiltinCommand
512510
( "Asciify Twitch, BTTV or FFZ emote"
513511
, $githubLinkLocationStr
514-
, nonEmptyRoles
515-
[qms|You have to be trusted to use this command.
516-
Subscribe to gain the trust instantly:
517-
https://twitch.tv/{channel'}/subscribe|]
518-
asciifyReaction))
512+
, nonEmptyRoles asciifyReaction))
519513
]
520-
where
521-
channel' = unChannel channel
522514

523515
nextStreamCommand :: Reaction Message a
524516
nextStreamCommand =
@@ -636,18 +628,18 @@ mention =
636628
else Nothing <$ msg) $
637629
ignoreNothing markov
638630

639-
bot :: ChannelName -> Bot
640-
bot channel Started = do
631+
bot :: Bot
632+
bot Started = do
641633
startRefreshFridayGistTimer
642-
startRefreshHelpGistTimer $ builtinCommands channel
634+
startRefreshHelpGistTimer builtinCommands
643635
-- TODO(#656): Restarted Twitch transport thread can duplicate timers
644-
bot _ (Joined channel@(TwitchChannel _)) = do
636+
bot (Joined channel@(TwitchChannel _)) = do
645637
startPeriodicCommands channel dispatchCommand
646638
periodicEffect (60 * 1000) (Just channel) (announceRunningPoll channel)
647639
-- TODO(#550): Periodic commands don't work in Discord channels
648-
bot _ (Joined channel@(DiscordChannel _)) =
640+
bot (Joined channel@(DiscordChannel _)) =
649641
periodicEffect (60 * 1000) (Just channel) (announceRunningPoll channel)
650-
bot _ (InMsg msg) =
642+
bot (InMsg msg) =
651643
runReaction
652644
(dupLiftExtractR internalMessageRoles $
653645
copyPastaFilter $ linkFilter messageReaction)
@@ -714,12 +706,12 @@ dispatchCommand message = do
714706
dispatchCustomCommand message
715707

716708
dispatchBuiltinCommand :: Message (Command T.Text) -> Effect ()
717-
dispatchBuiltinCommand message@Message { messageSender = sender
709+
dispatchBuiltinCommand message@Message { messageSender = _
718710
, messageContent = Command { commandName = name
719711
, commandArgs = args
720712
}
721713
} =
722714
maybe
723715
(return ())
724716
(\bc -> runReaction (bcReaction bc) $ fmap (const args) message)
725-
(M.lookup name $ builtinCommands $ channelToName $ senderChannel sender)
717+
(M.lookup name builtinCommands)

src/Bot/Replies.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,21 @@ onlyForRoles reply roles reaction =
7979
onlyForMods :: Reaction Message a -> Reaction Message a
8080
onlyForMods = onlyForRoles "Only for mods" authorityRoles
8181

82-
nonEmptyRoles :: T.Text -> Reaction Message a -> Reaction Message a
83-
nonEmptyRoles reply reaction =
82+
nonEmptyRoles :: Reaction Message a -> Reaction Message a
83+
nonEmptyRoles reaction =
8484
transR duplicate $
8585
ifR
8686
(null . senderRoles . messageSender)
87-
(cmapR (const reply) $ Reaction replyMessage)
87+
(Reaction noTrust)
8888
(cmapR extract reaction)
89+
90+
noTrust :: Message a -> Effect ()
91+
noTrust msg = replyToSender (messageSender msg) reply
92+
where
93+
reply = [qms|You have to be trusted to use this command.
94+
Subscribe to gain the trust instantly:
95+
https://twitch.tv/{channel'}/subscribe|]
96+
channel' = unChannel $ channelToName $ senderChannel $ messageSender msg
8997

9098
onlyForTwitch :: Reaction Message a -> Reaction Message a
9199
onlyForTwitch reaction =

src/Main.hs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import Control.Concurrent.STM
1212
import Control.Monad
1313
import Data.Foldable
1414
import Data.Maybe
15-
import qualified Data.Text as T
1615
import System.Clock
1716
import System.Environment
1817
import Text.InterpolatedString.QM
@@ -35,12 +34,7 @@ eventLoop b prevCPUTime botState = do
3534
logicEntry :: BotState -> IO ()
3635
logicEntry botState = do
3736
currCPUTime <- getTime Monotonic
38-
handleInEvent bot' Started botState >>= eventLoop bot' currCPUTime
39-
where
40-
channelName =
41-
ChannelName $
42-
maybe "tsoding" (T.drop 1 . tcChannel) $ configTwitch $ bsConfig botState
43-
bot' = bot channelName
37+
handleInEvent bot Started botState >>= eventLoop bot currCPUTime
4438

4539
-- TODO(#399): supervisor is vulnerable to errors that happen at the start of the action
4640
supavisah :: Show a => IO a -> IO ()

0 commit comments

Comments
 (0)