Lingo

An Elixir library for interacting with the Discord API.

Currently covers:

Check out the docs for guides and the full API reference.

Install

{:lingo, "~> 0.3.0"}

Quick start

defmodule MyBot do
use Lingo.Bot
command "ping", "Responds with pong" do
reply!(ctx, "Pong!")
end
handle :message_create, msg do
if msg.content == "hello" do
Lingo.send_message(msg.channel_id, content: "Hello back!")
end
end
end
children = [
{Lingo,
bot: MyBot,
token: System.get_env("BOT_TOKEN"),
intents: [:guilds, :guild_messages]}
]
Supervisor.start_link(children, strategy: :one_for_one)

Register your slash commands with Discord (run once from iex, or after changes):

Lingo.register_commands(MyBot)

Commands with options

command "greet", "Greet someone",
options: [
user("target", "Who to greet", required: true)
] do
user = resolved_user(ctx, option(ctx, "target"))
reply!(ctx, "Hey #{user.username}!")
end

Components and collectors

command "confirm", "Ask for confirmation" do
reply!(ctx, content: "Are you sure?", components: [
Lingo.action_row([
Lingo.button(custom_id: "yes", label: "Yes", style: :success),
Lingo.button(custom_id: "no", label: "No", style: :danger)
])
])
end
component "yes", ctx do
update!(ctx, content: "Confirmed!", components: [])
end

Or use collectors to await interactions inline:

case Lingo.await_component(msg.id, timeout: 30_000) do
{:ok, interaction} -> # handle the click
:timeout -> # no response
end

Embeds

Lingo.embed(
title: "Server Info",
color: 0x5865F2,
fields: [
%{name: "Members", value: "1,234", inline: true},
%{name: "Created", value: Lingo.timestamp(guild.created_at, :relative), inline: true}
]
)

Events

handle :guild_member_add, member do
Lingo.send_message(channel_id,
content: "Welcome #{Lingo.mention_user(member.user.id)}!"
)
end
handle :message_update, %{old: old, new: msg} do
if old, do: IO.puts("Changed from: #{old.content}")
end