[][src]Struct serenity::http::client::Http

pub struct Http {
    pub ratelimiter: Ratelimiter,
    pub token: String,
    // some fields omitted
}

Fields

ratelimiter: Ratelimitertoken: String

Methods

impl Http[src]

pub fn new(client: Arc<Client>, token: &str) -> Self[src]

pub fn new_with_token(token: &str) -> Self[src]

pub fn add_group_recipient(&self, group_id: u64, user_id: u64) -> Result<()>[src]

Adds a User as a recipient to a Group.

Note: Groups have a limit of 10 recipients, including the current user.

pub fn add_member_role(
    &self,
    guild_id: u64,
    user_id: u64,
    role_id: u64
) -> Result<()>
[src]

Adds a single Role to a Member in a Guild.

Note: Requires the Manage Roles permission and respect of role hierarchy.

pub fn ban_user(
    &self,
    guild_id: u64,
    user_id: u64,
    delete_message_days: u8,
    reason: &str
) -> Result<()>
[src]

Bans a User from a Guild, removing their messages sent in the last X number of days.

Passing a delete_message_days of 0 is equivalent to not removing any messages. Up to 7 days' worth of messages may be deleted.

Note: Requires that you have the Ban Members permission.

pub fn broadcast_typing(&self, channel_id: u64) -> Result<()>[src]

Broadcasts that the current user is typing in the given Channel.

This lasts for about 10 seconds, and will then need to be renewed to indicate that the current user is still typing.

This should rarely be used for bots, although it is a good indicator that a long-running command is still being processed.

pub fn create_channel(
    &self,
    guild_id: u64,
    map: &Map<String, Value>
) -> Result<GuildChannel>
[src]

Creates a GuildChannel in the Guild given its Id.

Refer to the Discord's docs for information on what fields this requires.

Note: Requires the Manage Channels permission.

pub fn create_emoji(&self, guild_id: u64, map: &Value) -> Result<Emoji>[src]

Creates an emoji in the given Guild with the given data.

View the source code for Guild's create_emoji method to see what fields this requires.

Note: Requires the Manage Emojis permission.

pub fn create_guild(&self, map: &Value) -> Result<PartialGuild>[src]

Creates a guild with the data provided.

Only a PartialGuild will be immediately returned, and a full Guild will be received over a Shard, if at least one is running.

Note: This endpoint is currently limited to 10 active guilds. The limits are raised for whitelisted GameBridge applications. See the documentation on this endpoint for more info.

Examples

Create a guild called "test" in the US West region:

This example is not tested
use serde_json::builder::ObjectBuilder;
use serde_json::Value;
use serenity::http::Http;

let map = ObjectBuilder::new()
    .insert("name", "test")
    .insert("region", "us-west")
    .build();

let _result = http.create_guild(map);

pub fn create_guild_integration(
    &self,
    guild_id: u64,
    integration_id: u64,
    map: &Value
) -> Result<()>
[src]

Creates an Integration for a Guild.

Refer to Discord's docs for field information.

Note: Requires the Manage Guild permission.

pub fn create_invite(
    &self,
    channel_id: u64,
    map: &Map<String, Value>
) -> Result<RichInvite>
[src]

Creates a RichInvite for the given channel.

Refer to Discord's docs for field information.

All fields are optional.

Note: Requires the Create Invite permission.

pub fn create_permission(
    &self,
    channel_id: u64,
    target_id: u64,
    map: &Value
) -> Result<()>
[src]

Creates a permission override for a member or a role in a channel.

pub fn create_private_channel(&self, map: &Value) -> Result<PrivateChannel>[src]

Creates a private channel with a user.

pub fn create_reaction(
    &self,
    channel_id: u64,
    message_id: u64,
    reaction_type: &ReactionType
) -> Result<()>
[src]

Reacts to a message.

pub fn create_role(
    &self,
    guild_id: u64,
    map: &Map<String, Value>
) -> Result<Role>
[src]

Creates a role.

pub fn create_webhook(&self, channel_id: u64, map: &Value) -> Result<Webhook>[src]

Creates a webhook for the given channel's Id, passing in the given data.

This method requires authentication.

The Value is a map with the values of:

  • avatar: base64-encoded 128x128 image for the webhook's default avatar (optional);
  • name: the name of the webhook, limited to between 2 and 100 characters long.

Examples

Creating a webhook named test:

This example is not tested
use serde_json::builder::ObjectBuilder;
use serenity::http::Http;

let channel_id = 81384788765712384;
let map = ObjectBuilder::new().insert("name", "test").build();

let webhook = http.create_webhook(channel_id, map).expect("Error creating");

