#discord-api #twilight #bucket #discord

twilight-bucket

Wait n seconds before running this again

5 releases

0.2.2 May 15, 2022
0.2.1 Apr 7, 2022
0.1.3 Mar 30, 2022

#26 in #twilight

MIT license

11KB
114 lines

docs.rs - read crates.io - view

Twilight Bucket

A utility crate to limit users' usage, a third party crate of the Twilight ecosystem

Refer to the docs for more

This crate can be used with any library, but it shares Twilight's non-goals, such as trying to be more verbose and less opinionated and Serenity already has a bucket implementation

Contributing

Contributions or even opening issues are very welcomed

Licence

Licensed MIT, this is a third party crate that's not affiliated with the Twilight organization


lib.rs:

Twilight Bucket

A utility crate to limit users' usage, a third party crate of the Twilight ecosystem

All the functionality of this crate is under Bucket, see its documentation for usage info

This crate can be used with any library, but it shares Twilight's non-goals, such as trying to be more verbose and less opinionated and Serenity already has a bucket implementation

Example

use std::{num::NonZeroU64, time::Duration};

use twilight_bucket::{Bucket, Limit};

#[tokio::main]
async fn main() {
    // A user can use it once every 10 seconds
    let my_command_user_bucket = Bucket::new(Limit::new(Duration::from_secs(10), 1));
    // It can be used up to 5 times every 30 seconds in one channel
    let my_command_channel_bucket = Bucket::new(Limit::new(Duration::from_secs(30), 5));
    run_my_command(
        my_command_user_bucket,
        my_command_channel_bucket,
        12345,
        123,
    )
    .await;
}

async fn run_my_command(
    user_bucket: Bucket,
    channel_bucket: Bucket,
    user_id: u64,
    channel_id: u64,
) -> String {
    if let Some(channel_limit_duration) = channel_bucket.limit_duration(channel_id) {
        return format!(
            "This was used too much in this channel, please wait {} seconds",
            channel_limit_duration.as_secs()
        );
    }
    if let Some(user_limit_duration) = user_bucket.limit_duration(user_id) {
        if Duration::from_secs(5) > user_limit_duration {
            tokio::time::sleep(user_limit_duration).await;
        } else {
            return format!(
                "You've been using this too much, please wait {} seconds",
                user_limit_duration.as_secs()
            );
        }
    }
    user_bucket.register(user_id);
    channel_bucket.register(channel_id);
    "Ran your command".to_owned()
}

Dependencies

~1–5.5MB
~21K SLoC