-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from approvers/uo
feat: uo
- Loading branch information
Showing
7 changed files
with
149 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use crate::bot::{parse_command, ui, BotService, Context, Message, KAWAEMON_DISCORD_USER_ID}; | ||
use anyhow::{Context as _, Result}; | ||
use async_trait::async_trait; | ||
use tokio::sync::Mutex; | ||
|
||
const NAME: &str = "rusty_ponyo::bot::uo"; | ||
const PREFIX: &str = "g!uo"; | ||
const UO: &str = "ウーォ"; | ||
|
||
ui! { | ||
/// ランダムでウーォと言います | ||
struct Ui { | ||
name: NAME, | ||
prefix: PREFIX, | ||
command: Command, | ||
} | ||
} | ||
|
||
#[derive(Debug, clap::Subcommand)] | ||
enum Command { | ||
Status, | ||
Reroll, | ||
} | ||
|
||
pub(crate) struct UoBot { | ||
prob_percent: Mutex<u8>, | ||
} | ||
|
||
impl UoBot { | ||
pub fn new() -> Self { | ||
Self { | ||
prob_percent: Mutex::new(3), | ||
} | ||
} | ||
|
||
async fn reroll(&self) { | ||
*self.prob_percent.lock().await = 1 + (rand::random::<u8>() % 10); | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl BotService for UoBot { | ||
fn name(&self) -> &'static str { | ||
NAME | ||
} | ||
|
||
async fn on_message(&self, msg: &dyn Message, ctx: &dyn Context) -> Result<()> { | ||
{ | ||
let p = *self.prob_percent.lock().await; | ||
if rand::random::<f64>() < (p as f64 / 100.0) { | ||
msg.reply(UO).await?; | ||
} | ||
} | ||
|
||
if !msg.content().starts_with(PREFIX) { | ||
return Ok(()); | ||
} | ||
|
||
let Some(parsed) = parse_command::<Ui>(msg.content(), ctx).await? else { | ||
return Ok(()); | ||
}; | ||
|
||
use Command::*; | ||
|
||
let msg = match parsed.command { | ||
Status => { | ||
let prob = self.prob_percent.lock().await; | ||
format!("```{UO}確率: {prob}%```") | ||
} | ||
Reroll => { | ||
if msg.author().id() == KAWAEMON_DISCORD_USER_ID { | ||
self.reroll().await; | ||
"振り直しました".to_owned() | ||
} else { | ||
"かわえもんでないとこの処理はできません".to_owned() | ||
} | ||
} | ||
}; | ||
|
||
ctx.send_text_message(&msg) | ||
.await | ||
.context("failed to send message")?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters