Skip to content

Commit

Permalink
fix #40 Twitchのチャットを取得しチャンネルへ入力可能にします (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
usagi authored Nov 12, 2023
1 parent ccf314a commit b630851
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ duct = "0.13.6"
runas = "1.1.0"
csv-async = { version = "1.2.6", features = ["tokio"] }
futures = "0.3.28"
twitch-irc = "5.0.1"

# Windows Only
[target.'cfg(windows)'.dependencies]
Expand Down
1 change: 0 additions & 1 deletion dictionary.arknights.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
げんがんこう 源岩鉱 名詞
けんじょうほうしゅ 堅城砲手 名詞
げんせき 源石 名詞
こう 功 名詞 将進酒
こうご 句呉 地名
こうごじょう 句呉城 名詞 将進酒
こうせきびょう 鉱石病 名詞
Expand Down
1 change: 1 addition & 0 deletions dictionary.chat.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
うさぎはかせちゃん UsagiNetwork
8 changes: 8 additions & 0 deletions src/conf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ pub enum RunWith {
},
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Twitch {
pub username: String,
pub channel_to: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Conf {
pub workers: Option<usize>,
Expand All @@ -37,6 +43,8 @@ pub struct Conf {
pub state_data_capacity: Option<usize>,
pub state_data_pretty: Option<bool>,

pub twitch: Option<Twitch>,

#[serde(default)]
pub run_with: Vec<RunWith>,

Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub enum Error {

#[error("OS-TTS エラーが発生しました: {0}")]
OsTtsError(#[from] tts::Error),

#[error("スレッド終了エラーが発生しました: {0}")]
JoinError(#[from] tokio::task::JoinError),
}

impl ResponseError for Error {
Expand All @@ -50,6 +53,7 @@ impl ResponseError for Error {
Self::SystemTimeError(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::RegexError(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::OsTtsError(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::JoinError(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,46 @@ pub async fn run() -> Result<()> {
// 共有ステートを作成
let state = State::new(&conf, audio_sink).await?;

// TODO: コード整理
use twitch_irc::login::StaticLoginCredentials;
use twitch_irc::TwitchIRCClient;
use twitch_irc::{ClientConfig, SecureTCPTransport};

let config = ClientConfig::default();
let (mut incoming_messages, client) = TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new(config);

let state_for_message_handler = state.clone();
let twitch = conf.twitch.clone();
let join_handle = tokio::spawn(async move {
if twitch.is_none() {
return;
}
let ch = twitch.unwrap().channel_to;
while let Some(message) = incoming_messages.recv().await {
match message {
twitch_irc::message::ServerMessage::Privmsg(msg) => {
log::debug!("Received message: {:?}", msg);
let m = format!("{}:{}", msg.sender.name, msg.message_text);
state_for_message_handler
.read()
.await
.push_channel_datum(ChannelDatum::new(ch.clone(), m).with_flag(ChannelDatum::FLAG_IS_FINAL))
.await;
},
_ => (),
}
}
});

if let Some(twitch) = conf.twitch.clone() {
client.join(twitch.username).unwrap();
}

// 通常の動作モードで実行
run_services(conf, state).await?;

join_handle.await?;

Ok(())
}

Expand Down

0 comments on commit b630851

Please sign in to comment.