Skip to main content

Crate stoat

Crate stoat 

Source
Expand description

§Stoat API Wrapper

A high-level Stoat API wrapper.

§Getting Started

This crate requires some boilerplate to ensure errors are handled from the crate and your bot seemlessly along with defining your event callbacks.

§Defining Your Error Type

Almost every event, command and callback uses your error type to allow you to propagate custom errors and stoat-rs errors throughout your code.

It is reconmended to use thiserror to define your error however a manual implemenation is also possible if you prefer manually implementing From.

Your error must implement From<stoat::Error>, Debug, Clone, Send, Sync and be 'static.

#[derive(Debug, Clone, thiserror::Error)]
pub enum Error {
    #[error("Stoat Error: {0}")]
    StoatError(#[from] stoat::Error),
}

§Setting Up Events

All events are implemented as a function in the EventHandler trait.

Every event’s first parameter is Context which contains the current bot state, this is given to you in-place of your Client.

use stoat::{async_trait, EventHandler, Context};

#[derive(Debug, Clone)]
struct Events;

#[async_trait]
impl EventHandler for Events {
    type Error = Error;  // Your error type defined above

    async fn ready(&self, context: Context) -> Result<(), Self::Error> {
        println!("Ready!");

        Ok(())
    }
}

§Running The Bot

Once you define your events you can create your Client which will be the root entry for running your bot.

For customizing your client’s config see the Client documentation.

#[tokio::main]
async fn main() -> Result<(), Error> {
    Client::new(Events).await?.run("BOT TOKEN").await
}

§Commands

See the commands module documentation for setting up commands.

§Logging

This crate makes use of the log crate to relay information and errors to you.

Ensure you have a log implementation setup to see the logs.

Re-exports§

pub use cache::CacheConfig;
pub use cache::GlobalCache;
pub use client::Client;
pub use context::Context;
pub use error::Error;
pub use error::Result;
pub use events::EventHandler;
pub use file::LocalFile;
pub use http::HttpClient;
pub use ulid::Ulid;
pub use ext::*;
pub use utils::*;

Modules§

builders
cache
client
commands
Commands
context
error
events
ext
file
http
notifiers
permissions
types
ulid
utils
websocket

Attribute Macros§

async_trait