Module serenity_utils::conversion
source · [−]please use serenity’s ArgumentConvert trait
Expand description
Provides a trait to convert strings into serenity’s guild-specific models.
The trait provides two methods:
The first method is available only when cache
feature is enabled. The
second method is always available.
Limitation
If the cache
feature is not enabled, an argument is only treated as an ID
or mention when trying to convert to Member
. It is not treated as user
name, nickname or user tag.
Example
Bring Conversion
trait into scope.
use serenity_utils::conversion::Conversion;
If cache is enabled and you require Guild
for other purposes too,
use from_guild_and_str
method so the guild is not fetched multiple times.
async fn foo(ctx: &Context, msg: &Message, arg: &str) {
let guild = match msg.guild(&ctx.cache) {
Some(g) => g,
None => return,
};
// Tries to get member from guild and the argument.
let opt_member = Member::from_guild_and_str(&guild, arg).await;
}
If cache is disabled or if cache is enabled but you don’t require Guild
for other purposes, use from_guild_id_and_str
method.
async fn bar(ctx: &Context, msg: &Message, arg: &str) {
// Tries to get role from guild id and the argument.
if let Some(guild_id) = msg.guild_id {
let opt_role = Role::from_guild_id_and_str(ctx, guild_id, arg).await;
}
}
Traits
A trait to convert a string into serenity’s models.