pub fn delete_channel(&self, channel_id: u64) -> Result<Channel>[src]

Deletes a private channel or a channel in a guild.

pub fn delete_emoji(&self, guild_id: u64, emoji_id: u64) -> Result<()>[src]

Deletes an emoji from a server.

pub fn delete_guild(&self, guild_id: u64) -> Result<PartialGuild>[src]

Deletes a guild, only if connected account owns it.

pub fn delete_guild_integration(
    &self,
    guild_id: u64,
    integration_id: u64
) -> Result<()>
[src]

Removes an integration from a guild.

pub fn delete_invite(&self, code: &str) -> Result<Invite>[src]

Deletes an invite by code.

pub fn delete_message(&self, channel_id: u64, message_id: u64) -> Result<()>[src]

Deletes a message if created by us or we have specific permissions.

pub fn delete_messages(&self, channel_id: u64, map: &Value) -> Result<()>[src]

Deletes a bunch of messages, only works for bots.

pub fn delete_message_reactions(
    &self,
    channel_id: u64,
    message_id: u64
) -> Result<()>
[src]

Deletes all of the Reactions associated with a Message.

Examples

use serenity::model::id::{ChannelId, MessageId};

let channel_id = ChannelId(7);
let message_id = MessageId(8);

let _ = http.as_ref().delete_message_reactions(channel_id.0, message_id.0)
    .expect("Error deleting reactions");

pub fn delete_permission(&self, channel_id: u64, target_id: u64) -> Result<()>[src]

Deletes a permission override from a role or a member in a channel.

pub fn delete_reaction(
    &self,
    channel_id: u64,
    message_id: u64,
    user_id: Option<u64>,
    reaction_type: &ReactionType
) -> Result<()>
[src]

Deletes a reaction from a message if owned by us or we have specific permissions.

pub fn delete_role(&self, guild_id: u64, role_id: u64) -> Result<()>[src]

Deletes a role from a server. Can't remove the default everyone role.

pub fn delete_webhook(&self, webhook_id: u64) -> Result<()>[src]

Deletes a Webhook given its Id.

This method requires authentication, whereas delete_webhook_with_token does not.

Examples

Deletes a webhook given its Id:

use serenity::http::Http;
use std::{env, sync::Arc};

// Due to the `delete_webhook` function requiring you to authenticate, you
// must have set the token first.
let http = Arc::new(Http::default());

http.as_ref().delete_webhook(245037420704169985).expect("Error deleting webhook");

pub fn delete_webhook_with_token(
    &self,
    webhook_id: u64,
    token: &str
) -> Result<()>
[src]

Deletes a Webhook given its Id and unique token.

This method does not require authentication.

Examples

Deletes a webhook given its Id and unique token:

let id = 245037420704169985;
let token = "ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";

http.as_ref().delete_webhook_with_token(id, token).expect("Error deleting webhook");

pub fn edit_channel(
    &self,
    channel_id: u64,
    map: &Map<String, Value>
) -> Result<GuildChannel>
[src]

Changes channel information.

pub fn edit_emoji(
    &self,
    guild_id: u64,
    emoji_id: u64,
    map: &Value
) -> Result<Emoji>
[src]

Changes emoji information.

pub fn edit_guild(
    &self,
    guild_id: u64,
    map: &Map<String, Value>
) -> Result<PartialGuild>
[src]

Changes guild information.

pub fn edit_guild_channel_positions(
    &self,
    guild_id: u64,
    value: &Value
) -> Result<()>
[src]

Edits the positions of a guild's channels.

pub fn edit_guild_embed(&self, guild_id: u64, map: &Value) -> Result<GuildEmbed>[src]

Edits a Guild's embed setting.

pub fn edit_member(
    &self,
    guild_id: u64,
    user_id: u64,
    map: &Map<String, Value>
) -> Result<()>
[src]

Does specific actions to a member.

pub fn edit_message(
    &self,
    channel_id: u64,
    message_id: u64,
    map: &Value
) -> Result<Message>
[src]

Edits a message by Id.

Note: Only the author of a message can modify it.

pub fn edit_nickname(
    &self,
    guild_id: u64,
    new_nickname: Option<&str>
) -> Result<()>
[src]

Edits the current user's nickname for the provided Guild via its Id.

Pass None to reset the nickname.

pub fn edit_profile(&self, map: &Map<String, Value>) -> Result<CurrentUser>[src]

Edits the current user's profile settings.

pub fn edit_role(
    &self,
    guild_id: u64,
    role_id: u64,
    map: &Map<String, Value>
) -> Result<Role>
[src]

Changes a role in a guild.

