alchemy v0.4.1 Alchemy.Client
Represents a Client connection to the Discord API. This is the main public interface for the REST API.
As you might have seen in other examples, this module is the main entry
point for Alchemy. start/2 will start up the necessary modules and caches
necessary to hook into discord. You generally want to do this before
you do any other work in your bot really.
Blocking
All the requests here will block the process using them. Since all the event handles and commands created with this framework run in concurrent processes, this isn’t usually an issue. If you do want to “fire and forget”, it’s easy to wrap these requests in a task.
Caching
In general, you should try and use the functions provided by Alchemy.Cache
over the requests in this module, because the cache functions will be much faster,
and avoid excess API requests.
Summary
Functions
Adds a reaction to a message
Adds a role to a member of a guild
Bans a member from a guild
Modifies the nickname of the current user
Opens a new private channel with a user
Creates a new channel in a guild
Creates a new invite for a channel
Creates a new role in the guild
Deletes a channel from a guild
Removes an integration from a guild
Deletes an invite
Deletes a message
Deletes a list of messages
Deletes a reaction added by another user
Removes a role from a guild
Edits a channel in a guild, referenced by id
Edits a previously sent embed
Modifies a guild’s settings
Edits an integration of a guild
Modifies a member in a guild
Edits a message’s contents
Edits the client’s user_name and/or avatar
Edits a preexisting role in a guild
Gets a list of private channels open with this user
Gets a list of users banned from this guild
Gets a channel by its ID. Works on both private channels, and guild channels
Gets a list of invites for a channel
Returns a list of channel objects for a guild
Gets a list of guilds the client is currently a part of
Gets info about a certain guild
Gets a list of integration objects for a guild
Gets the information for a single invite
Returns a list of invites for a guild
Gets info for a member of a guild
Gets a list of members from a guild
Gets a message by channel, and message_id
Gets up to 100 messages from a channel
Gets a list of pinned messages in a channel
Returns a count of members who would be kicked in a prune operation
Gets a list of users who reacted to message with a particular emoji
Returns a list of voice regions in a guild
Gets a list of roles available in a guild
Gets a user by their client_id
Kicks a member from a guild
Makes the client leave a guild
Returns a list of all possible voice regions
Swaps the position of channels in a guild
Modifies the position of roles in a guild
Pins a message to its channel
Removes inactive members of a guild
Removes a reaction on a message, posted by this user
Removes all reactions from a message
Removes a role of a guild member
Sends a message to a particular channel
Starts up a new Client with the given token
Syncs a guild integration
Triggers the typing indicator
Unbans a user from the server
Removes a pinned message from a channel
Updates the status of the client
Types
Functions
Specs
add_reaction(Alchemy.Message.t | {channel_id, message_id}, unicode | Alchemy.Reaction.Emoji.t) ::
{:ok, nil} |
{:error, term}
Adds a reaction to a message.
This supports sending either a custom emoji object, or a unicode literal.
While sending raw unicode is technically possible, you’ll usually run
into url encoding issues due to hidden characters if you try to send something like
“❤️️”; use \u2764 instead.
Examples
Cogs.def heart do
Client.add_reaction(message, "️\u2764")
end
Adds a role to a member of a guild.
Requires the :manage_roles permission.
Examples
Client.add_role(guild_id, user_id, role_id)
Bans a member from a guild.
This prevents a user from rejoining for as long as the ban persists,
as opposed to kick_member/2 which will just make them leave the server.
A days paramater can be set to delete x days of messages; limited to 7.
Examples
Client.ban_member(guild_id, user_id, 1)
Modifies the nickname of the current user.
Examples
Client.change_nickname(guild_id, "best bot")
Specs
create_DM(snowflake) :: Alchemy.Channel.dm_channel
Opens a new private channel with a user.
Examples
Cogs.def dm_me do
Client.create_DM(message.author.id)
end
Specs
create_channel(snowflake, String.t, voice: Boolean, bitrate: Integer, user_limit: Integer) ::
{:ok, Alchemy.Channel.t} |
{:error, term}
Creates a new channel in a guild.
Requires the MANAGE_CHANNELS permission.
Options
voiceSetting this creates a new voice channel.bitrateSets the bitrate (bits) for a voice channel.user_limitSets the max amount of users for a voice channel.permission_overwritesAn overwrite for permissions in that channel
Examples
Client.create_channel(guild_id)
Specs
create_invite(snowflake, max_age: Integer, max_uses: Integer, temporary: Boolean, unique: True) ::
{:ok, Alchemy.Channel.invite} |
{:error, term}
Creates a new invite for a channel.
Requires the CREATE_INSTANT_INVITE permission.
Options
max_ageThe duration (seconds) of the invite.
0for never.max_usesThe max number of uses.
0for unlimited.temporaryWhether this invite grants temporary membership.
uniqueWhen set, a similar invite won’t try to be used. Useful for creating unique one time use invites.
Examples
Cogs.def invite do
{:ok, invite} = Client.create_invite(message.channel_id, max_age: 0)
Cogs.say("Here you go:\nhttps://discord.gg/#{invite.code}")
end
Specs
create_role(snowflake, name: String.t, permissions: Integer, color: Integer, hoist: Booean, mentionable: Boolean) ::
{:ok, Alchemy.Guild.role} |
{:error, term}
Creates a new role in the guild.
Requires the :manage_roles permission.
Options
nameThe name of the new role. Defaults to “new role”permissionsThe set of permissions for that role. Defaults to the@everyonepermissions in that guild.colorThe color of the role. Defaults to0x000000hoistWhen set totrue, the role will be displayed seperately in the sidebar.mentionableWhen set totrue, allows the role to be mentioned.
Examples
Client.create_role(guild_id, name: "the best role", color: 0x4bd1be)
Specs
delete_channel(snowflake) ::
{:ok, Alchemy.Channel.t} |
{:ok, Alchemy.Channel.dm_channel} |
{:error, term}
Deletes a channel from a guild.
Here’s an example of how to deal with the possible return types using pattern matching:
def my_delete(id) do
{:ok, channel} = Client.delete_channel(id)
case channel do
%DMChannel{} -> "this is a private channel!"
%Channel{} -> "this is a normal channel!"
end
end
Removes an integration from a guild.
Requires the :manage_guild permission.
Specs
delete_invite(String.t) :: {:ok, nil} | {:error, term}
Deletes an invite.
After deletion the invite can no longer be used, as you might expect.
Specs
delete_message(Alchemy.Message.t | {channel_id, message_id}) ::
{:ok, nil} |
{:error, term}
Deletes a message.
Requires the MANAGE_MESSAGES permission for messages not sent by the user.
Examples
content = "self destructing in 1s!!!"
{:ok, message} = Client.send_message(channel_id, content)
Process.sleep(1000)
Client.delete_message(message)
Specs
delete_messages(snowflake, [Alchemy.Message.t | snowflake]) ::
{:ok, nil} |
{:error, term}
Deletes a list of messages.
Requires the MANAGE_MESSAGES permission for messages not posted by this user.
Can only delete messages up to 2 weeks old.
Cogs.def countdown do
{:ok, m1} = Cogs.say "3..."
Process.sleep(1000)
{:ok, m2} = Cogs.say "2..."
Process.sleep(1000)
{:ok, m3} = Cogs.say "1..."
Process.sleep(1000)
Client.delete_messages(message.channel, [m1, m2, m3])
end
Specs
delete_reaction(Alchemy.Message.t | {channel_id, message_id}, unicode | Alchemy.Reaction.Emoji.t, snowflake | Alchemy.User.t) ::
{:ok, nil} |
{:error, term}
Deletes a reaction added by another user.
Requires the MANAGE_MESSAGES permission.
Removes a role from a guild.
Requires the :manage_roles permission.
Specs
edit_channel(snowflake, name: String.t, position: Integer, topic: String.t, bitrate: Integer, user_limit: Integer) ::
{:ok, Alchemy.Channel.t} |
{:error, term}
Edits a channel in a guild, referenced by id.
All the paramaters are optional. Some are mutually exclusive. I.E. you can’t use voice only and text only parameters in the same request.
Options
namethe name for the channelpositionthe position in the left hand listingtopic~ text only ~ the topic of the channelbitrate~ voice only ~ the bitrate, in bits, from8000to96000, for the voice channel to takeuser_limit~ voice only ~ the max amount of users allowed in this channel. From1to99, or0for no limit.
Examples
Client.edit_channel(id, name: "the best channel", position: 1)
{:ok, new_voice_channel} = Client.edit_channel(id, bitrate: 8000)
Specs
edit_embed(Alchemy.Message.t | {channel_id, message_id}, Alchemy.Embed.t) ::
{:ok, Alchemy.Message.t} |
{:error, term}
Edits a previously sent embed.
Note that this can be accomplished via edit_message/3 as well, but that requires
editing the content as well.
Cogs.def embed do
embed = %Embed{description: "the best embed"}
|> color(0xc13261)
{:ok, message} = Embed.send(embed)
Process.sleep(2000)
Client.edit_embed(message, embed |> color(0x5aa4d4))
end
Specs
Modifies a guild’s settings.
Options
nameThe name of the guild.regionThe id of the voice region.verification_levelThe level of verification of the guild.default_message_notificationsThe default message notification settings.afk_channel_idThe id of the afk channel.afk_timeoutThe afk timeout in seconds.iconA url to the new icon. Must be a 128x128 jpeg image.splashA url to the new splash screen. This is only available for partnered guilds.
Examples
Client.edit_guild(guild_id, name: "new name")
Specs
Edits an integration of a guild.
Requires the :manage_guild permission.
Options
expire_behaviourThe behaviour when an integration subscription lapses.expire_grace_periodPeriod (seconds) where the integration ignores lapsed subscriptions.enable_emoticonsWhether or not emoticons should be synced for this integration.
Specs
Modifies a member in a guild.
Each option requires different permissions.
Options
nickThe nickname of the user. Requires:manage_nicknamesrolesA list of roles (ids) the user should have after the change. Requires:manage_rolesmuteWhether or not the user should be muted. Requires:mute_membersdeafWhether or not the user should be deafened. Requires:deafen_memberschannel_idVoice channel to move the user too (if they are connected). Requires:move_members, and permission to connect to that channel.
Examples
Client.edit_member(guild_id, user_id, nick: "cool guy")
Edits a message’s contents.
Examples
{:ok, message} = Client.send_message(channel, "ping!")
Process.sleep(1000)
Client.edit_message(message, "not ping anymore!")
Specs
edit_profile(username: String.t, avatar: url) ::
{:ok, Alchemy.User.t} |
{:error, term}
Edits the client’s user_name and/or avatar.
Options
user_nameA string specifiying the new user_name for the clientavatarA link to an image for the client’s avatar
Examples
# Will edit "behind the scenes"
Client.edit_profile(username: "NewGuy", avatar: "imgur.com/image.jpeg")
iex> {:ok, user} = Client.edit_profile(username: "NewName")
{:ok, Alchemy.User%{....
Specs
edit_role(snowflake, snowflake, name: String.t, permissions: Integer, color: Integer, hoist: Booean, mentionable: Boolean) ::
{:ok, Alchemy.Guild.role} |
{:error, term}
Edits a preexisting role in a guild.
The same as create_role/2 except that this operates on a role that has already
been created. See that function for discussion.
Specs
get_DMs :: [Alchemy.Channel.dm_channel]
Gets a list of private channels open with this user.
Examples
Client.get_DMs()
Specs
get_bans(snowflake) ::
{:ok, [Alchemy.User.t]} |
{:error, term}
Gets a list of users banned from this guild.
Requires the :ban_members permission.
Examples
{:ok, bans} = Client.get_bans(guild_id)
Specs
get_channel(snowflake) ::
{:ok, Alchemy.Channel.t} |
{:ok, Alchemy.Channel.dm_channel} |
{:error, term}
Gets a channel by its ID. Works on both private channels, and guild channels.
Examples
{:ok, channel} = Client.get_channel("id")
Specs
get_channel_invites(snowflake) ::
{:ok, [Alchemy.Channel.invite]} |
{:error, term}
Gets a list of invites for a channel.
Only usable for guild channels.
Examples
Cogs.def count_invites do
{:ok, invites} = Client.get_channel_invites(message.channel_id)
Cogs.say("there are #{length(invites)} invites active in this channel")
end
Specs
get_channels(snowflake) ::
{:ok, [Alchemy.Channel.t]} |
{:error, term}
Returns a list of channel objects for a guild.
As with most guild methods, the cache should be preferred over the api if possible.
Examples
Client.get_channels(guild_id)
Specs
get_current_guilds ::
{:ok, [Alchemy.User.user_guild]} |
{:error, term}
Gets a list of guilds the client is currently a part of.
Examples
{:ok, guilds} = Client.current_guilds
Specs
get_guild(snowflake) ::
{:ok, Alchemy.Guild.t} |
{:error, term}
Gets info about a certain guild.
The info returned here doesn’t contain as much info as contained in the cache. For guilds the user is a part of, the cache should be preferred over this method.
Client.get_guild(id)
Specs
get_integrations(snowflake) ::
{:ok, %{}} |
{:error, term}
Gets a list of integration objects for a guild.
Requires the :manage_guild permission.
Specs
get_invite(String.t) ::
{:ok, Alchemy.Channel.invite} |
{:error, term}
Gets the information for a single invite.
Not to be confused with get_invites/1, which lists out the invites
in a guild. This merely gets the information relating to a single invite,
accessed by its code.
Specs
get_invites(snowflake) ::
{:ok, [Alchemy.Channel.invite]} |
{:error, term}
Returns a list of invites for a guild.
Requires the :manage_guild permission.
Specs
get_member(snowflake, snowflake) ::
{:ok, Alchemy.Guild.member} |
{:error, term}
Gets info for a member of a guild.
For guilds the bot is in, use the corresponding cache method instead.
Examples
Client.get_member(guild_id, user_id)
Specs
get_member_list(snowflake, limit: Integer, after: snowflake) ::
{:ok, [Alchemy.Guild.member]} |
{:error, term}
Gets a list of members from a guild.
Options
limitThe number of members to fetch (max 1000).afterSetting this to a user id will only fetch members that joined after that person.
Examples
Client.get_member_list(guild_id, limit: 10)
Specs
get_message(snowflake, snowflake) ::
{:ok, Alchemy.Message.t} |
{:error, term}
Gets a message by channel, and message_id
Use get_messages for a bulk request instead.
Examples
{:ok, message} = Client.get_message(channel, id)
Specs
get_messages(snowflake, around: snowflake, before: snowflake, after: snowflake, limit: Integer) ::
{:ok, [Alchemy.Message.t]} |
{:error, term}
Gets up to 100 messages from a channel.
around, before, after are all mutually exclusive.
Options
aroundwill search for messages around the time of a particular messagebeforewill get messages before a certain messageafterwill get messages after a certain messagelimitthe number of messages to get. Defaults to100
Examples
{:ok, messages} = Client.get_messages(around: id, limit: 40)
Specs
get_pins(snowflake) ::
{:ok, [Alchemy.Message.t]} |
{:error, term}
Gets a list of pinned messages in a channel.
Examples
Cogs.def pins do
{:ok, pinned} = Client.get_pins(message.channel_id)
Cogs.say("there are #{length(pinned)} pins in this channel.")
end
Specs
get_prune_count(snowflake, Integer) ::
{:ok, nil} |
{:error, term}
Returns a count of members who would be kicked in a prune operation.
Days specifies the amount of days of inactivity to check for.
See prune_guild/2 for a discussion of this operation.
Specs
get_reactions(Alchemy.Message.t | {channel_id, message_id}, unicode | Alchemy.Reaction.Emoji.t) ::
{:ok, [Alchemy.User.t]} |
{:error, term}
Gets a list of users who reacted to message with a particular emoji.
Examples
Cogs.def react do {:ok, message} = Cogs.say(“react to this!”) Process.sleep(10000) {:ok, users} = Client.get_reactions(message, “❤”) Cogs.say(“#{length(users)} users reacted with a ❤!”) end
Specs
get_regions(snowflake) ::
{:ok, [Alchemy.VoiceRegion.t]} |
{:error, term}
Returns a list of voice regions in a guild.
Specs
get_roles(snowflake) ::
{:ok, [Alchemy.Guild.role]} |
{:error, term}
Gets a list of roles available in a guild.
Requires the :manage_roles permission.
Examples
Client.get_roles(guild_id)
Specs
get_user(snowflake) ::
{:ok, Alchemy.User.t} |
{:error, term}
Gets a user by their client_id.
"@me" can be passed to get the info
relevant to the Client.
Examples
iex> {:ok, user} = Client.get_user("client_id")
{:ok, Alchemy.User%{....
Kicks a member from a guild.
Not to be confused with ban_member/3.
Examples
Client.kick_member(guild_id, user_id)
Specs
leave_guild(snowflake) :: {:ok, nil} | {:error, term}
Makes the client leave a guild.
Examples
Client.leave_guild(guild_id)
Specs
list_voice_regions ::
{:ok, [Alchemy.VoiceRegion.t]} |
{:error, term}
Returns a list of all possible voice regions.
Swaps the position of channels in a guild.
Examples
# alphabetizes a guild channel list
with {:ok, channels} <- Client.get_channels(guild_id) do
channels
|> Enum.sort_by(& &1.name)
|> Stream.map(& &1.id)
|> Enum.with_index
|> (&Client.move_channels(guild_id, &1)).()
end
Specs
move_roles(snowflake, [{snowflake, Integer}]) ::
{:ok, [Alchemy.Guild.role]} |
{:error, term}
Modifies the position of roles in a guild.
Takes a list of {id, position} where position is an integer starting at 0,
and id is the id of the role.
Returns a list of all the roles in the guild.
Requires the :manage_roles permission.
Specs
pin(Alchemy.Message.t | {channel_id, message_id}) ::
{:ok, nil} |
{:error, term}
Pins a message to its channel.
Examples
Cogs.def pin_this do
Client.pin(message)
end
Specs
prune_guild(snowflake, Integer) ::
{:ok, nil} |
{:error, term}
Removes inactive members of a guild.
Days allows you to specify the amount of days of inactivity necessary to be kicked from the guild.
Requires the :manage_roles permission
Examples
Client.prune_guild(guild_id, )
Specs
remove_reaction(Alchemy.Message.t | {channel_id, message_id}, unicode | Alchemy.Reaction.Emoji.t) ::
{:ok, nil} |
{:error, term}
Removes a reaction on a message, posted by this user.
This doesn’t require the MANAGE_MESSAGES permission, unlike
delete_reaction.
Example
Cogs.def indecisive do
Client.add_reaction(message, "❤")
Process.sleep(3000)
Client.remove_reaction(message, "❤")
end
Specs
remove_reactions(Alchemy.Message.t | {channel_id, message_id}) ::
{:ok, nil} |
{:error, term}
Removes all reactions from a message.
Requires the MANAGE_MESSAGES permission.
Examples
Cogs.def psyche do
{:ok, message} = Cogs.say("react to this")
Process.sleep(10000)
Client.delete_reactions(message)
end
Removes a role of a guild member.
Requires the :manage_roles permission.
Examples
Client.remove_role(guild_id, user_id, role_id)
Sends a message to a particular channel
Options
ttsused to set whether or not a message should be text to speechembedused to send anEmbedobject along with the messagefileused to send a file along with the message
Examples
{:ok, message} = Client.send_message(chan_id, "pong!")
Sending files along with messages is simple as well.
Client.send_message(chan_id, "here you go!", "foo.txt")
Starts up a new Client with the given token.
An optional selfbot: id can be passed, indiciating that you’re
using a user token instead of a normal bot token. SelfBots will only
respond to themselves, and certain functionalities of the API may
not work as well as for normal bots.
Syncs a guild integration.
Requires the :manage_guild permission.
Specs
trigger_typing(snowflake) ::
{:ok, nil} |
{:error, term}
Triggers the typing indicator.
This shouldn’t be used by bots usually.
Examples
Cogs.def hard_math do
Client.trigger_typing(message.channel_id)
Process.sleep(3000)
Cogs.say("done!")
end
Unbans a user from the server.
Examples
Client.unban_member(guild_id, user_id)
Specs
unpin(Alchemy.Message.t | {channel_id, message_id}) ::
{:ok, nil} |
{:error, term}
Removes a pinned message from a channel.
Examples
Cogs.def unpin do
{:ok, [first|_]} = Client.get_pins(message.channel_id)
Client.unpin(first)
end
Updates the status of the client.
The status displays a “playing Game” message under the client, as well setting an inactivity based on idleness.
game_name specifies the name to update the game_status to, nil
will clear that status. idle_since can be specified, using
unix time, in milliseconds, to indicate for how long the client has been idle.
Note on ratelimiting
This functionality is heavily ratelimited, at a rate of 1 req / 12s. Because of this, this function will automatically error after 24s of waiting. Because of how long this may take, you might want to run this in a new task.
Examples
Client.update_status("Alchemy")