Invite bot to server / group for #8.

This commit is contained in:
Paul
2021-08-12 09:24:06 +01:00
parent 93610ebbc3
commit 9f749cfdf8
7 changed files with 127 additions and 51 deletions

65
src/routes/bots/invite.rs Normal file
View File

@@ -0,0 +1,65 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use rocket::serde::json::Json;
use serde::Deserialize;
#[derive(Deserialize)]
pub struct ServerId {
server: String
}
#[derive(Deserialize)]
pub struct GroupId {
group: String
}
#[derive(Deserialize)]
#[serde(untagged)]
pub enum Destination {
Server(ServerId),
Group(GroupId)
}
#[post("/<target>/invite", data = "<dest>")]
pub async fn invite_bot(user: User, target: Ref, dest: Json<Destination>) -> Result<()> {
let bot = target.fetch_bot().await?;
if !bot.public {
if bot.owner != user.id {
return Err(Error::BotIsPrivate);
}
}
match dest.into_inner() {
Destination::Server(ServerId { server }) => {
let server = Ref::from(server)?.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_server(&server)
.for_server()
.await?;
if !perm.get_manage_server() {
Err(Error::MissingPermission)?
}
server.join_member(&bot.id).await?;
Ok(())
}
Destination::Group(GroupId { group }) => {
let channel = Ref::from(group)?.fetch_channel().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_channel(&channel)
.for_channel()
.await?;
if !perm.get_invite_others() {
Err(Error::MissingPermission)?
}
channel.add_to_group(bot.id, user.id).await
}
}
}

View File

@@ -1,9 +1,11 @@
use rocket::Route;
mod create;
mod invite;
pub fn routes() -> Vec<Route> {
routes![
create::create_bot
create::create_bot,
invite::invite_bot
]
}