pub fn edit_role_position(
    &self,
    guild_id: u64,
    role_id: u64,
    position: u64
) -> Result<Vec<Role>>
[src]

Changes the position of a role in a guild.

pub fn edit_webhook(&self, webhook_id: u64, map: &Value) -> Result<Webhook>[src]

Edits a the webhook with the given data.

The Value is a map with optional values of:

  • avatar: base64-encoded 128x128 image for the webhook's default avatar (optional);
  • name: the name of the webhook, limited to between 2 and 100 characters long.

Note that, unlike with create_webhook, all values are optional.

This method requires authentication, whereas edit_webhook_with_token does not.

Examples

Edit the image of a webhook given its Id and unique token:

This example is not tested
use serde_json::builder::ObjectBuilder;
use serenity::http::Http;

let id = 245037420704169985;
let token = "ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
let image = serenity::utils::read_image("./webhook_img.png")
    .expect("Error reading image");
let map = ObjectBuilder::new().insert("avatar", image).build();

let edited = http.as_ref().edit_webhook_with_token(id, token, map)
    .expect("Error editing webhook");

pub fn edit_webhook_with_token(
    &self,
    webhook_id: u64,
    token: &str,
    map: &Map<String, Value>
) -> Result<Webhook>
[src]

Edits the webhook with the given data.

Refer to the documentation for edit_webhook for more information.

This method does not require authentication.

Examples

Edit the name of a webhook given its Id and unique token:

This example is not tested
use serde_json::builder::ObjectBuilder;
use serenity::http::Http;

let id = 245037420704169985;
let token = "ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
let map = ObjectBuilder::new().insert("name", "new name").build();

let edited = http.as_ref().edit_webhook_with_token(id, token, map)
    .expect("Error editing webhook");

pub fn execute_webhook(
    &self,
    webhook_id: u64,
    token: &str,
    wait: bool,
    map: &Map<String, Value>
) -> Result<Option<Message>>
[src]

Executes a webhook, posting a Message in the webhook's associated Channel.

This method does not require authentication.

Pass true to wait to wait for server confirmation of the message sending before receiving a response. From the Discord docs:

waits for server confirmation of message send before response, and returns the created message body (defaults to false; when false a message that is not saved does not return an error)

The map can optionally contain the following data:

  • avatar_url: Override the default avatar of the webhook with a URL.
  • tts: Whether this is a text-to-speech message (defaults to false).
  • username: Override the default username of the webhook.

Additionally, at least one of the following must be given:

  • content: The content of the message.
  • embeds: An array of rich embeds.

Note: For embed objects, all fields are registered by Discord except for height, provider, proxy_url, type (it will always be rich), video, and width. The rest will be determined by Discord.

Examples

Sending a webhook with message content of test:

This example is not tested
use serde_json::builder::ObjectBuilder;
use serenity::http::Http;

let id = 245037420704169985;
let token = "ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
let map = ObjectBuilder::new().insert("content", "test").build();

let message = match http.as_ref().execute_webhook(id, token, true, map) {
    Ok(Some(message)) => message,
    Ok(None) => {
        println!("Expected a webhook message");

        return;
    },
    Err(why) => {
        println!("Error executing webhook: {:?}", why);

        return;
    },
};

pub fn get_active_maintenances(&self) -> Result<Vec<Maintenance>>[src]

Gets the active maintenances from Discord's Status API.

Does not require authentication.

pub fn get_bans(&self, guild_id: u64) -> Result<Vec<Ban>>[src]

Gets all the users that are banned in specific guild.

pub fn get_audit_logs(
    &self,
    guild_id: u64,
    action_type: Option<u8>,
    user_id: Option<u64>,
    before: Option<u64>,
    limit: Option<u8>
) -> Result<AuditLogs>
[src]

Gets all audit logs in a specific guild.

pub fn get_bot_gateway(&self) -> Result<BotGateway>[src]

Gets current bot gateway.

pub fn get_channel_invites(&self, channel_id: u64) -> Result<Vec<RichInvite>>[src]

Gets all invites for a channel.

pub fn get_channel_webhooks(&self, channel_id: u64) -> Result<Vec<Webhook>>[src]

Retrieves the webhooks for the given channel's Id.

This method requires authentication.

Examples

Retrieve all of the webhooks owned by a channel:


let channel_id = 81384788765712384;

let webhooks = http.as_ref().get_channel_webhooks(channel_id)
    .expect("Error getting channel webhooks");

pub fn get_channel(&self, channel_id: u64) -> Result<Channel>[src]

Gets channel information.

pub fn get_channels(&self, guild_id: u64) -> Result<Vec<GuildChannel>>[src]

Gets all channels in a guild.

