Discord Bot Light Client

A lightweight, modern Erlang library for building Discord bots using the Gateway and HTTP APIs.
This library provides a simple, configurable client process that connects to Discord, listens for messages, and lets you define your own command handlers.

Hex.pm Hex Docs License


Features


Installation

Add the following to your rebar.config dependencies:

{discord_bot_light, "v0.3.0"}

Make sure you also depend on:


Usage

1. Write a Command Handler Module

Create a module that exports handle_message/3:

%%% example_bot_commands.erl
-module(example_bot_commands).
-export([handle_message/4]).
handle_message(<<"!hello">>, ChannelId, Author, Token) ->
Username = maps:get(<<"username">>, Author, <<"Unknown">>),
Response = <<"Hello, ", Username/binary, "!">>,
discord_bot_light_client:send_message(ChannelId, Response, Token);
handle_message(<<"!ping">>, ChannelId, _Author, Token) ->
discord_bot_light_client:send_message(ChannelId, <<"Pong!">>, Token);
handle_message(_, _, _, _) ->
ok.

2. Start the Bot

You can start the bot from your application supervisor or shell:

Token = <<"YOUR_DISCORD_BOT_TOKEN">>,
Handler = example_bot_commands,
{ok, Pid} = discord_bot_light_client:start_link(Token, [{command_handler, Handler}]).

3. Send Messages from Anywhere

You can send messages manually:

discord_bot_light_client:send_message(<<"channel_id">>, <<"Hello, Discord!">>, <<"YOUR_TOKEN">>).

Or edit messages:

discord_bot_light_client:edit_message(<<"channel_id">>, <<"message_id">>, <<"New content!">>, <<"YOUR_TOKEN">>).

Command Handler Interface

Your handler should export:

handle_message(Content, ChannelId, Author) -> ok | {error, Reason}.

Notes


License

Licensed under the Apache License, Version 2.0. See the LICENSE file for details.


Credits


Happy hacking!

For questions or improvements, open an issue or PR on the repository.