2次发布
0.0.2 | 2023年5月16日 |
---|---|
0.0.1 | 2023年5月8日 |
#35 in #cloudflare-workers
24次每月下载
145KB
2.5K SLoC
Cloudflare适配器
该适配器用于Cloudflare Workers。它是一个简单的适配器,解析请求正文并将其传递给框架。
用法
此示例基于 npm init cloudflare project_name worker-rust
模板。
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),
))
}
待办事项
- 减小包大小(简单构建结果为约800 KB的worker大小)
- 添加Discord REST集成(可配置以最小化包大小)
依赖项
~18MB
~356K SLoC