Struct serenity::client::Context
[−]
[src]
pub struct Context { pub channel_id: Option<ChannelId>, pub shard: Arc<Mutex<Shard>>, // some fields omitted }
The context is a general utility struct provided on event dispatches, which
helps with dealing with the current "context" of the event dispatch,
and providing helper methods where possible. The context also acts as a
general high-level interface over the associated Shard
which
received the event, or the low-level rest
module.
For example, when the Client::on_message
handler is dispatched to, the
context will contain the Id of the Channel
that the message was created
for. This allows for using shortcuts like say
, which will
post its given argument to the associated channel for you as a Message
.
Additionally, the context contains "shortcuts", like for interacting with
the shard. Methods like set_game
will unlock the shard and perform an
update for you to save a bit of work.
A context will only live for the event it was dispatched for. After the event handler finished, it is destroyed and will not be re-used.
Automatically using the Cache
The context makes use of the Cache
being global, and will first check
the cache for associated data before hitting the REST API. This is to save
Discord requests, and ultimately save your bot bandwidth and time. This also
acts as a clean interface for retrieving from the cache without needing to
check it yourself first, and then performing a request if it does not exist.
The context ultimately acts as a means to simplify these two operations into
one.
For example, if you are needing information about a
channel within a guild, then you can
use get_channel
to retrieve it. Under most circumstances, the guild and
its channels will be cached within the cache, and get_channel
will just
pull from the cache. If it does not exist, it will make a request to the
REST API, and then insert a clone of the channel into the cache, returning
you the channel.
In this scenario, now that the cache has the channel, performing the same
request to get_channel
will instead pull from the cache, as it is now
cached.
Fields
channel_id: Option<ChannelId>
The Id of the relevant channel, if there is one. This is present on the
on_message
handler, for example.
The associated shard which dispatched the event handler.
Note that if you are sharding, in relevant terms, this is the shard which received the event being dispatched.
Methods
impl Context
[src]
fn accept_invite(&self, invite: &str) -> Result<Invite>
Accepts the given invite.
Refer to the documentation for rest::accept_invite
for restrictions
on accepting an invite.
Note: Requires that the current user be a user account.
Errors
Returns a ClientError::InvalidOperationAsBot
if the current user is
a bot user.
fn ack<C, M>(&self, channel_id: C, message_id: M) -> Result<()> where C: Into<ChannelId>, M: Into<MessageId>
Marks a Channel
as being read up to a certain Message
.
Refer to the documentation for rest::ack_message
for more
information.
Errors
Returns a ClientError::InvalidOperationAsBot
if the current user is
a bot user.
fn ban<G, U>(&self,
guild_id: G,
user_id: U,
delete_message_days: u8)
-> Result<()> where G: Into<GuildId>, U: Into<UserId>
guild_id: G,
user_id: U,
delete_message_days: u8)
-> Result<()> where G: Into<GuildId>, U: Into<UserId>
Bans a User
from a Guild
, removing their messages sent in the
last X number of days.
Refer to the documentation for rest::ban_user
for more information.
Note: Requires that you have the Ban Members permission.
Examples
Ban the user that sent a message for 7
days:
// assuming you are in a context context.ban_user(context.guild_id, context.message.author, 7);
Errors
Returns a ClientError::DeleteMessageDaysAmount
if the number of days
given is over the maximum allowed.
fn broadcast_typing<C>(&self, channel_id: C) -> Result<()> where C: Into<ChannelId>
Broadcasts that you are typing to a channel for the next 5 seconds.
After 5 seconds, another request must be made to continue broadcasting that you are typing.
This should rarely be used for bots, and should likely only be used for signifying that a long-running command is still being executed.
Examples
// assuming you are in a context context.broadcast_typing(context.channel_id);
fn create_channel<G>(&self,
guild_id: G,
name: &str,
kind: ChannelType)
-> Result<Channel> where G: Into<GuildId>
guild_id: G,
name: &str,
kind: ChannelType)
-> Result<Channel> where G: Into<GuildId>
Creates a GuildChannel
in the given Guild
.
Refer to rest::create_channel
for more information.
Note: Requires the Manage Channels permission.
Examples
Create a voice channel in a guild with the name test
:
use serenity::model::ChannelType; context.create_channel(context.guild_id, "test", ChannelType::Voice);
fn create_emoji<G>(&self, guild_id: G, name: &str, image: &str) -> Result<Emoji> where G: Into<GuildId>
Creates an emoji in the given guild with a name and base64-encoded
image. The utils::read_image
function is provided for you as a
simple method to read an image and encode it into base64, if you are
reading from the filesystem.
Note: Requires the Manage Emojis permission.
Examples
See the EditProfile::avatar
example for an in-depth example as to
how to read an image from the filesystem and encode it as base64. Most
of the example can be applied similarly for this method.
fn create_guild(&self,
name: &str,
region: Region,
icon: Option<&str>)
-> Result<PartialGuild>
name: &str,
region: Region,
icon: Option<&str>)
-> Result<PartialGuild>
Creates a guild with the data provided.
Only a PartialGuild
will be immediately returned, and a full
Guild
will be received over a Shard
.
Note: This endpoint is usually only available for user accounts. Refer to Discord's information for the endpoint here for more information. If you require this as a bot, re-think what you are doing and if it really needs to be doing this.
Examples
Create a guild called "test"
in the US West region with no icon:
use serenity::model::Region; context.create_guild("test", Region::UsWest, None);
fn create_integration<G, I>(&self,
guild_id: G,
integration_id: I,
kind: &str)
-> Result<()> where G: Into<GuildId>, I: Into<IntegrationId>
guild_id: G,
integration_id: I,
kind: &str)
-> Result<()> where G: Into<GuildId>, I: Into<IntegrationId>
Creates an Integration
for a Guild
.
Note: Requires the Manage Guild permission.
fn create_invite<C, F>(&self, channel_id: C, f: F) -> Result<RichInvite> where C: Into<ChannelId>, F: FnOnce(CreateInvite) -> CreateInvite
Creates an invite for the channel, providing a builder so that fields may optionally be set.
See the documentation for the CreateInvite
builder for information
on how to use this and the default values that it provides.
Note: Requires the Create Invite permission.
fn create_permission<C>(&self,
channel_id: C,
target: PermissionOverwrite)
-> Result<()> where C: Into<ChannelId>
channel_id: C,
target: PermissionOverwrite)
-> Result<()> where C: Into<ChannelId>
Creates a permission overwrite for either a
single Member
or Role
within a Channel
.
Refer to the documentation for PermissionOverwrite
s for more
information.
Note: Requires the Manage Channels permission.
Examples
Creating a permission overwrite for a member by specifying the
PermissionOverwrite::Member
variant, allowing it the [Send Messages]
permission, but denying the Send TTS Messages and Attach Files
permissions:
use serenity::model::{ChannelId, PermissionOverwrite, permissions}; // assuming you are in a context let channel_id = 7; let user_id = 8; let allow = permissions::SEND_MESSAGES; let deny = permissions::SEND_TTS_MESSAGES | permissions::ATTACH_FILES; let overwrite = PermissionOverwrite { allow: allow, deny: deny, kind: PermissionOverwriteType::Member(user_id), }; let _result = context.create_permission(channel_id, overwrite);
Creating a permission overwrite for a role by specifying the
[PermissionOverwrite::Role
] variant, allowing it the Manage Webhooks
permission, but denying the Send TTS Messages and Attach Files
permissions:
use serenity::model::{ChannelId, PermissionOverwrite, permissions}; // assuming you are in a context let channel_id = 7; let user_id = 8; let allow = permissions::SEND_MESSAGES; let deny = permissions::SEND_TTS_MESSAGES | permissions::ATTACH_FILES; let overwrite = PermissionOverwrite { allow: allow, deny: deny, kind: PermissionOverwriteType::Member(user_id), }; let _result = context.create_permission(channel_id, overwrite);
fn create_direct_message_channel<U>(&self, user_id: U) -> Result<PrivateChannel> where U: Into<UserId>
Creates a direct message channel between the current user and another
User
. This can also retrieve the channel if one already exists.
fn create_reaction<C, M, R>(&self,
channel_id: C,
message_id: M,
reaction_type: R)
-> Result<()> where C: Into<ChannelId>, M: Into<MessageId>, R: Into<ReactionType>
channel_id: C,
message_id: M,
reaction_type: R)
-> Result<()> where C: Into<ChannelId>, M: Into<MessageId>, R: Into<ReactionType>
React to a Message
with a custom Emoji
or unicode character.
Message::react
may be a more suited method of reacting in most
cases.
Note: Requires the Add Reactions permission, if the current user is the first user to perform a react with a certain emoji.
fn create_role<F, G>(&self, guild_id: G, f: F) -> Result<Role> where F: FnOnce(EditRole) -> EditRole, G: Into<GuildId>
Creates a Role
in guild with given Id. Second argument is a
closure, and you can use it to automatically configure role.
Examples
This creates a role which can be mentioned, with name 'test':
let role = context.create_role(guild_id, |r| r .hoist(true) .name("role"));
fn delete_channel<C>(&self, channel_id: C) -> Result<Channel> where C: Into<ChannelId>
Deletes a Channel
based on the Id given.
If the channel being deleted is a GuildChannel
, then the Manage Channels permission is required.
fn delete_emoji<E, G>(&self, guild_id: G, emoji_id: E) -> Result<()> where E: Into<EmojiId>, G: Into<GuildId>
Deletes an emoji in a Guild
given its Id.
Note: Requires the Manage Emojis permission.
fn delete_guild<G: Into<GuildId>>(&self, guild_id: G) -> Result<PartialGuild>
Deletes a Guild
. The current user must be the guild owner to be able
to delete it.
Only a PartialGuild
will be immediately returned.
fn delete_integration<G, I>(&self, guild_id: G, integration_id: I) -> Result<()> where G: Into<GuildId>, I: Into<IntegrationId>
Deletes an integration by Id from a guild which Id was given.
fn delete_invite(&self, invite: &str) -> Result<Invite>
Deletes the given invite.
Refer to the documentation for Invite::delete
for restrictions on
deleting an invite.
Errors
Returns a ClientError::InvalidPermissions
if the current user does
not have the required [permission].
fn delete_message<C, M>(&self, channel_id: C, message_id: M) -> Result<()> where C: Into<ChannelId>, M: Into<MessageId>
Deletes a Message
given its Id.
Examples
Deleting every message that is received:
use serenity::Client; use std::env; let client = Client::login_bot(&env::var("DISCORD_BOT_TOKEN").unwrap()); client.on_message(|context, message| { context.delete_message(message); });
(in practice, please do not do this)
fn delete_messages<C>(&self,
channel_id: C,
message_ids: &[MessageId])
-> Result<()> where C: Into<ChannelId>
channel_id: C,
message_ids: &[MessageId])
-> Result<()> where C: Into<ChannelId>
Deletes all messages by Ids from the given vector in the given channel.
The minimum amount of messages is 2 and the maximum amount is 100.
Note: This uses bulk delete endpoint which is not available for user accounts.
fn delete_note<U: Into<UserId>>(&self, user_id: U) -> Result<()>
Deletes a profile note from a user.
fn delete_permission<C>(&self,
channel_id: C,
permission_type: PermissionOverwriteType)
-> Result<()> where C: Into<ChannelId>
channel_id: C,
permission_type: PermissionOverwriteType)
-> Result<()> where C: Into<ChannelId>
Deletes all permission overrides in a channel from a member or a role.
fn delete_reaction<C, M, R>(&self,
channel_id: C,
message_id: M,
user_id: Option<UserId>,
reaction_type: R)
-> Result<()> where C: Into<ChannelId>, M: Into<MessageId>, R: Into<ReactionType>
channel_id: C,
message_id: M,
user_id: Option<UserId>,
reaction_type: R)
-> Result<()> where C: Into<ChannelId>, M: Into<MessageId>, R: Into<ReactionType>
Deletes the given Reaction
.
Note: Requires the Manage Messages permission, if the current user did not perform the reaction.
fn delete_role<G, R>(&self, guild_id: G, role_id: R) -> Result<()> where G: Into<GuildId>, R: Into<RoleId>
Deletes a role by Id from the given guild.
fn dm<C: Into<ChannelId>>(&self, target_id: C, content: &str) -> Result<Message>
Sends a message to a user through a direct message channel. This is a channel that can only be accessed by you and the recipient.
Examples
There are three ways to send a direct message to someone, the first being an unrelated, although equally helpful method.
Sending a message via User::dm
:
// assuming you are in a context let _ = context.message.author.dm("Hello!");
Sending a message to a PrivateChannel
:
assuming you are in a context let private_channel = context.create_private_channel(context.message.author.id); let _ = context.direct_message(private_channel, "Test!");
Sending a message to a PrivateChannel
given its ID:
use serenity::Client; use std::env; let mut client = Client::login_bot(&env::var("DISCORD_BOT_TOKEN").unwrap()); client.on_message(|context, message| { if message.content == "!pm-me" { let channel = context.create_private_channel(message.author.id) .unwrap(); let _ = channel.send_message("test!"); } });
fn edit_channel<C, F>(&self, channel_id: C, f: F) -> Result<GuildChannel> where C: Into<ChannelId>, F: FnOnce(EditChannel) -> EditChannel
Edits the settings of a Channel
, optionally setting new values.
Refer to EditChannel
's documentation for its methods.
Examples
Change a voice channel's name and bitrate:
context.edit_channel(channel_id, |c| c .name("test") .bitrate(64000));
fn edit_emoji<E, G>(&self,
guild_id: G,
emoji_id: E,
name: &str)
-> Result<Emoji> where E: Into<EmojiId>, G: Into<GuildId>
guild_id: G,
emoji_id: E,
name: &str)
-> Result<Emoji> where E: Into<EmojiId>, G: Into<GuildId>
Edits an Emoji
's name.
fn edit_guild<F, G>(&self, guild_id: G, f: F) -> Result<PartialGuild> where F: FnOnce(EditGuild) -> EditGuild, G: Into<GuildId>
Edits the settings of a Guild
, optionally setting new values.
Refer to EditGuild
's documentation for a full list of methods.
Examples
Change a guild's icon using a file name "icon.png":
use serenity::utils; // We are using read_image helper function from utils. let base64_icon = utils::read_image("./icon.png") .expect("Failed to read image"); context.edit_guild(guild_id, |g| g.icon(base64_icon));
fn edit_member<F, G, U>(&self, guild_id: G, user_id: U, f: F) -> Result<()> where F: FnOnce(EditMember) -> EditMember, G: Into<GuildId>, U: Into<UserId>
Edits the properties of member of a guild, such as muting or nicknaming them.
Refer to EditMember
's documentation for a full list of methods.
Examples
Mute a member and set their roles to just one role with a predefined Id:
context.edit_member(guild_id, user_id, |m| m .mute(true) .roles(&vec![role_id]));
fn edit_nickname<G>(&self,
guild_id: G,
new_nickname: Option<&str>)
-> Result<()> where G: Into<GuildId>
guild_id: G,
new_nickname: Option<&str>)
-> Result<()> where G: Into<GuildId>
Edits the current user's nickname for the provided Guild
via its Id.
Pass None
to reset the nickname.
Note: Requires the Change Nickname permission.
fn edit_profile<F: FnOnce(EditProfile) -> EditProfile>(&mut self,
f: F)
-> Result<CurrentUser>
f: F)
-> Result<CurrentUser>
Edits the current user's settings.
Refer to EditProfile
's documentation for its methods.
Examples
Change our username:
context.edit_member(|p| p.username("meew zero"));
fn edit_role<F, G, R>(&self, guild_id: G, role_id: R, f: F) -> Result<Role> where F: FnOnce(EditRole) -> EditRole, G: Into<GuildId>, R: Into<GuildId>
Edits a Role
, optionally setting its new fields.
Examples
Make a role hoisted:
context.edit_role(guild_id, role_id, |r| r .hoist(true));
fn edit_message<C, F, M>(&self,
channel_id: C,
message_id: M,
text: &str,
f: F)
-> Result<Message> where C: Into<ChannelId>, F: FnOnce(CreateEmbed) -> CreateEmbed, M: Into<MessageId>
channel_id: C,
message_id: M,
text: &str,
f: F)
-> Result<Message> where C: Into<ChannelId>, F: FnOnce(CreateEmbed) -> CreateEmbed, M: Into<MessageId>
Edits a Message
given its Id and the Id of the channel it belongs
to.
Pass an empty string (""
) to text
if you are editing a message with
an embed or file but no content. Otherwise, text
must be given.
fn edit_note<U: Into<UserId>>(&self, user_id: U, note: &str) -> Result<()>
Edits the note that the current user has set for another user.
Use delete_note
to remove a note.
Note: Requires that the current user be a user account.
Examples
Set a note for a message's author:
// assuming a `message` has been bound let _ = context.edit_note(message.author, "test note");
Errors
Returns a ClientError::InvalidOperationAsBot
if the current user is
a bot user.
fn get_bans<G: Into<GuildId>>(&self, guild_id: G) -> Result<Vec<Ban>>
Gets a list of the given Guild
's bans.
Requires the Ban Members permission.
fn get_channel_invites<C: Into<ChannelId>>(&self,
channel_id: C)
-> Result<Vec<RichInvite>>
channel_id: C)
-> Result<Vec<RichInvite>>
Gets all of a GuildChannel
's invites.
Requires the Manage Guild permission.
fn get_channel<C>(&self, channel_id: C) -> Result<Channel> where C: Into<ChannelId>
Gets a Channel
by the given Id.
fn get_channels<G>(&self,
guild_id: G)
-> Result<HashMap<ChannelId, GuildChannel>> where G: Into<GuildId>
guild_id: G)
-> Result<HashMap<ChannelId, GuildChannel>> where G: Into<GuildId>
Gets all channels of a guild with given Id.
fn get_emoji<E, G>(&self, guild_id: G, emoji_id: E) -> Result<Emoji> where E: Into<EmojiId>, G: Into<GuildId>
Gets an guild's emoji by Id.
fn get_emojis<G: Into<GuildId>>(&self, guild_id: G) -> Result<Vec<Emoji>>
Gets a list of all emojis a guild has.
fn get_guild<G: Into<GuildId>>(&self, guild_id: G) -> Result<PartialGuild>
Gets a guild by its Id.
Requires that the current user be in the guild.
fn get_guild_invites<G>(&self, guild_id: G) -> Result<Vec<RichInvite>> where G: Into<GuildId>
Gets all of a guild's invites.
Requires the Manage Guild permission.
fn get_guild_prune_count<G>(&self, guild_id: G, days: u16) -> Result<GuildPrune> where G: Into<GuildId>
Gets the number of Member
s that would be pruned with the given
number of days.
fn get_guilds(&self) -> Result<Vec<GuildInfo>>
Gets all guilds that the current user is in.
fn get_integrations<G: Into<GuildId>>(&self,
guild_id: G)
-> Result<Vec<Integration>>
guild_id: G)
-> Result<Vec<Integration>>
Gets all integrations of a guild via the given Id.
fn get_invite(&self, invite: &str) -> Result<Invite>
Gets the information about an invite.
fn get_member<G, U>(&self, guild_id: G, user_id: U) -> Result<Member> where G: Into<GuildId>, U: Into<UserId>
fn get_members<G, U>(&self,
guild_id: G,
limit: Option<u64>,
after: Option<U>)
-> Result<Vec<Member>> where G: Into<GuildId>, U: Into<UserId>
guild_id: G,
limit: Option<u64>,
after: Option<U>)
-> Result<Vec<Member>> where G: Into<GuildId>, U: Into<UserId>
Gets a list of a Guild
's members.
Optionally pass in the limit
to limit the number of results. Maximum
value is 1000. Optionally pass in after
to offset the results by a
User
's Id.
fn get_message<C, M>(&self, channel_id: C, message_id: M) -> Result<Message> where C: Into<ChannelId>, M: Into<MessageId>
Gets a single Message
from a Channel
.
Requires the Read Message History permission.
Errors
Returns a ClientError::InvalidOperationAsUser
if the current user is
not a user account.
fn get_messages<C, F>(&self, channel_id: C, f: F) -> Result<Vec<Message>> where C: Into<ChannelId>, F: FnOnce(GetMessages) -> GetMessages
Gets messages from a specific channel.
Examples
let role = context.get_messages(channel_id, |g| g .before(20) .after(100)); // Maximum is 100.
fn get_reaction_users<C, M, R, U>(&self,
channel_id: C,
message_id: M,
reaction_type: R,
limit: Option<u8>,
after: Option<U>)
-> Result<Vec<User>> where C: Into<ChannelId>, M: Into<MessageId>, R: Into<ReactionType>, U: Into<UserId>
channel_id: C,
message_id: M,
reaction_type: R,
limit: Option<u8>,
after: Option<U>)
-> Result<Vec<User>> where C: Into<ChannelId>, M: Into<MessageId>, R: Into<ReactionType>, U: Into<UserId>
Gets the list of User
s who have reacted to a Message
with a
certain Emoji
.
The default limit
is 50
- specify otherwise to receive a different
maximum number of users. The maximum that may be retrieve at a time is
100
, if a greater number is provided then it is automatically reduced.
The optional after
attribute is to retrieve the users after a certain
user. This is useful for pagination.
Note: Requires the Read Message History permission.
Errors
Returns a ClientError::InvalidPermissions
if the current user does
not have the required [permissions].
fn kick_member<G, U>(&self, guild_id: G, user_id: U) -> Result<()> where G: Into<GuildId>, U: Into<UserId>
Kicks a Member
from the specified Guild
if they are in it.
Requires the Kick Members permission.
fn leave_guild<G: Into<GuildId>>(&self, guild_id: G) -> Result<PartialGuild>
Leaves a guild by its Id.
fn move_member<C, G, U>(&self,
guild_id: G,
user_id: U,
channel_id: C)
-> Result<()> where C: Into<ChannelId>, G: Into<ChannelId>, U: Into<ChannelId>
guild_id: G,
user_id: U,
channel_id: C)
-> Result<()> where C: Into<ChannelId>, G: Into<ChannelId>, U: Into<ChannelId>
Moves a member to a specific voice channel.
fn get_pins<C>(&self, channel_id: C) -> Result<Vec<Message>> where C: Into<ChannelId>
fn pin<C, M>(&self, channel_id: C, message_id: M) -> Result<()> where C: Into<ChannelId>, M: Into<MessageId>
Pins a message in the specified channel by the given Id.
fn say(&self, content: &str) -> Result<Message>
Sends a message with just the given message content in the channel that a message was received from.
Note: This will only work when a Message
is received.
Errors
Returns a ClientError::MessageTooLong
if the content of the message
is over the above limit, containing the number of unicode code points
over the limit.
Returns a ClientError::NoChannelId
when there is no ChannelId
directly available.
fn send_file<C, F, R>(&self,
channel_id: C,
file: R,
filename: &str,
f: F)
-> Result<Message> where C: Into<ChannelId>, F: FnOnce(CreateMessage) -> CreateMessage, R: Read
channel_id: C,
file: R,
filename: &str,
f: F)
-> Result<Message> where C: Into<ChannelId>, F: FnOnce(CreateMessage) -> CreateMessage, R: Read
Sends a file along with optional message contents. The filename must be specified.
Message contents may be passed by using the CreateMessage::content
method.
An embed can not be sent when sending a file. If you set one, it will be automatically removed.
Note: Message contents must be under 2000 unicode code points.
Errors
If the content of the message is over the above limit, then a
ClientError::MessageTooLong
will be returned, containing the number
of unicode code points over the limit.
fn send_message<C, F>(&self, channel_id: C, f: F) -> Result<Message> where C: Into<ChannelId>, F: FnOnce(CreateMessage) -> CreateMessage
Sends a message to a Channel
.
Refer to the documentation for CreateMessage
for more information
regarding message restrictions and requirements.
Note: Message contents must be under 2000 unicode code points.
Example
Send a message with just the content test
:
// assuming you are in a context let _ = context.send_message(message.channel_id, |f| f.content("test"));
Send a message on !ping
with a very descriptive Embed
. This sends
a message content of "Pong! Here's some info"
, with an embed with the
following attributes:
- Dark gold in colour;
- A description of
"Information about the message just posted"
; - A title of
"Message Information"
; - A URL of
"https://rust-lang.org"
; - An author structure containing an icon and the user's name;
- An inline field structure containing the message's content with a label;
- An inline field containing the channel's name with a label;
- A footer containing the current user's icon and name, saying that the information was generated by them.
use serenity::client::{CACHE, Client, Context}; use serenity::model::{Channel, Message}; use serenity::utils::Colour; use std::env; let mut client = Client::login_bot(&env::var("DISCORD_TOKEN").unwrap()); client.with_framework(|f| f .configure(|c| c.prefix("~")) .on("ping", ping)); client.on_ready(|_context, ready| { println!("{} is connected!", ready.user.name); }); let _ = client.start(); fn ping(context: &Context, message: &Message, _arguments: Vec<String>) { let cache = CACHE.read().unwrap(); let channel = cache.get_guild_channel(message.channel_id); let _ = context.send_message(message.channel_id, |m| m .content("Pong! Here's some info") .embed(|e| e .colour(Colour::dark_gold()) .description("Information about the message just posted") .title("Message information") .url("https://rust-lang.org") .author(|mut a| { a = a.name(&message.author.name); if let Some(avatar) = message.author.avatar_url() { a = a.icon_url(&avatar); } a }) .field(|f| f .inline(true) .name("Message content:") .value(&message.content)) .field(|f| f .inline(true) .name("Channel name:") .value(&channel.map_or_else(|| "Unknown", |c| &c.name))) .footer(|mut f| { f = f.text(&format!("Generated by {}", cache.user.name)); if let Some(avatar) = cache.user.avatar_url() { f = f.icon_url(&avatar); } f }))); }
Note that for most use cases, your embed layout will not be this ugly. This is an example of a very involved and conditional embed.
Errors
Returns a ClientError::MessageTooLong
if the content of the message
is over the above limit, containing the number of unicode code points
over the limit.
fn online(&self)
Sets the current user as being Online
. This maintains the current
game and afk
setting.
fn idle(&self)
Sets the current user as being Idle
. This maintains the current
game and afk
setting.
fn dnd(&self)
Sets the current user as being DoNotDisturb
. This maintains the
current game and afk
setting.
fn invisible(&self)
Sets the current user as being Invisible
. This maintains the current
game and afk
setting.
fn reset_presence(&self)
"Resets" the current user's presence, by setting the game to None
,
the online status to Online
, and afk
to false
.
Use set_presence
for fine-grained control over individual details.
fn set_game(&self, game: Game)
Sets the current game, defaulting to an online status of Online
, and
setting afk
to false
.
Examples
Set the current user as playing "Heroes of the Storm":
use serenity::model::Game; // assuming you are in a context context.set_game(Game::playing("Heroes of the Storm"));
fn set_game_name(&self, game_name: &str)
Sets the current game, passing in only its name. This will automatically
set the current user's OnlineStatus
to Online
, and its
GameType
as Playing
.
Use reset_presence
to clear the current game, or set_presence
for more fine-grained control.
fn set_presence(&self, game: Option<Game>, status: OnlineStatus, afk: bool)
Sets the current user's presence, providing all fields to be passed.
Examples
Setting the current user as having no game, being Idle
,
and setting afk
to true
:
use serenity::model::OnlineStatus; // assuming you are in a context context.set_game(None, OnlineStatus::Idle, true);
Setting the current user as playing "Heroes of the Storm", being
DoNotDisturb
, and setting afk
to false
:
use serenity::model::{Game, OnlineStatus}; // assuming you are in a context let game = Game::playing("Heroes of the Storm"); let status = OnlineStatus::DoNotDisturb; context.set_game(Some(game), status, false);
fn start_guild_prune<G>(&self, guild_id: G, days: u16) -> Result<GuildPrune> where G: Into<GuildId>
Deletes an undefined amount of members from the given guild based on the amount of days they've been offline for.
Note: This will trigger GuildMemberRemove
events
fn start_integration_sync<G, I>(&self,
guild_id: G,
integration_id: I)
-> Result<()> where G: Into<GuildId>, I: Into<IntegrationId>
guild_id: G,
integration_id: I)
-> Result<()> where G: Into<GuildId>, I: Into<IntegrationId>
Starts integration synchronization by the given integration Id.
fn unban<G, U>(&self, guild_id: G, user_id: U) -> Result<()> where G: Into<GuildId>, U: Into<UserId>
Requires the Ban Members permission.
fn unpin<C, M>(&self, channel_id: C, message_id: M) -> Result<()> where C: Into<ChannelId>, M: Into<MessageId>
Trait Implementations
impl Clone for Context
[src]
fn clone(&self) -> Context
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more