Skip to content

Commit 2fdc90b

Browse files
committed
Introduce NoTrustReply
1 parent a6189cb commit 2fdc90b

4 files changed

Lines changed: 49 additions & 24 deletions

File tree

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 = channel
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/{unChannel $ channelToName channel}/subscribe|]
142+
reply <- entityPayload <$> noTrustReply
143+
replyToSender sender $ noTrustLinkReply reply
149144
msg -> runReaction reaction msg)

src/Bot/Replies.hs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import Reaction
1414
import Regexp
1515
import Text.InterpolatedString.QM
1616
import Transport
17+
import Entity
18+
import Data.Proxy
19+
import Property
20+
import Data.Maybe
1721

1822
sayMessage :: Reaction Message T.Text
1923
sayMessage =
@@ -87,14 +91,39 @@ nonEmptyRoles reaction =
8791
(Reaction noTrust)
8892
(cmapR extract reaction)
8993

94+
data NoTrustReply = NoTrustReply
95+
{ noTrustCommandReply :: T.Text
96+
, noTrustLinkReply :: T.Text
97+
} deriving (Eq)
98+
99+
instance IsEntity NoTrustReply where
100+
nameOfEntity Proxy = "NoTrustReply"
101+
toProperties reply =
102+
M.fromList
103+
[ ("command", PropertyText $ noTrustCommandReply reply)
104+
, ("link", PropertyText $ noTrustLinkReply reply)
105+
]
106+
fromProperties properties =
107+
NoTrustReply <$> extractProperty "command" properties <*>
108+
extractProperty "link" properties
109+
110+
noTrustReply :: Effect (Entity NoTrustReply)
111+
noTrustReply = do
112+
reply <- listToMaybe <$> selectEntities Proxy (Take 1 $ All)
113+
case reply of
114+
Just reply' -> return reply'
115+
Nothing ->
116+
createEntity Proxy $
117+
NoTrustReply
118+
[qms|You have to be trusted to use this command.
119+
Mods can change this message with !TODO|]
120+
[qms|You have to be trusted to send links.
121+
Mods can change this message with !TODO|]
122+
90123
noTrust :: Message a -> Effect ()
91-
noTrust msg = replyToSender (messageSender msg) reply
92-
where
93-
reply =
94-
[qms|You have to be trusted to use this command.
95-
Subscribe to gain the trust instantly:
96-
https://twitch.tv/{channel'}/subscribe|]
97-
channel' = unChannel $ channelToName $ senderChannel $ messageSender msg
124+
noTrust Message {messageSender = sender} = do
125+
reply <- entityPayload <$> noTrustReply
126+
replyToSender sender $ noTrustCommandReply reply
98127

99128
onlyForTwitch :: Reaction Message a -> Reaction Message a
100129
onlyForTwitch reaction =

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: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,6 @@ paidRoles = [tsodingTwitchedDiscordRole, TwitchSub]
7171
senderAuthority :: Sender -> Bool
7272
senderAuthority sender = any (`elem` senderRoles sender) authorityRoles
7373

74-
channelToName :: Channel -> ChannelName
75-
channelToName (DiscordChannel x) = ChannelName $ T.pack $ show x
76-
channelToName (TwitchChannel (T.uncons -> Just ('#', x))) = ChannelName x
77-
channelToName (TwitchChannel _) = ChannelName ""
78-
79-
newtype ChannelName = ChannelName
80-
{ unChannel :: T.Text
81-
} deriving Eq
82-
8374
data InEvent
8475
= Started
8576
| Joined Channel

0 commit comments

Comments
 (0)