Sign in

    discord

    Discord REST API client for Cloudflare-style bots written in MoonBit

    Download zip
    Author
    Version
    0.1.2
    License
    Apache-2.0
    Last updated
    6 months ago
    Downloads
    48

    #discord

    MoonBit implementation of the mizchi/discord-cf REST/API layer.

    #Features

    • Discord API v10 REST client
    • API modules: channels, users, guilds, webhooks, interactions, voice
    • Configurable auth mode (Bot token, explicit token, no auth)
    • JS target fetch backend
    • mizchi/cloudflare integration (mizchi/discord/cfw)

    #Quick Start

    ///|
    test {
    let rest = new_rest_client(token="DISCORD_BOT_TOKEN")
    let api = new_api(rest)

    // Prepare and inspect request details before calling API
    let prepared = rest.prepare_request(
    request_method_get(),
    route_channel_messages("1234567890"),
    request_options(query=[("limit", "10")]),
    )

    inspect(
    prepared.url,
    content="https://discord.com/api/v10/channels/1234567890/messages?limit=10",
    )

    // API modules are available from new_api(rest)
    inspect(api.users.rest.options.timeout_ms, content="15000")
    }

    #Commands

    just check just test just info

    #Cloudflare Worker Integration

    src/cfw exports get_discord_handler.

    moon build src/cfw --target js npx wrangler dev --config fixtures/wrangler.jsonc

    Handler routes:

    • GET /health
    • GET /discord/send?channel_id=...&content=...
      • if channel_id is omitted, DISCORD_CHANNEL_ID is used

    Required environment variables:

    • DISCORD_TOKEN (required)
    • DISCORD_CHANNEL_ID (optional)

    fixtures/cf-worker.js loads target/js/release/build/cfw/cfw.js and wires get_discord_handler() to Worker fetch.

    API

    pub struct API {
    channels : ChannelsAPI
    interactions : InteractionsAPI
    webhooks : WebhooksAPI
    users : UsersAPI
    guilds : GuildsAPI
    voice : VoiceAPI
    }

    API::new

    fn API::new(rest : RestClient) -> API

    AuthMode

    pub enum AuthMode {
    UseClientToken
    UseToken(String)
    SkipAuth
    } derive(Eq, Show)

    ChannelsAPI

    pub struct ChannelsAPI {
    rest : RestClient
    }

    ChannelsAPI::create_message

    async fn ChannelsAPI::create_message(self : ChannelsAPI, channel_id : String, body : Json, auth_mode? : AuthMode) -> Result[Json, RestError]

    ChannelsAPI::create_reaction

    async fn ChannelsAPI::create_reaction(self : ChannelsAPI, channel_id : String, message_id : String, emoji : String) -> Result[Json, RestError]

    ChannelsAPI::delete_message

    async fn ChannelsAPI::delete_message(self : ChannelsAPI, channel_id : String, message_id : String) -> Result[Json, RestError]

    ChannelsAPI::delete_own_reaction

    async fn ChannelsAPI::delete_own_reaction(self : ChannelsAPI, channel_id : String, message_id : String, emoji : String) -> Result[Json, RestError]

    ChannelsAPI::edit_message

    async fn ChannelsAPI::edit_message(self : ChannelsAPI, channel_id : String, message_id : String, body : Json, auth_mode? : AuthMode) -> Result[Json, RestError]

    ChannelsAPI::get

    async fn ChannelsAPI::get(self : ChannelsAPI, channel_id : String) -> Result[Json, RestError]

    ChannelsAPI::get_message

    async fn ChannelsAPI::get_message(self : ChannelsAPI, channel_id : String, message_id : String) -> Result[Json, RestError]

    ChannelsAPI::get_messages

    async fn ChannelsAPI::get_messages(self : ChannelsAPI, channel_id : String, query? : Array[(String, String)]) -> Result[Json, RestError]

    ChannelsAPI::new

    ChannelsAPI::trigger_typing_indicator

    async fn ChannelsAPI::trigger_typing_indicator(self : ChannelsAPI, channel_id : String) -> Result[Json, RestError]

    FetchResponse

    pub(all) struct FetchResponse {
    ok : Bool
    status : Int
    content_type : String?
    body : String
    }

    GuildsAPI

    pub struct GuildsAPI {
    rest : RestClient
    }

    GuildsAPI::create_channel

    async fn GuildsAPI::create_channel(self : GuildsAPI, guild_id : String, body : Json) -> Result[Json, RestError]

    GuildsAPI::create_role

    async fn GuildsAPI::create_role(self : GuildsAPI, guild_id : String, body : Json) -> Result[Json, RestError]

    GuildsAPI::delete

    async fn GuildsAPI::delete(self : GuildsAPI, guild_id : String) -> Result[Json, RestError]

    GuildsAPI::delete_role

    async fn GuildsAPI::delete_role(self : GuildsAPI, guild_id : String, role_id : String) -> Result[Json, RestError]

    GuildsAPI::edit

    async fn GuildsAPI::edit(self : GuildsAPI, guild_id : String, body : Json) -> Result[Json, RestError]

    GuildsAPI::edit_role

    async fn GuildsAPI::edit_role(self : GuildsAPI, guild_id : String, role_id : String, body : Json) -> Result[Json, RestError]

    GuildsAPI::get

    async fn GuildsAPI::get(self : GuildsAPI, guild_id : String) -> Result[Json, RestError]

    GuildsAPI::get_channels

    async fn GuildsAPI::get_channels(self : GuildsAPI, guild_id : String) -> Result[Json, RestError]

    GuildsAPI::get_member

    async fn GuildsAPI::get_member(self : GuildsAPI, guild_id : String, user_id : String) -> Result[Json, RestError]

    GuildsAPI::get_members

    async fn GuildsAPI::get_members(self : GuildsAPI, guild_id : String, query? : Array[(String, String)]) -> Result[Json, RestError]

    GuildsAPI::get_roles

    async fn GuildsAPI::get_roles(self : GuildsAPI, guild_id : String) -> Result[Json, RestError]

    GuildsAPI::new

    fn GuildsAPI::new(rest : RestClient) -> GuildsAPI

    InteractionsAPI

    pub struct InteractionsAPI {
    rest : RestClient
    }

    InteractionsAPI::delete_followup_message

    async fn InteractionsAPI::delete_followup_message(self : InteractionsAPI, application_id : String, interaction_token : String, message_id : String) -> Result[Json, RestError]

    InteractionsAPI::delete_reply

    async fn InteractionsAPI::delete_reply(self : InteractionsAPI, application_id : String, interaction_token : String) -> Result[Json, RestError]

    InteractionsAPI::edit_followup_message

    async fn InteractionsAPI::edit_followup_message(self : InteractionsAPI, application_id : String, interaction_token : String, message_id : String, body : Json) -> Result[Json, RestError]

    InteractionsAPI::edit_reply

    async fn InteractionsAPI::edit_reply(self : InteractionsAPI, application_id : String, interaction_token : String, body : Json) -> Result[Json, RestError]

    InteractionsAPI::follow_up

    async fn InteractionsAPI::follow_up(self : InteractionsAPI, application_id : String, interaction_token : String, body : Json) -> Result[Json, RestError]

    InteractionsAPI::get_followup_message

    async fn InteractionsAPI::get_followup_message(self : InteractionsAPI, application_id : String, interaction_token : String, message_id : String) -> Result[Json, RestError]

    InteractionsAPI::get_original_response

    async fn InteractionsAPI::get_original_response(self : InteractionsAPI, application_id : String, interaction_token : String) -> Result[Json, RestError]

    InteractionsAPI::new

    InteractionsAPI::reply

    async fn InteractionsAPI::reply(self : InteractionsAPI, interaction_id : String, interaction_token : String, body : Json, auth_mode? : AuthMode) -> Result[Json, RestError]

    PreparedRequest

    pub(all) struct PreparedRequest {
    meth : RequestMethod
    url : String
    headers : Map[String, String]
    body : String?
    }

    RequestMethod

    pub enum RequestMethod {
    MethodGet
    MethodPost
    MethodPut
    MethodPatch
    MethodDelete
    } derive(Eq, Show)

    RequestOptions

    pub struct RequestOptions {
    auth_mode : AuthMode
    body : Json?
    query : Array[(String, String)]
    headers : Map[String, String]
    }

    RestClient

    pub struct RestClient {
    options : RestOptions
    token : String?
    }

    RestClient::delete

    async fn RestClient::delete(self : RestClient, route : String, options? : RequestOptions) -> Result[Json, RestError]

    RestClient::get

    async fn RestClient::get(self : RestClient, route : String, options? : RequestOptions) -> Result[Json, RestError]

    RestClient::new

    fn RestClient::new(options? : RestOptions) -> RestClient

    RestClient::patch

    async fn RestClient::patch(self : RestClient, route : String, options? : RequestOptions) -> Result[Json, RestError]

    RestClient::post

    async fn RestClient::post(self : RestClient, route : String, options? : RequestOptions) -> Result[Json, RestError]

    RestClient::prepare_request

    fn RestClient::prepare_request(self : RestClient, meth : RequestMethod, route : String, options : RequestOptions) -> PreparedRequest

    RestClient::put

    async fn RestClient::put(self : RestClient, route : String, options? : RequestOptions) -> Result[Json, RestError]

    RestClient::request

    async fn RestClient::request(self : RestClient, meth : RequestMethod, route : String, options : RequestOptions) -> Result[Json, RestError]

    RestClient::with_token

    fn RestClient::with_token(self : RestClient, token : String) -> RestClient

    RestError

    pub enum RestError {
    Network(String)
    ApiError(Int, String)
    DecodeError(String)
    } derive(Eq, Show)

    RestOptions

    pub struct RestOptions {
    api : String
    headers : Map[String, String]
    timeout_ms : Int
    }

    UsersAPI

    pub struct UsersAPI {
    rest : RestClient
    }

    UsersAPI::create_dm

    async fn UsersAPI::create_dm(self : UsersAPI, body : Json) -> Result[Json, RestError]

    UsersAPI::edit

    async fn UsersAPI::edit(self : UsersAPI, body : Json) -> Result[Json, RestError]

    UsersAPI::get

    async fn UsersAPI::get(self : UsersAPI, user_id : String) -> Result[Json, RestError]

    UsersAPI::get_current

    async fn UsersAPI::get_current(self : UsersAPI) -> Result[Json, RestError]

    UsersAPI::get_guilds

    async fn UsersAPI::get_guilds(self : UsersAPI, query? : Array[(String, String)]) -> Result[Json, RestError]

    UsersAPI::new

    fn UsersAPI::new(rest : RestClient) -> UsersAPI

    VoiceAPI

    pub struct VoiceAPI {
    rest : RestClient
    }

    VoiceAPI::get_guild_voice_regions

    async fn VoiceAPI::get_guild_voice_regions(self : VoiceAPI, guild_id : String) -> Result[Json, RestError]

    VoiceAPI::get_voice_state

    async fn VoiceAPI::get_voice_state(self : VoiceAPI, guild_id : String, user_id : String) -> Result[Json, RestError]

    VoiceAPI::list_voice_regions

    async fn VoiceAPI::list_voice_regions(self : VoiceAPI) -> Result[Json, RestError]

    VoiceAPI::modify_current_user_voice_state

    async fn VoiceAPI::modify_current_user_voice_state(self : VoiceAPI, guild_id : String, channel_id : String, options? : Json) -> Result[Json, RestError]

    VoiceAPI::modify_user_voice_state

    async fn VoiceAPI::modify_user_voice_state(self : VoiceAPI, guild_id : String, user_id : String, channel_id : String, options? : Json) -> Result[Json, RestError]

    VoiceAPI::new

    fn VoiceAPI::new(rest : RestClient) -> VoiceAPI

    WebhooksAPI

    pub struct WebhooksAPI {
    rest : RestClient
    }

    WebhooksAPI::delete_message

    async fn WebhooksAPI::delete_message(self : WebhooksAPI, id : String, token : String, message_id : String, thread_id? : String) -> Result[Json, RestError]

    WebhooksAPI::edit_message

    async fn WebhooksAPI::edit_message(self : WebhooksAPI, id : String, token : String, message_id : String, body : Json) -> Result[Json, RestError]

    WebhooksAPI::execute

    async fn WebhooksAPI::execute(self : WebhooksAPI, id : String, token : String, body : Json, wait? : Bool, thread_id? : String) -> Result[Json, RestError]

    WebhooksAPI::get_message

    async fn WebhooksAPI::get_message(self : WebhooksAPI, id : String, token : String, message_id : String, thread_id? : String) -> Result[Json, RestError]

    WebhooksAPI::new

    api_base

    let api_base : String

    api_version

    let api_version : String

    auth_mode_token

    fn auth_mode_token(token : String) -> AuthMode

    cdn_base

    let cdn_base : String

    default_rest_headers

    fn default_rest_headers() -> Map[String, String]

    default_rest_options

    fn default_rest_options() -> RestOptions

    default_timeout_ms

    let default_timeout_ms : Int

    default_user_agent

    let default_user_agent : String

    encode_query

    fn encode_query(params : Array[(String, String)]) -> String

    new_api

    fn new_api(rest : RestClient) -> API

    Create grouped API modules backed by a REST client.

    new_rest_client

    fn new_rest_client(token? : String, api? : String, timeout_ms? : Int) -> RestClient

    Create a REST client configured for Discord API.

    request_method_get

    fn request_method_get() -> RequestMethod

    request_options

    fn request_options(auth_mode? : AuthMode, body? : Json, query? : Array[(String, String)], headers? : Map[String, String]) -> RequestOptions

    rest_options

    fn rest_options(api? : String, headers? : Map[String, String], timeout_ms? : Int) -> RestOptions

    route_channel

    fn route_channel(channel_id : String) -> String

    route_channel_message

    fn route_channel_message(channel_id : String, message_id : String) -> String

    route_channel_message_own_reaction

    fn route_channel_message_own_reaction(channel_id : String, message_id : String, emoji : String) -> String

    route_channel_messages

    fn route_channel_messages(channel_id : String) -> String

    route_channel_typing

    fn route_channel_typing(channel_id : String) -> String

    route_guild

    fn route_guild(guild_id : String) -> String

    route_guild_channels

    fn route_guild_channels(guild_id : String) -> String

    route_guild_member

    fn route_guild_member(guild_id : String, user_id : String) -> String

    route_guild_members

    fn route_guild_members(guild_id : String) -> String

    route_guild_regions

    fn route_guild_regions(guild_id : String) -> String

    route_guild_role

    fn route_guild_role(guild_id : String, role_id : String) -> String

    route_guild_roles

    fn route_guild_roles(guild_id : String) -> String

    route_guild_voice_state

    fn route_guild_voice_state(guild_id : String, user_id : String) -> String

    route_interaction_callback

    fn route_interaction_callback(interaction_id : String, interaction_token : String) -> String

    route_user

    fn route_user(user_id : String) -> String

    route_user_channels

    fn route_user_channels() -> String

    route_user_guilds

    fn route_user_guilds() -> String

    route_voice_regions

    fn route_voice_regions() -> String

    route_webhook

    fn route_webhook(id : String, token : String) -> String

    route_webhook_message

    fn route_webhook_message(id : String, token : String, message_id : String) -> String

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io