Skip to content

Commit

Permalink
Update serenity and fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
proelbtn committed Nov 9, 2024
1 parent da8f20a commit 829c7dd
Show file tree
Hide file tree
Showing 15 changed files with 559 additions and 517 deletions.
265 changes: 142 additions & 123 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ serde = "1.0.131"
serde_derive = "1.0.131"
serde_json = "1.0.108"
serde_yaml = "0.8.21"
serenity = { version = "0.11.6", default-features = false, features = ["client", "collector", "gateway", "rustls_backend", "model", "unstable_discord_api"] }
serenity = { version = "0.12.2", default-features = false, features = ["client", "collector", "gateway", "rustls_backend", "model", "unstable_discord_api"] }
thiserror = "1.0.30"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.30"
Expand Down
33 changes: 15 additions & 18 deletions src/bot/commands/archive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use serenity::builder::CreateApplicationCommand;
use serenity::model::prelude::application_command::ApplicationCommandInteraction;
use serenity::all::CreateCommand;
use serenity::all::EditInteractionResponse;
use serenity::model::prelude::*;

use crate::bot::helpers::HelperError;
Expand All @@ -18,33 +18,29 @@ enum ArchiveCommandError {
type ArchiveCommandResult<T> = std::result::Result<T, ArchiveCommandError>;

impl Bot {
pub fn create_archive_command(
command: &mut CreateApplicationCommand,
) -> &mut CreateApplicationCommand {
command
.name("archive")
.description("運営への質問スレッドを終了します")
pub fn create_archive_command() -> CreateCommand {
CreateCommand::new("archive").description("運営への質問スレッドを終了します")
}

pub async fn handle_archive_command(
&self,
interaction: &ApplicationCommandInteraction,
) -> Result<()> {
pub async fn handle_archive_command(&self, interaction: &CommandInteraction) -> Result<()> {
tracing::debug!("send acknowledgement");
self.defer_response(interaction).await?;

if let Err(err) = self.do_archive_command(interaction).await {
tracing::error!(?err, "failed to do archive command");
self.edit_response(interaction, |data| data.content(err.to_string()))
.await?;
self.edit_response(
interaction,
EditInteractionResponse::new().content(err.to_string()),
)
.await?;
}
Ok(())
}

#[tracing::instrument(skip_all)]
async fn do_archive_command(
&self,
interaction: &ApplicationCommandInteraction,
interaction: &CommandInteraction,
) -> ArchiveCommandResult<()> {
let channel_id = interaction.channel_id;
let channel = self.get_channel(channel_id).await?;
Expand All @@ -60,9 +56,10 @@ impl Bot {

self.archive_thread(&mut guild_channel).await?;

self.edit_response(interaction, |response| {
response.content("質問スレッドを終了しました。")
})
self.edit_response(
interaction,
EditInteractionResponse::new().content("質問スレッドを終了しました。"),
)
.await?;

Ok(())
Expand Down
73 changes: 41 additions & 32 deletions src/bot/commands/ask.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::Result;
use serenity::builder::CreateApplicationCommand;
use serenity::model::prelude::application_command::ApplicationCommandInteraction;
use serenity::model::prelude::command::*;
use serenity::all::CreateCommand;
use serenity::all::CreateCommandOption;
use serenity::all::CreateInteractionResponseMessage;
use serenity::all::CreateMessage;
use serenity::all::EditInteractionResponse;
use serenity::model::prelude::*;

use crate::bot::helpers::HelperError;
Expand All @@ -26,32 +28,30 @@ enum AskCommandError {
type AskCommandResult<T> = std::result::Result<T, AskCommandError>;

impl Bot {
pub fn create_ask_command(
command: &mut CreateApplicationCommand,
) -> &mut CreateApplicationCommand {
command
.name("ask")
pub fn create_ask_command() -> CreateCommand {
CreateCommand::new("ask")
.description("運営への質問スレッドを開始します")
.create_option(|option| {
option
.name("title")
.description("質問タイトル(50文字以内)")
.kind(CommandOptionType::String)
.required(true)
})
.add_option(
CreateCommandOption::new(
CommandOptionType::String,
"title",
"質問タイトル(50文字以内)",
)
.required(true),
)
}

#[tracing::instrument(skip_all)]
pub async fn handle_ask_command(
&self,
interaction: &ApplicationCommandInteraction,
) -> Result<()> {
pub async fn handle_ask_command(&self, interaction: &CommandInteraction) -> Result<()> {
let (guild_channel, title) = match self.validate_ask_command(interaction).await {
Ok(v) => v,
Err(err) => {
self.respond(interaction, |data| {
data.ephemeral(true).content(err.to_string())
})
self.respond(
interaction,
CreateInteractionResponseMessage::new()
.ephemeral(true)
.content(err.to_string()),
)
.await?;
return Ok(());
},
Expand All @@ -65,16 +65,19 @@ impl Bot {
.await
{
tracing::error!(?err, "failed to do ask command");
self.edit_response(interaction, |data| data.content(err.to_string()))
.await?;
self.edit_response(
interaction,
EditInteractionResponse::new().content(err.to_string()),
)
.await?;
}

Ok(())
}

async fn validate_ask_command<'t>(
&self,
interaction: &'t ApplicationCommandInteraction,
interaction: &'t CommandInteraction,
) -> AskCommandResult<(GuildChannel, &'t str)> {
let channel = self.get_channel(interaction.channel_id).await?;

Expand Down Expand Up @@ -103,7 +106,7 @@ impl Bot {

async fn do_ask_command(
&self,
interaction: &ApplicationCommandInteraction,
interaction: &CommandInteraction,
guild_channel: &GuildChannel,
title: &str,
) -> AskCommandResult<()> {
Expand All @@ -117,9 +120,11 @@ impl Bot {
.map(|role| Mention::from(role.id).to_string())
.collect();

self.edit_response(interaction, |data| {
data.content(format!("{} 質問内容を入力してください。", sender_mention))
})
self.edit_response(
interaction,
EditInteractionResponse::new()
.content(format!("{} 質問スレッドを開始します。", sender_mention)),
)
.await?;

let message = self.get_response(interaction).await?;
Expand All @@ -130,9 +135,13 @@ impl Bot {

// TODO: 直接メッセージを送信するな!!!
channel
.send_message(&self.discord_client, |data| {
data.content(format!("{}", staff_mentions.join(" ")))
})
.send_message(
&self.discord_client,
CreateMessage::new().content(format!(
"{} 質問スレッドを開始します。",
staff_mentions.join(" ")
)),
)
.await?;

Ok(())
Expand Down
64 changes: 35 additions & 29 deletions src/bot/commands/join.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::collections::HashSet;

use anyhow::Result;
use serenity::builder::CreateApplicationCommand;
use serenity::model::prelude::application_command::ApplicationCommandInteraction;
use serenity::model::prelude::command::*;
use serenity::all::CommandInteraction;
use serenity::all::CommandOptionType;
use serenity::all::CreateCommand;
use serenity::all::CreateCommandOption;
use serenity::all::CreateInteractionResponseMessage;
use serenity::all::EditInteractionResponse;

use crate::bot::helpers::HelperError;
use crate::bot::roles;
Expand All @@ -25,32 +28,30 @@ enum JoinCommandError<'a> {
type JoinCommandResult<'t, T> = std::result::Result<T, JoinCommandError<'t>>;

impl Bot {
pub fn create_join_command(
command: &mut CreateApplicationCommand,
) -> &mut CreateApplicationCommand {
command
.name("join")
pub fn create_join_command() -> CreateCommand {
CreateCommand::new("join")
.description("チームに参加します。")
.create_option(|option| {
option
.name("invitation_code")
.description("招待コード")
.kind(CommandOptionType::String)
.required(true)
})
.add_option(
CreateCommandOption::new(
CommandOptionType::String,
"invitation_code",
"招待コード",
)
.required(true),
)
}

#[tracing::instrument(skip_all)]
pub async fn handle_join_command(
&self,
interaction: &ApplicationCommandInteraction,
) -> Result<()> {
pub async fn handle_join_command(&self, interaction: &CommandInteraction) -> Result<()> {
let role_name = match self.validate_join_command(interaction) {
Ok(role_name) => role_name,
Err(err) => {
self.respond(interaction, |data| {
data.ephemeral(true).content(err.to_string())
})
self.respond(
interaction,
CreateInteractionResponseMessage::new()
.ephemeral(true)
.content(err.to_string()),
)
.await?;
return Ok(());
},
Expand All @@ -61,16 +62,19 @@ impl Bot {

if let Err(err) = self.do_join_command(interaction, role_name).await {
tracing::error!(?err, "failed to do join command");
self.edit_response(interaction, |data| data.content(err.to_string()))
.await?;
self.edit_response(
interaction,
EditInteractionResponse::new().content(err.to_string()),
)
.await?;
}

Ok(())
}

fn validate_join_command<'t>(
&self,
interaction: &'t ApplicationCommandInteraction,
interaction: &'t CommandInteraction,
) -> JoinCommandResult<'t, &str> {
// joinコマンドはGlobalCommandなので、どこからでも呼び出すことは可能である。
// だが、間違ってrandomチャンネル等で呼び出されてしまうことを防ぐため、DM以外からの呼び出しはエラーとする。
Expand Down Expand Up @@ -106,7 +110,7 @@ impl Bot {

async fn do_join_command(
&self,
interaction: &ApplicationCommandInteraction,
interaction: &CommandInteraction,
role_name: &str,
) -> JoinCommandResult<()> {
// DMの送信元が、ICTSC Discordチャンネルに参加しているかをチェックする。
Expand Down Expand Up @@ -141,9 +145,11 @@ impl Bot {
self.revoke_roles(&mut sender_member, role_ids_revoked)
.await?;

self.edit_response(interaction, |data| {
data.content(format!("チーム `{}` に参加しました。", role_name))
})
self.edit_response(
interaction,
EditInteractionResponse::new()
.content(format!("チーム `{}` に参加しました。", role_name)),
)
.await?;

Ok(())
Expand Down
Loading

0 comments on commit 829c7dd

Please sign in to comment.