pub fn get_current_application_info(&self) -> Result<CurrentApplicationInfo>[src]

Gets information about the current application.

Note: Only applications may use this endpoint.

pub fn get_current_user(&self) -> Result<CurrentUser>[src]

Gets information about the user we're connected with.

pub fn get_gateway(&self) -> Result<Gateway>[src]

Gets current gateway.

pub fn get_guild(&self, guild_id: u64) -> Result<PartialGuild>[src]

Gets guild information.

pub fn get_guild_embed(&self, guild_id: u64) -> Result<GuildEmbed>[src]

Gets a guild embed information.

pub fn get_guild_integrations(&self, guild_id: u64) -> Result<Vec<Integration>>[src]

Gets integrations that a guild has.

pub fn get_guild_invites(&self, guild_id: u64) -> Result<Vec<RichInvite>>[src]

Gets all invites to a guild.

pub fn get_guild_vanity_url(&self, guild_id: u64) -> Result<String>[src]

Gets a guild's vanity URL if it has one.

pub fn get_guild_members(
    &self,
    guild_id: u64,
    limit: Option<u64>,
    after: Option<u64>
) -> Result<Vec<Member>>
[src]

Gets the members of a guild. Optionally pass a limit and the Id of the user to offset the result by.

pub fn get_guild_prune_count(
    &self,
    guild_id: u64,
    map: &Value
) -> Result<GuildPrune>
[src]

Gets the amount of users that can be pruned.

pub fn get_guild_regions(&self, guild_id: u64) -> Result<Vec<VoiceRegion>>[src]

Gets regions that a guild can use. If a guild has the VIP_REGIONS feature enabled, then additional VIP-only regions are returned.

pub fn get_guild_roles(&self, guild_id: u64) -> Result<Vec<Role>>[src]

Retrieves a list of roles in a Guild.

pub fn get_guild_webhooks(&self, guild_id: u64) -> Result<Vec<Webhook>>[src]

Retrieves the webhooks for the given guild's Id.

This method requires authentication.

Examples

Retrieve all of the webhooks owned by a guild:

let guild_id = 81384788765712384;

let webhooks = http.as_ref().get_guild_webhooks(guild_id)
    .expect("Error getting guild webhooks");

pub fn get_guilds(
    &self,
    target: &GuildPagination,
    limit: u64
) -> Result<Vec<GuildInfo>>
[src]

Gets a paginated list of the current user's guilds.

The limit has a maximum value of 100.

Discord's documentation

Examples

Get the first 10 guilds after a certain guild's Id:

use serenity::{http::GuildPagination, model::id::GuildId};

let guild_id = GuildId(81384788765712384);

let guilds = http.as_ref().get_guilds(&GuildPagination::After(guild_id), 10).unwrap();

pub fn get_invite(&self, code: &str, stats: bool) -> Result<Invite>[src]

Gets information about a specific invite.

pub fn get_member(&self, guild_id: u64, user_id: u64) -> Result<Member>[src]

Gets member of a guild.

pub fn get_message(&self, channel_id: u64, message_id: u64) -> Result<Message>[src]

Gets a message by an Id, bots only.

pub fn get_messages(&self, channel_id: u64, query: &str) -> Result<Vec<Message>>[src]

Gets X messages from a channel.

pub fn get_pins(&self, channel_id: u64) -> Result<Vec<Message>>[src]

Gets all pins of a channel.

pub fn get_reaction_users(
    &self,
    channel_id: u64,
    message_id: u64,
    reaction_type: &ReactionType,
    limit: u8,
    after: Option<u64>
) -> Result<Vec<User>>
[src]

Gets user Ids based on their reaction to a message. This endpoint is dumb.

pub fn get_unresolved_incidents(&self) -> Result<Vec<Incident>>[src]

Gets the current unresolved incidents from Discord's Status API.

Does not require authentication.

pub fn get_upcoming_maintenances(&self) -> Result<Vec<Maintenance>>[src]

Gets the upcoming (planned) maintenances from Discord's Status API.

Does not require authentication.

pub fn get_user(&self, user_id: u64) -> Result<User>[src]

Gets a user by Id.

pub fn get_user_dm_channels(&self) -> Result<Vec<PrivateChannel>>[src]

Gets our DM channels.

pub fn get_voice_regions(&self) -> Result<Vec<VoiceRegion>>[src]

Gets all voice regions.

pub fn get_webhook(&self, webhook_id: u64) -> Result<Webhook>[src]

Retrieves a webhook given its Id.

This method requires authentication, whereas get_webhook_with_token does not.

Examples

Retrieve a webhook by Id:


