Discord Flows

Discord Integration for [Flows.network](https://test.flows.network)

Quick Start

This is a plain text echo bot.

```rust use discordflows::{getclient, listentoevent, model::Message};

[no_mangle]

[tokio::main(flavor = "current_thread")]

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.

Creating a Bot Account

Some of the following are excerpts from discord.py docs

  1. Make sure you’re logged on to the Discord website.
  2. Navigate to the application page.
  3. Click on the “New Application” button. new-application
  4. Give the application a name and click “Create”. fill-name
  5. Navigate to the “Bot”.
  6. Make sure that Public Bot is ticked if you want others to invite your bot. And tick all the options of the "Privileged Gateway Intents". Privileged Gateway Intents
  7. Click on the "Reset Token" button. reset-token
  8. Confirm reset by clicking "Yes, do it!" button. confirm-reset-token
  9. Copy the token using the “Copy” button. > It should be worth noting that this token is essentially your bot’s password. > You should never share this with someone else. > In doing so, someone can log in to your bot and do malicious things, > such as leaving servers, ban all members inside a server, > or pinging everyone maliciously. > > The possibilities are endless, so do not share this token. > > If you accidentally leaked your token, > click the “Regenerate” button as soon as possible. > This revokes your old token and re-generates a new one. > Now you need to use the new token to login.
  10. Keep your token in a safe place, the token will only be shown once.

Inviting Your Bot

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.

  1. Make sure you’re logged on to the Discord website.
  2. Navigate to the application page
  3. Click on your bot’s page.
  4. Go to the “OAuth2” tab. discord_oauth2
  5. Tick the “bot” checkbox under “scopes”. discord<em>oauth2</em>scope
  6. Tick the permissions required for your bot to function under “Bot Permissions”.
  7. Now the resulting URL can be used to add your bot to a server. Copy and paste the URL into your browser, choose a server to invite the bot to, and click “Authorize”.

The person adding the bot needs “Manage Server” permissions to do so.

Using the default Bot

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};

[no_mangle]

[tokio::main(flavor = "current_thread")]

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