Skip to content

Commit 6782cd6

Browse files
authored
Merge pull request #805 from jappeace/really-upstream
Replace hardcoded channel with configured channel in link filters
2 parents bd73fab + aa32405 commit 6782cd6

File tree

5 files changed

+93
-24
lines changed

5 files changed

+93
-24
lines changed

src/Bot.hs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,7 @@ builtinCommands =
402402
, mkBuiltinCommand
403403
( "Suggest video for the friday stream"
404404
, $githubLinkLocationStr
405-
, nonEmptyRoles
406-
[qms|You have to be trusted to use this command.
407-
Subscribe to gain the trust instantly:
408-
https://twitch.tv/tsoding/subscribe|]
409-
fridayCommand))
405+
, nonEmptyRoles fridayCommand))
410406
, ( "videoq"
411407
, mkBuiltinCommand
412408
( "Get the link to the current Friday Queue"
@@ -484,6 +480,11 @@ builtinCommands =
484480
[ ( "help"
485481
, subcommand
486482
[("setgist", setHelpGistId), ("refresh", refreshHelpGistId)])
483+
, ( "reply"
484+
, subcommand
485+
[ ("link", setNoTrustLinkReplyCommand)
486+
, ("command", setNoTrustCommandReplyCommand)
487+
])
487488
]))
488489
, ( "version"
489490
, mkBuiltinCommand
@@ -507,11 +508,7 @@ builtinCommands =
507508
, mkBuiltinCommand
508509
( "Asciify Twitch, BTTV or FFZ emote"
509510
, $githubLinkLocationStr
510-
, nonEmptyRoles
511-
[qms|You have to be trusted to use this command.
512-
Subscribe to gain the trust instantly:
513-
https://twitch.tv/tsoding/subscribe|]
514-
asciifyReaction))
511+
, nonEmptyRoles asciifyReaction))
515512
]
516513

517514
nextStreamCommand :: Reaction Message a
@@ -708,9 +705,11 @@ dispatchCommand message = do
708705
dispatchCustomCommand message
709706

710707
dispatchBuiltinCommand :: Message (Command T.Text) -> Effect ()
711-
dispatchBuiltinCommand message@Message {messageContent = Command { commandName = name
712-
, commandArgs = args
713-
}} =
708+
dispatchBuiltinCommand message@Message { messageSender = _
709+
, messageContent = Command { commandName = name
710+
, commandArgs = args
711+
}
712+
} =
714713
maybe
715714
(return ())
716715
(\bc -> runReaction (bcReaction bc) $ fmap (const args) message)

