Skip to content

Commit

Permalink
team毎のtopicを設定するようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryoga Saito committed Dec 2, 2023
1 parent 23d45ff commit 8e08881
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/bot/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serenity::model::prelude::*;
use crate::bot::helpers::channels::GuildChannelDefinition;
use crate::bot::helpers::channels::GuildChannelDefinitionBuilder;
use crate::bot::Bot;
use crate::models::Team;

static STAFF_CATEGORY_NAME: &str = "ICTSC2023 Staff";

Expand All @@ -21,6 +22,25 @@ static RANDOM_CHANNEL_NAME: &str = "random";
static TEXT_CHANNEL_NAME_SUFFIX: &str = "text";
static VOICE_CHANNEL_NAME_SUFFIX: &str = "voice";

impl Bot {
fn generate_team_channel_topic(team: &Team) -> String {
format!(
"**__踏み台サーバ__**
ホスト名: {team_id}.bastion.ictsc.net
ユーザ名: user
パスワード: {invitation_code}
**__スコアサーバ__**
ユーザ登録URL: https://contest.ictsc.net/signup?invitation_code={invitation_code}&user_group_id={user_group_id}",
team_id = team.id,
invitation_code = team.invitation_code,
user_group_id = team.user_group_id,
)
}
}

impl Bot {
#[tracing::instrument(skip_all)]
pub async fn sync_channels(&self) -> Result<()> {
Expand Down Expand Up @@ -125,10 +145,12 @@ impl Bot {
.get_permission_overwrites_for_team_channel(team)
.await?;

let topic = Self::generate_team_channel_topic(team);
channels.push(
GuildChannelDefinitionBuilder::default()
.name(format!("{}-{}", team.id, TEXT_CHANNEL_NAME_SUFFIX))
.kind(ChannelType::Text)
.topic(Some(topic))
.category(Some(team_category_id))
.permissions(permissions_for_team_channel.clone())
.build()?,
Expand Down Expand Up @@ -237,6 +259,7 @@ impl Bot {
channel.kind == definition.kind
&& channel.name == definition.name
&& channel.parent_id == definition.category
&& channel.topic == definition.topic
// Discordはpermission_overwritesを順不同で返すため、順序を無視して比較する
&& channel.permission_overwrites.iter().all(|overwrite| definition.permissions.contains(overwrite))
&& definition.permissions.iter().all(|permission| channel.permission_overwrites.contains(permission))
Expand Down
14 changes: 13 additions & 1 deletion src/bot/helpers/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub struct GuildChannelDefinition {
pub name: String,
pub kind: ChannelType,
#[builder(default)]
pub topic: Option<String>,
#[builder(default)]
pub category: Option<ChannelId>,
#[builder(default)]
pub permissions: Vec<PermissionOverwrite>,
Expand All @@ -31,6 +33,11 @@ impl Bot {
.kind(definition.kind)
.permissions(definition.permissions);

match definition.topic {
Some(topic) => channel.topic(topic),
None => channel,
};

match definition.category {
Some(category_id) => channel.category(category_id),
None => channel,
Expand Down Expand Up @@ -77,7 +84,12 @@ impl Bot {
.edit(&self.discord_client, |edit| {
edit.name(&definition.name)
.category(definition.category)
.permissions(definition.permissions.clone())
.permissions(definition.permissions.clone());

match &definition.topic {
Some(topic) => edit.topic(topic),
None => edit,
}
})
.await?)
}
Expand Down
1 change: 1 addition & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub struct Team {
pub id: String,
pub role_name: String,
pub invitation_code: String,
pub user_group_id: String,
}

#[derive(Debug, Clone, Deserialize)]
Expand Down

0 comments on commit 8e08881

Please sign in to comment.