This is a plain text echo bot.
```rust use discordflows::{getclient, listentoevent, model::Message};
pub async fn run() { let token = std::env::var("DISCORD_TOKEN").unwrap();
listen_to_event(token.clone(), move |msg| handle(msg, token)).await;
}
async fn handle(msg: Message, token: String) { let client = getclient(token); let channelid = msg.channel_id; let content = msg.content;
if msg.author.bot {
return;
}
_ = client
.send_message(
channel_id.into(),
&serde_json::json!({
"content": content,
}),
)
.await;
} ```
[get_client()] is a Discord
constructor that represents a bot.
If you don't have a token, please see this section.
[listentoevent()] is responsible for registering a listener for the bot
represented by the bot_token
. When a new Message
coming, the callback
is called with received Message
.
Some of the following are excerpts from discord.py docs
So you’ve made a Bot User but it’s not actually in any server.
If you want to invite your bot you must create an invite URL for it.
The person adding the bot needs “Manage Server” permissions to do so.
If you don't want to create your own Discord Bot, we have created a pub Bot which can be used by all users.
```rust use discordflows::{getclient, listentoevent, model::Message, Bot};
pub async fn run() { listentoevent(Bot::Default, move |msg| handle(msg)).await; }
async fn handle(msg: Message) { let client = getclient(Bot::Default); let channelid = msg.channel_id; let content = msg.content;
if msg.author.bot {
return;
}
_ = client
.send_message(
channel_id.into(),
&serde_json::json!({
"content": content,
}),
)
.await;
} ```
To invite this public Bot, you should compose the auth url with the permissions and scope
and replace the client_id
by 1090851501473271919
.
For example, if the scope is bot
and permissions is Send Messages
then the auth url should look like:
https://discord.com/api/oauth2/authorize?client_id=1090851501473271919&permissions=2048&scope=bot