You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.6 KiB

3 years ago
3 years ago
3 years ago
  1. {-# LANGUAGE MultiParamTypeClasses #-}
  2. {-# LANGUAGE OverloadedStrings #-}
  3. {-# LANGUAGE RecordWildCards #-}
  4. -- | Manages all about reminders
  5. module Commands.Reminds where
  6. import Conf ( )
  7. import qualified Control.Event as E
  8. import Control.Monad ( void )
  9. import Control.Monad.IO.Unlift ( MonadUnliftIO(withRunInIO) )
  10. import Data.Convertible ( ConvertResult
  11. , Convertible(safeConvert)
  12. )
  13. import qualified Data.Text as T
  14. import Data.Time ( UTCTime )
  15. import qualified Database.HDBC as DB
  16. import qualified Database.HDBC.Sqlite3 as DB.SQ3
  17. import Discord ( DiscordHandler
  18. , restCall
  19. )
  20. import Discord.Interactions ( )
  21. import Discord.Requests ( ChannelRequest(CreateMessage)
  22. )
  23. import Discord.Types ( Snowflake(..) )
  24. data Remind = Remind
  25. { rmdUser :: Snowflake
  26. , rmdChannel :: Snowflake
  27. , rmdMessage :: T.Text
  28. , rmdDatetime :: UTCTime
  29. }
  30. instance Convertible Snowflake DB.SqlValue where
  31. safeConvert (Snowflake v) = safeConvert v
  32. setupRemindDb :: IO ()
  33. setupRemindDb = do
  34. conn <- DB.SQ3.connectSqlite3 "db.sqlite3"
  35. DB.run
  36. conn
  37. "CREATE TABLE IF NOT EXISTS reminds\
  38. \(id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL,\
  39. \user INTEGER NOT NULL,\
  40. \channel INTEGER NOT NULL,\
  41. \message INTEGER NOT NULL,\
  42. \dt DATETIME);"
  43. []
  44. DB.commit conn
  45. DB.disconnect conn
  46. scheduleRemind :: E.EventSystem -> Remind -> DiscordHandler ()
  47. scheduleRemind ev Remind {..} = do
  48. void $ withRunInIO $ \runInIo ->
  49. E.addEvent ev rmdDatetime
  50. $ void
  51. $ runInIo
  52. $ restCall
  53. $ CreateMessage rmdChannel
  54. $ "<@"
  55. <> T.pack (show rmdUser)
  56. <> "> **Reminder**\n"
  57. <> rmdMessage
  58. registerRemind :: Remind -> IO ()
  59. registerRemind Remind {..} = do
  60. conn <- DB.SQ3.connectSqlite3 "db.sqlite3"
  61. DB.run
  62. conn
  63. "INSERT INTO reminds(user, channel, message, dt)\
  64. \VALUES (?,?,?,?)"
  65. [ DB.toSql rmdUser
  66. , DB.toSql rmdChannel
  67. , DB.toSql rmdMessage
  68. , DB.toSql rmdDatetime
  69. ]
  70. DB.commit conn
  71. DB.disconnect conn