Crates.io | composure_adapter_cloudflare |
lib.rs | composure_adapter_cloudflare |
version | 0.0.2 |
source | src |
created_at | 2023-05-08 20:36:08.391907 |
updated_at | 2023-05-16 15:50:23.819596 |
description | Discord bot framework for running on the edge |
homepage | |
repository | https://github.com/BlueFrog130/composure-rs/adapters/cloudflare |
max_upload_size | |
id | 860066 |
size | 9,777 |
This adapter is for use with Cloudflare Workers. It is a simple adapter that parses the request body and passes it to the framework.
This sample is based off of npm init cloudflare project_name worker-rust
template.
use composure_cloudflare::CloudflareInteractionBot;
use worker::*;
mod utils;
#[event(fetch)]
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
// Optionally, get more helpful error messages written to the console in the case of a panic.
utils::set_panic_hook();
let command_handler = |interaction: ApplicationCommandInteraction| async move {
match interaction.data.name.as_str() {
"test" => test_handler(interaction),
"subcommand" => {
match interaction.data.first_option().ok_or_else(|| {
Error::RustError("expected subcommand or subcommand group".into())
})? {
ApplicationCommandInteractionDataOption::Subcommand(command) => {
match command.name.as_str() {
"test" => test_handler(interaction),
_ => unknown_handler(interaction),
}
}
ApplicationCommandInteractionDataOption::SubcommandGroup(command) => {
match command.name.as_str() {
"group" => match command.subcommand.name.as_str() {
"test" => test_handler(&command, &interaction),
_ => unknown_handler(interaction),
},
_ => unknown_handler(interaction),
}
}
_ => Err(Error::RustError("expected subcommand group".into())),
}
}
_ => unknown_handler(interaction),
}
};
// The adapter will handle the request and return a response.
CloudflareInteractionBot::new(req, env)
.with_command_handler(|interaction| Box::pin(command_handler(interaction)))
.process()
.await
}
fn test_handler(command: ApplicationCommandInteraction) -> Result<InteractionResponse> {
let username = match command.common.member {
Some(member) => match member.nick {
Some(nick) => nick,
None => member.user.username,
},
None => "unknown user".into(),
};
let response = InteractionResponse::ChannelMessageWithSource(MessageCallbackData {
content: Some(format!("Hello, {}!", username)),
allowed_mentions: None,
embeds: None,
flags: None,
tts: None,
attachments: None,
components: None,
});
Ok(response)
}
fn unknown_handler(_command: ApplicationCommandInteraction) -> Result<InteractionResponse> {
Ok(InteractionResponse::respond_with_embed(
Embed::new()
.with_description("Unknown command!".into())
.with_color(0xf04747),
))
}