let id = 245037420704169985;
let webhook = http.as_ref().get_webhook(id).expect("Error getting webhook");

pub fn get_webhook_with_token(
    &self,
    webhook_id: u64,
    token: &str
) -> Result<Webhook>
[src]

Retrieves a webhook given its Id and unique token.

This method does not require authentication.

Examples

Retrieve a webhook by Id and its unique token:

let id = 245037420704169985;
let token = "ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";

let webhook = http.as_ref().get_webhook_with_token(id, token)
    .expect("Error getting webhook");

pub fn kick_member(&self, guild_id: u64, user_id: u64) -> Result<()>[src]

Kicks a member from a guild.

pub fn kick_member_with_reason(
    &self,
    guild_id: u64,
    user_id: u64,
    reason: &str
) -> Result<()>
[src]

Kicks a member from a guild with a provided reason.

pub fn leave_group(&self, group_id: u64) -> Result<Group>[src]

Leaves a group DM.

pub fn leave_guild(&self, guild_id: u64) -> Result<()>[src]

Leaves a guild.

pub fn remove_group_recipient(&self, group_id: u64, user_id: u64) -> Result<()>[src]

Deletes a user from group DM.

pub fn send_files<'a, T, It: IntoIterator<Item = T>>(
    &self,
    channel_id: u64,
    files: It,
    map: Map<String, Value>
) -> Result<Message> where
    T: Into<AttachmentType<'a>>, 
[src]

Sends file(s) to a channel.

Errors

Returns an HttpError::InvalidRequest(PayloadTooLarge) if the file is too large to send.

pub fn send_message(&self, channel_id: u64, map: &Value) -> Result<Message>[src]

Sends a message to a channel.

pub fn pin_message(&self, channel_id: u64, message_id: u64) -> Result<()>[src]

Pins a message in a channel.

pub fn remove_ban(&self, guild_id: u64, user_id: u64) -> Result<()>[src]

Unbans a user from a guild.

pub fn remove_member_role(
    &self,
    guild_id: u64,
    user_id: u64,
    role_id: u64
) -> Result<()>
[src]

Deletes a single Role from a Member in a Guild.

Note: Requires the Manage Roles permission and respect of role hierarchy.

pub fn start_guild_prune(
    &self,
    guild_id: u64,
    map: &Value
) -> Result<GuildPrune>
[src]

Starts removing some members from a guild based on the last time they've been online.

pub fn start_integration_sync(
    &self,
    guild_id: u64,
    integration_id: u64
) -> Result<()>
[src]

Starts syncing an integration with a guild.

pub fn unpin_message(&self, channel_id: u64, message_id: u64) -> Result<()>[src]

Unpins a message from a channel.

pub fn fire<T: DeserializeOwned>(&self, req: Request) -> Result<T>[src]

Fires off a request, deserializing the response reader via the given type bound.

If you don't need to deserialize the response and want the response instance itself, use request.

Examples

Create a new message via the RouteInfo::CreateMessage endpoint and deserialize the response into a Message:

use serenity::{
    http::{
        routing::RouteInfo,
        request::RequestBuilder,
    },
    model::channel::Message,
};

let bytes = vec![
    // payload bytes here
];
let channel_id = 381880193700069377;
let route_info = RouteInfo::CreateMessage { channel_id };

let mut request = RequestBuilder::new(route_info);
request.body(Some(&bytes));

let message = http.fire::<Message>(request.build())?;

println!("Message content: {}", message.content);

pub fn request(&self, req: Request) -> Result<ReqwestResponse>[src]

Performs a request, ratelimiting it if necessary.

Returns the raw reqwest Response. Use fire to deserialize the response into some type.

Examples

Send a body of bytes over the RouteInfo::CreateMessage endpoint:

use serenity::http::{
    self,
    request::RequestBuilder,
    routing::RouteInfo,
};

let bytes = vec![
    // payload bytes here
];
let channel_id = 381880193700069377;
let route_info = RouteInfo::CreateMessage { channel_id };

let mut request = RequestBuilder::new(route_info);
request.body(Some(&bytes));

let response = http.request(request.build())?;

println!("Response successful?: {}", response.status().is_success());

Trait Implementations

impl AsRef<Http> for Context[src]

impl AsRef<Http> for Http[src]

impl<'_, '_> AsRef<Http> for (&'_ CacheRwLock, &'_ Http)[src]

impl<'_> CacheHttp for &'_ Http[src]

impl Default for Http[src]

Auto Trait Implementations

impl !RefUnwindSafe for Http

impl Send for Http

impl Sync for Http

impl Unpin for Http

impl !UnwindSafe for Http

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> UnsafeAny for T where
    T: Any

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,