-
Notifications
You must be signed in to change notification settings - Fork 597
/
main.rs
64 lines (57 loc) · 2.5 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::env;
use serenity::async_trait;
use serenity::builder::{CreateAttachment, CreateEmbed, CreateEmbedFooter, CreateMessage};
use serenity::model::channel::Message;
use serenity::model::gateway::Ready;
use serenity::model::Timestamp;
use serenity::prelude::*;
struct Handler;
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == "!hello" {
// The create message builder allows you to easily create embeds and messages using a
// builder syntax.
// This example will create a message that says "Hello, World!", with an embed that has
// a title, description, an image, three fields, and a footer.
let footer = CreateEmbedFooter::new("This is a footer");
let embed = CreateEmbed::new()
.title("This is a title")
.description("This is a description")
.image("attachment://ferris_eyes.png")
.fields(vec![
("This is the first field", "This is a field body", true),
("This is the second field", "Both fields are inline", true),
])
.field("This is the third field", "This is not an inline field", false)
.footer(footer)
// Add a timestamp for the current time
// This also accepts a rfc3339 Timestamp
.timestamp(Timestamp::now());
let builder = CreateMessage::new()
.content("Hello, World!")
.embed(embed)
.add_file(CreateAttachment::path("./ferris_eyes.png").await.unwrap());
let msg = msg.channel_id.send_message(&ctx.http, builder).await;
if let Err(why) = msg {
println!("Error sending message: {why:?}");
}
}
}
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
#[tokio::main]
async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client =
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
if let Err(why) = client.start().await {
println!("Client error: {why:?}");
}
}