src/Bot/Links.hs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,10 @@ linkFilter reaction =
135135
Reaction
136136
(\case
137137
Message { messageContent = message
138-
, messageSender = sender@Sender { senderRoles = []
139-
, senderChannel = TwitchChannel _
140-
}
138+
, messageSender = sender@Sender {senderRoles = []}
141139
}
142140
| textContainsLink message -> do
143141
timeoutSender 1 sender
144-
replyToSender
145-
sender
146-
[qms|Links are not allowed for untrusted users.
147-
Subscribe to gain the trust instantly:
148-
https://twitch.tv/tsoding/subscribe|]
142+
reply <- entityPayload <$> noTrustReply
143+
replyToSender sender $ noTrustLinkReply reply
149144
msg -> runReaction reaction msg)

src/Bot/Replies.hs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ module Bot.Replies where
55

66
import Data.Aeson
77
import qualified Data.ByteString.Lazy as BS
8+
import Data.Functor
89
import qualified Data.Map as M
10+
import Data.Maybe
11+
import Data.Proxy
912
import qualified Data.Text as T
1013
import Effect
14+
import Entity
1115
import HyperNerd.Comonad
1216
import Network.HTTP.Simple (getResponseBody, parseRequest)
17+
import Property
1318
import Reaction
1419
import Regexp
1520
import Text.InterpolatedString.QM
@@ -79,14 +84,73 @@ onlyForRoles reply roles reaction =
7984
onlyForMods :: Reaction Message a -> Reaction Message a
8085
onlyForMods = onlyForRoles "Only for mods" authorityRoles
8186

82-
nonEmptyRoles :: T.Text -> Reaction Message a -> Reaction Message a
83-
nonEmptyRoles reply reaction =
87+
nonEmptyRoles :: Reaction Message a -> Reaction Message a
88+
nonEmptyRoles reaction =
8489
transR duplicate $
8590
ifR
8691
(null . senderRoles . messageSender)
87-
(cmapR (const reply) $ Reaction replyMessage)
92+
(Reaction noTrust)
8893
(cmapR extract reaction)
8994

95+
data NoTrustReply = NoTrustReply
96+
{ noTrustCommandReply :: T.Text
97+
, noTrustLinkReply :: T.Text
98+
} deriving (Eq)
99+
100+
updateNoTrustCommandReply :: T.Text -> NoTrustReply -> NoTrustReply
101+
updateNoTrustCommandReply value reply = reply {noTrustCommandReply = value}
102+
103+
updateNoTrustLinkReply :: T.Text -> NoTrustReply -> NoTrustReply
104+
updateNoTrustLinkReply value reply = reply {noTrustLinkReply = value}
105+
106+
instance IsEntity NoTrustReply where
107+
nameOfEntity Proxy = "NoTrustReply"
108+
toProperties reply =
109+
M.fromList
110+
[ ("command", PropertyText $ noTrustCommandReply reply)
111+
, ("link", PropertyText $ noTrustLinkReply reply)
112+
]
113+
fromProperties properties =
114+
NoTrustReply <$> extractProperty "command" properties <*>
115+
extractProperty "link" properties
116+
117+
setNoTrustLinkReplyCommand :: Reaction Message T.Text
118+
setNoTrustLinkReplyCommand =
119+
liftR
120+
(\msg -> do
121+
reply <- noTrustReply
122+
void $ updateEntityById $ fmap (updateNoTrustLinkReply msg) reply) $
123+
cmapR (const "Updated not trust link reply message") $ Reaction replyMessage
124+
125+
setNoTrustCommandReplyCommand :: Reaction Message T.Text
126+
setNoTrustCommandReplyCommand =
127+
liftR
128+
(\msg -> do
129+
reply <- noTrustReply
130+
void $ updateEntityById $ fmap (updateNoTrustCommandReply msg) reply) $
131+
cmapR (const "Updated not trust command reply message") $
132+
Reaction replyMessage
133+
134+
noTrustReply :: Effect (Entity NoTrustReply)
135+
noTrustReply = do
136+
reply <- listToMaybe <$> selectEntities Proxy (Take 1 All)
137+
case reply of
138+
Just reply' -> return reply'
139+
Nothing ->
140+
createEntity Proxy $
141+
NoTrustReply
142+
[qms|You have to be trusted to use this command.
143+
Mods can change this message with
144+
!config reply command <message>|]
145+
[qms|You have to be trusted to send links.
146+
Mods can change this message with
147+
!config reply link <message>|]
148+
149+
noTrust :: Message a -> Effect ()
150+
noTrust Message {messageSender = sender} = do
151+
reply <- entityPayload <$> noTrustReply
152+
replyToSender sender $ noTrustCommandReply reply
153+
90154
onlyForTwitch :: Reaction Message a -> Reaction Message a
91155
onlyForTwitch reaction =
92156
Reaction $ \msg ->

src/Sqlite/EntityPersistence.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ selectEntityIds conn name (Take n (Filter (PropertyEquals propertyName property)
292292
, ":propertyUTCTime" := (fromProperty property :: Maybe UTCTime)
293293
, ":n" := n
294294
]
295+
selectEntityIds conn name (Take n All) =
296+
map fromOnly <$>
297+
queryNamed
298+
conn
299+
[r| SELECT entityId
300+
FROM EntityProperty
301+
WHERE entityName = :entityName
302+
GROUP BY entityId
303+
LIMIT :n |]
304+
[":entityName" := name, ":n" := n]
295305
selectEntityIds conn name (Shuffle All) =
296306
map fromOnly <$>
297307
queryNamed

src/Transport.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE DeriveFunctor #-}
2+
{-# LANGUAGE OverloadedStrings #-}
23

34
module Transport where
45

0 commit comments

Comments
 (0)