Skip to main content

CommandData

Struct CommandData 

Source
#[non_exhaustive]
pub struct CommandData { pub id: CommandId, pub name: CompactString, pub kind: CommandType, pub resolved: Option<Box<Resolved>>, pub options: Vec<CommandDataOption>, pub guild_id: Option<GuildId>, pub target_id: Option<GenericId>, }
Expand description

The data of a command interaction (and of an autocomplete interaction).

discord.js counterpart: the command fields of CommandInteraction (commandId, commandName, commandType, options).

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§id: CommandId

The command’s ID.

§name: CompactString

The command’s name.

§kind: CommandType

The command’s type.

§resolved: Option<Box<Resolved>>

The users, members, roles, channels, messages and attachments the options refer to (and the target of context menu commands).

§options: Vec<CommandDataOption>

The options the user filled in (in autocomplete interactions: possibly partial).

§guild_id: Option<GuildId>

The guild the command is registered in (guild commands).

§target_id: Option<GenericId>

The user or message a context menu command was used on (in resolved).

Implementations§

Source§

impl CommandData

Source

pub fn focused(&self) -> Option<&CommandDataOption>

Returns the option that is being typed in an autocomplete interaction, searching subcommands and groups.

discord.js counterpart: CommandInteractionOptionResolver#getFocused.

Source§

impl CommandData

Reads command options by name and type (D-250).

The getters look at the options of the subcommand that was run (leaf_options), so get_string("reason") works the same for /ban reason:… and /mod ban reason:…. User, member, role, channel and attachment getters return the resolved object behind the option’s ID.

§Examples

use djs_model::interaction::Interaction;

// `/mod warn reason:spam days:7`
let interaction: Interaction = serde_json::from_str(json).unwrap();
let data = interaction.command().unwrap();
assert_eq!(data.subcommand(), Some("warn"));
assert_eq!(data.get_string("reason"), Some("spam"));
assert_eq!(data.get_integer("days"), Some(7));
assert_eq!(data.get_boolean("silent"), None); // not filled in
assert_eq!(data.get_string("days"), None); // another type
Source

pub fn leaf_options(&self) -> &[CommandDataOption]

Returns the options of the subcommand that was run, or the command’s own options if it has no subcommands: the options every getter searches.

discord.js counterpart: the hoisted options of CommandInteractionOptionResolver.

Source

pub fn subcommand(&self) -> Option<&str>

Returns the name of the subcommand that was run (also inside a group).

discord.js counterpart: CommandInteractionOptionResolver#getSubcommand.

Source

pub fn subcommand_group(&self) -> Option<&str>

Returns the name of the subcommand group of the subcommand that was run.

discord.js counterpart: CommandInteractionOptionResolver#getSubcommandGroup.

Source

pub fn get(&self, name: &str) -> Option<&CommandOptionValue>

Returns the value of the option name (among the leaf_options), whatever its type.

discord.js counterpart: CommandInteractionOptionResolver#get.

Source

pub fn get_string(&self, name: &str) -> Option<&str>

Returns the value of the string option name.

discord.js counterpart: CommandInteractionOptionResolver#getString.

Source

pub fn get_integer(&self, name: &str) -> Option<i64>

Returns the value of the integer option name.

discord.js counterpart: CommandInteractionOptionResolver#getInteger.

Source

pub fn get_number(&self, name: &str) -> Option<f64>

Returns the value of the number (floating-point) option name.

discord.js counterpart: CommandInteractionOptionResolver#getNumber.

Source

pub fn get_boolean(&self, name: &str) -> Option<bool>

Returns the value of the boolean option name.

discord.js counterpart: CommandInteractionOptionResolver#getBoolean.

Source

pub fn get_user(&self, name: &str) -> Option<&User>

Returns the user chosen in the user (or mentionable) option name.

discord.js counterpart: CommandInteractionOptionResolver#getUser.

Source

pub fn get_member(&self, name: &str) -> Option<(&User, &InteractionMember)>

Returns the user chosen in the user (or mentionable) option name with their member data; None outside guilds or if the user is not a member.

discord.js counterpart: CommandInteractionOptionResolver#getMember.

Source

pub fn get_role(&self, name: &str) -> Option<&Role>

Returns the role chosen in the role (or mentionable) option name.

discord.js counterpart: CommandInteractionOptionResolver#getRole.

Source

pub fn get_channel(&self, name: &str) -> Option<&InteractionChannel>

Returns the channel chosen in the channel option name (a partial channel with the invoking member’s permissions in it).

discord.js counterpart: CommandInteractionOptionResolver#getChannel.

Source

pub fn get_attachment(&self, name: &str) -> Option<&Attachment>

Returns the file uploaded in the attachment option name.

discord.js counterpart: CommandInteractionOptionResolver#getAttachment.

Source

pub fn get_mentionable(&self, name: &str) -> Option<Mentionable<'_>>

Returns the user or role chosen in the mentionable option name.

discord.js counterpart: CommandInteractionOptionResolver#getMentionable.

Source

pub fn focused_value(&self) -> Option<&str>

Returns what the user has typed so far in the focused option of an autocomplete interaction.

discord.js counterpart: CommandInteractionOptionResolver#getFocused.

Source

pub fn target_user(&self) -> Option<&User>

Returns the user a user command was used on.

discord.js counterpart: UserContextMenuCommandInteraction#targetUser.

Source

pub fn target_member(&self) -> Option<(&User, &InteractionMember)>

Returns the user a user command was used on with their member data (in guilds).

discord.js counterpart: UserContextMenuCommandInteraction#targetMember.

Source

pub fn target_message(&self) -> Option<&Message>

Returns the message a message command was used on.

discord.js counterpart: MessageContextMenuCommandInteraction#targetMessage.

Trait Implementations§

Source§

impl Clone for CommandData

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CommandData

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for CommandData

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for CommandData

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for CommandData

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.