+ Games now have a config for which roles they have + Roles are assigned at game start + Roles don't function within the bot, except the spy + Added a spy listener + Clippy + fmt
94 lines
2.6 KiB
Rust
94 lines
2.6 KiB
Rust
use serenity::client::Context;
|
|
use serenity::framework::standard::Args;
|
|
use serenity::model::channel::{PermissionOverwrite, PermissionOverwriteType};
|
|
use serenity::model::guild::{Guild, Member};
|
|
use serenity::model::id::ChannelId;
|
|
use serenity::model::Permissions;
|
|
|
|
use crate::error;
|
|
use crate::error::WoxlfError;
|
|
use crate::game::game_state::PhaseDuration;
|
|
use crate::game::global_data::GlobalData;
|
|
use crate::game::player_data::PlayerData;
|
|
use crate::game::role::Role;
|
|
use crate::imgur::Image;
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub async fn add_user_to_game(
|
|
ctx: &Context,
|
|
guild: &Guild,
|
|
global_data: &mut GlobalData,
|
|
discord_user: &Member,
|
|
first_name: Option<String>,
|
|
last_name: Option<String>,
|
|
profile_pic: Option<Image>,
|
|
role: Option<Role>,
|
|
) -> error::Result<PlayerData> {
|
|
if first_name.is_none() && last_name.is_none() {
|
|
return Err(WoxlfError::RanOutOfCodenames);
|
|
}
|
|
|
|
if profile_pic.is_none() {
|
|
return Err(WoxlfError::RanOutOfProfilePics);
|
|
}
|
|
|
|
if role.is_none() {
|
|
return Err(WoxlfError::RanOutOfRoles);
|
|
}
|
|
|
|
let codename = global_data
|
|
.templates()?
|
|
.build_name(global_data, first_name, last_name)
|
|
.unwrap();
|
|
|
|
let channel = guild
|
|
.create_channel(&ctx.http, |c| {
|
|
c.category(&ChannelId::from(global_data.cfg.discord_config.category))
|
|
.name(format!("{}'s Channel", discord_user.display_name()))
|
|
})
|
|
.await?;
|
|
|
|
let allow =
|
|
Permissions::SEND_MESSAGES | Permissions::READ_MESSAGE_HISTORY | Permissions::VIEW_CHANNEL;
|
|
|
|
let overwrite = PermissionOverwrite {
|
|
allow,
|
|
deny: Default::default(),
|
|
kind: PermissionOverwriteType::Member(discord_user.user.id),
|
|
};
|
|
|
|
channel.create_permission(&ctx.http, &overwrite).await?;
|
|
|
|
let webhook = channel
|
|
.create_webhook(
|
|
&ctx.http,
|
|
format!("{}'s Woxlf Webhook", discord_user.display_name()),
|
|
)
|
|
.await?;
|
|
|
|
let player_data = PlayerData {
|
|
channel: channel.id.0,
|
|
discord_id: discord_user.user.id.0,
|
|
vote_target: None,
|
|
codename,
|
|
channel_webhook_id: webhook.id.0,
|
|
profile_pic_url: profile_pic.unwrap().link,
|
|
alive: true,
|
|
role: role.unwrap(),
|
|
};
|
|
|
|
global_data
|
|
.game_state_mut()?
|
|
.player_data
|
|
.push(player_data.clone());
|
|
|
|
Ok(player_data)
|
|
}
|
|
|
|
pub async fn parse_duration_arg(args: &mut Args) -> error::Result<PhaseDuration> {
|
|
match args.single::<PhaseDuration>() {
|
|
Ok(d) => Ok(d),
|
|
Err(_) => Err(WoxlfError::DurationParseError),
|
|
}
|
|
}
|