Expand description
Descord is a minimal and easy to use discord api wrapper.
§Example
use descord::prelude::*;
#[tokio::main]
async fn main() {
let mut client = Client::new(
"TOKEN",
GatewayIntent::NON_PRIVILEGED
// for message commands
| GatewayIntent::MESSAGE_CONTENT,
"!", // default prefix for message command
)
.await;
// register commands, events and slash commands manually
client.register_commands(vec![echo()]);
client.register_events(vec![ready()]);
client.register_slash_commands(vec![avatar()]).await;
// alternatively you can do this, this is neat but
// it is very counterintuitive since it read
// the files and find functions marked with
// proc macros
// register_all!(client => ["src/main.rs", "src/file1.rs"]);
// start the bot!
client.login().await;
}
// An event handler
#[descord::event] // Alternatively you can do `#[descord::event(ready)]`
async fn ready(data: ReadyData) {
println!(
"Logged in as {}#{}",
data.user.username, data.user.discriminator
)
}
// A message command
//
// you can also do `#[descord::command(prefix = "new_prefix")]` to change
// the command prefix for this command to change
// the command prefix for this command
#[descord::command]
async fn echo(
/// information about the messgae
msg: Message,
/// some types can be parsed automatically
echo_what: String,
) {
msg.reply(echo_what).await;
}
// A slash command
#[descord::slash(description = "Get a user's avatar")]
async fn avatar(
interaction: Interaction,
#[doc = "User to fetch avatar from"] user: Option<User>,
) {
let member = interaction.member.as_ref().unwrap();
let (username, avatar_url) = match user {
Some(user) => (
&user.username,
user.get_avatar_url(ImageFormat::WebP, None).unwrap(),
),
_ => (
&member.user.as_ref().unwrap().username,
member.get_avatar_url(ImageFormat::WebP, None).unwrap(),
),
};
// Creating an embed
let embed = EmbedBuilder::new()
.color(Color::Orange)
.title(&format!("{username}'s avatar"))
.image(avatar_url, None, None)
.build();
interaction.reply(embed, false).await;
}
Modules§
Macros§
- Usage:
register_all!(client => ["src/file.rs", "src/file2.rs"]);
Whereclient
is the client object and the array is the list of files to search for events, commands, and slash commands. If the array is empty, it will recursively search for files in thesrc
directory. - Usage:
register_all_commands!(client => ["src/commands.rs", "src/commands2.rs"]);
Whereclient
is the client object and the array is the list of files to search for commands. If the array is empty, it will recursively search for files in thesrc
directory. - Usage:
register_all_events!(client => ["src/events.rs", "src/events2.rs"]);
Whereclient
is the client object and the array is the list of files to search for events. If the array is empty, it will recursively search for files in thesrc
directory.
Structs§
- The main client struct for interacting with the Discord API.