refactoring

This commit is contained in:
mii
2022-08-12 19:05:02 +09:00
parent b36bee8be8
commit 47b11262e2
7 changed files with 139 additions and 115 deletions

View File

@ -1,8 +1,5 @@
use serenity::{client::{EventHandler, Context}, async_trait, model::{gateway::Ready, interactions::{Interaction, application_command::ApplicationCommandInteraction, InteractionApplicationCommandCallbackDataFlags}, id::{GuildId, UserId}, channel::Message, prelude::{Member, application_command::{ApplicationCommandOptionType, ApplicationCommandOption, ApplicationCommand}}, voice::VoiceState}, framework::standard::macros::group}; use serenity::{client::{EventHandler, Context}, async_trait, model::{gateway::Ready, interactions::{Interaction, application_command::ApplicationCommandInteraction, InteractionApplicationCommandCallbackDataFlags}, id::{GuildId, UserId}, channel::Message, prelude::{Member, application_command::{ApplicationCommandOptionType, ApplicationCommandOption, ApplicationCommand}}, voice::VoiceState}, framework::standard::macros::group};
use crate::{data::TTSData, tts::{instance::TTSInstance, message::AnnounceMessage}, implement::member_name::ReadName}; use crate::{data::TTSData, tts::{instance::TTSInstance, message::AnnounceMessage}, implement::{member_name::ReadName, voice_move_state::{VoiceMoveStateTrait, VoiceMoveState}}, events};
#[group]
struct Test;
pub struct Handler; pub struct Handler;
@ -151,6 +148,15 @@ async fn setup_command(ctx: &Context, command: &ApplicationCommandInteraction) -
#[async_trait] #[async_trait]
impl EventHandler for Handler { impl EventHandler for Handler {
async fn message(&self, ctx: Context, message: Message) {
events::message_receive::message(ctx, message).await
}
async fn ready(&self, ctx: Context, ready: Ready) {
events::ready::ready(ctx, ready).await
}
async fn interaction_create(&self, ctx: Context, interaction: Interaction) { async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
if let Interaction::ApplicationCommand(command) = interaction { if let Interaction::ApplicationCommand(command) = interaction {
let name = &*command.data.name; let name = &*command.data.name;
@ -184,57 +190,13 @@ impl EventHandler for Handler {
let instance = storage.get_mut(&guild_id).unwrap(); let instance = storage.get_mut(&guild_id).unwrap();
let mut message: Option<String> = None; let voice_move_state = new.move_state(&old, instance.voice_channel);
match old { let message: Option<String> = match voice_move_state {
Some(old) => { VoiceMoveState::JOIN => Some(format!("{} さんが通話に参加しました", new.member.unwrap().read_name())),
match (old.channel_id, new.channel_id) { VoiceMoveState::LEAVE => Some(format!("{} さんが通話から退出しました", new.member.unwrap().read_name())),
(Some(old_channel_id), Some(new_channel_id)) => { _ => None,
if old_channel_id == new_channel_id { };
return;
}
if old_channel_id != new_channel_id {
if instance.voice_channel == new_channel_id {
message = Some(format!("{} さんが通話に参加しました", new.member.unwrap().read_name()));
}
} else if old_channel_id == instance.voice_channel && new_channel_id != instance.voice_channel {
message = Some(format!("{} さんが通話から退出しました", new.member.unwrap().read_name()));
} else {
return;
}
}
(Some(old_channel_id), None) => {
if old_channel_id == instance.voice_channel {
message = Some(format!("{} さんが通話から退出しました", new.member.unwrap().read_name()));
} else {
return;
}
}
(None, Some(new_channel_id)) => {
if new_channel_id == instance.voice_channel {
message = Some(format!("{} さんが通話に参加しました", new.member.unwrap().read_name()));
} else {
return;
}
}
_ => {
return;
}
}
}
None => {
match new.channel_id {
Some(channel_id) => {
if instance.voice_channel == channel_id {
message = Some(format!("{} さんが通話に参加しました", new.member.unwrap().read_name()));
}
}
None => {
return;
}
}
}
}
if let Some(message) = message { if let Some(message) = message {
instance.read(AnnounceMessage { instance.read(AnnounceMessage {
@ -243,57 +205,4 @@ impl EventHandler for Handler {
} }
} }
} }
async fn message(&self, ctx: Context, message: Message) {
if message.author.bot {
return;
}
let guild_id = message.guild(&ctx.cache).await;
if let None = guild_id {
return;
}
let guild_id = guild_id.unwrap().id;
let storage_lock = {
let data_read = ctx.data.read().await;
data_read.get::<TTSData>().expect("Cannot get TTSStorage").clone()
};
{
let mut storage = storage_lock.write().await;
if !storage.contains_key(&guild_id) {
return;
}
let instance = storage.get_mut(&guild_id).unwrap();
if instance.text_channel.0 != message.channel_id.0 {
return;
}
instance.read(message, &ctx).await;
}
}
async fn ready(&self, ctx: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
let _ = ApplicationCommand::set_global_application_commands(&ctx.http, |commands| {
commands.create_application_command(|command| {
command.name("stop")
.description("Stop tts")
});
commands.create_application_command(|command| {
command.name("setup")
.description("Setup tts")
});
commands.create_application_command(|command| {
command.name("config")
.description("Config")
})
}).await;
}
} }

View File

@ -0,0 +1,37 @@
use serenity::{prelude::Context, model::prelude::Message};
use crate::data::TTSData;
pub async fn message(ctx: Context, message: Message) {
if message.author.bot {
return;
}
let guild_id = message.guild(&ctx.cache).await;
if let None = guild_id {
return;
}
let guild_id = guild_id.unwrap().id;
let storage_lock = {
let data_read = ctx.data.read().await;
data_read.get::<TTSData>().expect("Cannot get TTSStorage").clone()
};
{
let mut storage = storage_lock.write().await;
if !storage.contains_key(&guild_id) {
return;
}
let instance = storage.get_mut(&guild_id).unwrap();
if instance.text_channel.0 != message.channel_id.0 {
return;
}
instance.read(message, &ctx).await;
}
}

2
src/events/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod ready;
pub mod message_receive;

20
src/events/ready.rs Normal file
View File

@ -0,0 +1,20 @@
use serenity::{prelude::Context, model::prelude::{Ready, application_command::ApplicationCommand}};
pub async fn ready(ctx: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
let _ = ApplicationCommand::set_global_application_commands(&ctx.http, |commands| {
commands.create_application_command(|command| {
command.name("stop")
.description("Stop tts")
});
commands.create_application_command(|command| {
command.name("setup")
.description("Setup tts")
});
commands.create_application_command(|command| {
command.name("config")
.description("Config")
})
}).await;
}

View File

@ -1,2 +1,3 @@
pub mod message; pub mod message;
pub mod member_name; pub mod member_name;
pub mod voice_move_state;

View File

@ -0,0 +1,54 @@
use serenity::model::{voice::VoiceState, prelude::ChannelId};
pub trait VoiceMoveStateTrait {
fn move_state(&self, old: &Option<VoiceState>, target_channel: ChannelId) -> VoiceMoveState;
}
pub enum VoiceMoveState {
JOIN,
LEAVE,
MOVE,
NONE
}
impl VoiceMoveStateTrait for VoiceState {
fn move_state(&self, old: &Option<VoiceState>, target_channel: ChannelId) -> VoiceMoveState {
let new = self;
if let None = old.clone() {
return if target_channel == new.channel_id.unwrap() {
VoiceMoveState::JOIN
} else {
VoiceMoveState::NONE
}
}
let old = (*old).clone().unwrap();
match (old.channel_id, new.channel_id) {
(Some(old_channel_id), Some(new_channel_id)) => {
if old_channel_id == new_channel_id {
VoiceMoveState::NONE
} else if old_channel_id != new_channel_id {
if target_channel == new_channel_id {
VoiceMoveState::JOIN
} else {
VoiceMoveState::NONE
}
} else {
VoiceMoveState::NONE
}
}
(Some(old_channel_id), None) => {
if old_channel_id == target_channel {
VoiceMoveState::LEAVE
} else {
VoiceMoveState::NONE
}
}
_ => {
VoiceMoveState::NONE
}
}
}
}

View File

@ -1,3 +1,11 @@
mod config;
mod event_handler;
mod tts;
mod implement;
mod data;
mod database;
mod events;
use std::{sync::Arc, collections::HashMap}; use std::{sync::Arc, collections::HashMap};
use config::Config; use config::Config;
@ -12,13 +20,6 @@ use serenity::{
use songbird::SerenityInit; use songbird::SerenityInit;
mod config;
mod event_handler;
mod tts;
mod implement;
mod data;
mod database;
/// Create discord client /// Create discord client
/// ///
/// Example: /// Example: