Advertisement
NathanWaltz

Discord Bot

Jun 3rd, 2021
1,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Main where
  2.  
  3. import Calamity
  4.     ( react,
  5.       runBotIO,
  6.       defaultIntents,
  7.       invoke,
  8.       EventType(MessageCreateEvt),
  9.       ChannelRequest(CreateReaction, CreateMessage),
  10.       RawEmoji(UnicodeEmoji),
  11.       Token(BotToken) )
  12. import Calamity.Cache.InMemory ( runCacheInMemory )
  13. import Calamity.Commands ( useConstantPrefix )
  14. import Calamity.Metrics.Noop ( runMetricsNoop )
  15. import Control.Lens ((^.))
  16. import Control.Monad ( void, when )
  17. import Data.Default ()
  18. import Data.Generics.Labels ()
  19. import Data.Maybe ()
  20. import Data.Text.Lazy (Text)
  21. import Data.Text.Lazy.Encoding ( decodeUtf8 )
  22. import qualified Data.Text.Lazy as T
  23. import qualified Di
  24. import DiPolysemy ( info, runDiToIO )
  25. import qualified Polysemy as P
  26. import System.Environment (getEnv)
  27. import TextShow ()
  28. import Di ( new, info )
  29. import qualified Data.ByteString.Lazy.Char8 as L
  30. import qualified Data.Set as Set
  31.  
  32. -- Get extract the words from a message
  33. getMessageWords :: Text -> [Text]
  34. getMessageWords = T.split (== ' ')
  35.  
  36. -- Check if any elements of a message are a swear word
  37. anySwearWord :: Text -> Set.Set Text -> Bool
  38. anySwearWord msg = listContainsSwearWord (getMessageWords msg)
  39.   where
  40.     listContainsSwearWord wordlist swearWords = any ((`isSwearWord` swearWords) . T.toUpper) wordlist
  41.     isSwearWord = Set.member
  42.  
  43. main :: IO ()
  44. main = do
  45.   -- get set of swear words from file
  46.   naughtyDict <- Set.fromList . map (T.toUpper . decodeUtf8) . L.words <$> L.readFile "assets/google_curse_wordlist.txt"
  47.   -- get Token env variable
  48.   token <- T.pack <$> getEnv "DISCORD"
  49.  
  50.   -- do bot things
  51.   Di.new $ \di ->
  52.     void
  53.       . P.runFinal
  54.       . P.embedToFinal @IO
  55.       . runDiToIO di
  56.       . runCacheInMemory
  57.       . runMetricsNoop
  58.       . useConstantPrefix "!"
  59.       . runBotIO (BotToken token) defaultIntents
  60.       $ do
  61.         DiPolysemy.info @Text "Connection successful!"
  62.  
  63.         -- show the haskell pride
  64.         react @'MessageCreateEvt $ \msg -> do
  65.          when ("HASKELL" `T.isInfixOf` T.toUpper (msg ^. #content) && not (anySwearWord (msg ^. #content) naughtyDict)) $
  66.            void . invoke $ CreateReaction msg msg (UnicodeEmoji "😄")
  67.  
  68.        -- discourage cursing
  69.        react @'MessageCreateEvt $ \msg -> do
  70.           when (anySwearWord (msg ^. #content) naughtyDict) $
  71.             void . invoke $ CreateReaction msg msg (UnicodeEmoji "😡")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement