• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

discord-haskell/discord-haskell: Haskell library for writing Discord bots

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

discord-haskell/discord-haskell

开源软件地址(OpenSource Url):

https://github.com/discord-haskell/discord-haskell

开源编程语言(OpenSource Language):

Haskell 100.0%

开源软件介绍(OpenSource Introduction):

discord-haskell CI Status Hackage version Discord server

Build that discord bot in Haskell! Also checkout the calamity haskell library for a more advanced interface.

Documentation

[installing] [debugging] [creating-bot]

[app-commands] [components] [cache] [embeds] [emoji] [intents] [voice]

[design] [contributing] [todo]

Example

This is an example bot that replies "pong" to messages that start with "ping". Checkout the other examples for things like state management.

{-# LANGUAGE OverloadedStrings #-}  -- allows "string literals" to be Text
import           Control.Monad (when, void)
import           UnliftIO.Concurrent
import           Data.Text (isPrefixOf, toLower, Text)
import qualified Data.Text.IO as TIO

import           Discord
import           Discord.Types
import qualified Discord.Requests as R

-- | Replies "pong" to every message that starts with "ping"
pingpongExample :: IO ()
pingpongExample = do
    userFacingError <- runDiscord $ def
             { discordToken = "Bot ZZZZZZZZZZZZZZZZZZZ"
             , discordOnEvent = eventHandler
             , discordOnLog = \s -> TIO.putStrLn s >> TIO.putStrLn ""
             } -- if you see OnLog error, post in the discord / open an issue

    TIO.putStrLn userFacingError
    -- userFacingError is an unrecoverable error
    -- put normal 'cleanup' code in discordOnEnd (see examples)

eventHandler :: Event -> DiscordHandler ()
eventHandler event = case event of
    MessageCreate m -> when (isPing m && not (fromBot m)) $ do
        void $ restCall (R.CreateReaction (messageChannelId m, messageId m) "eyes")
        threadDelay (2 * 10^6)
        void $ restCall (R.CreateMessage (messageChannelId m) "Pong!")
    _ -> return ()

fromBot :: Message -> Bool
fromBot = userIsBot . messageAuthor

isPing :: Message -> Bool
isPing = ("ping" `isPrefixOf`) . toLower . messageContent

Discord Server

Ask questions, get updates, request features, etc in the project discord server: https://discord.gg/eaRAGgX3bK

Official Discord Documentation

This api closley matches the official discord documentation, which lists the rest requests, gateway events, and gateway sendables.

You can use the docs to check the name of something you want to do. For example: the docs list a Get Channel API path, which translates to discord-haskell's rest request ADT for GetChannel of type ChannelId -> ChannelRequest Channel.

Open an Issue

If something goes wrong: check the error message (optional: check the debugging logs), make sure you have the most recent version, ask on discord, or open a github issue.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap