Fixed stories

+ Removed story mutex, using the channel mutex instead
+ clippy + fmt
This commit is contained in:
Joey Hines 2022-12-31 23:28:53 -06:00
parent 7c9cb2a81f
commit 35f157b42a
3 changed files with 44 additions and 17 deletions

View File

@ -78,7 +78,6 @@ pub struct BotState {
pub templates: HashMap<String, Tera>, pub templates: HashMap<String, Tera>,
pub albums: HashMap<String, Vec<Image>>, pub albums: HashMap<String, Vec<Image>>,
pub bad_apple_running: bool, pub bad_apple_running: bool,
pub story_in_progress: Mutex<()>,
} }
impl BotState { impl BotState {
@ -109,7 +108,6 @@ impl BotState {
templates, templates,
albums, albums,
bad_apple_running: false, bad_apple_running: false,
story_in_progress: Mutex::new(()),
}) })
} }

View File

@ -74,24 +74,25 @@ async fn list_stories(ctx: &Context, msg: &Message, _args: Args) -> CommandResul
#[only_in(guilds)] #[only_in(guilds)]
#[description("Let me tell you a tail")] #[description("Let me tell you a tail")]
async fn story(ctx: &Context, msg: &Message, args: Args) -> CommandResult { async fn story(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let story_channel = {
let data = ctx.data.read().await; let data = ctx.data.read().await;
data.get::<Channel<String>>().unwrap().clone()
};
let global_data = data.get::<GlobalData>().unwrap(); let mut story_recv = match story_channel.recv.try_lock() {
Ok(story_recv) => story_recv,
let _story_lock = match global_data.bot_state.story_in_progress.try_lock() {
Ok(lock) => lock,
Err(_) => { Err(_) => {
msg.reply(&ctx.http, "Let me finish telling this story bub.") msg.reply(&ctx.http, "Let me finish this story bub").await?;
.await?;
return Ok(()); return Ok(());
} }
}; };
let story_channel = data.get::<Channel<String>>().unwrap(); let stories = {
let data = ctx.data.read().await;
let mut story_recv = story_channel.recv.lock().await; let global_data = data.get::<GlobalData>().unwrap();
get_all_stories(&global_data.cfg).await
let stories = get_all_stories(&global_data.cfg).await; };
let story_path = if args.is_empty() { let story_path = if args.is_empty() {
let mut rng = thread_rng(); let mut rng = thread_rng();
@ -162,15 +163,13 @@ async fn story(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
#[description("Give me a word")] #[description("Give me a word")]
async fn word(ctx: &Context, msg: &Message, args: Args) -> CommandResult { async fn word(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let data = ctx.data.read().await; let data = ctx.data.read().await;
let global = data.get::<GlobalData>().unwrap(); let story_channel = data.get::<Channel<String>>().unwrap();
if global.bot_state.story_in_progress.try_lock().is_ok() { if story_channel.recv.try_lock().is_ok() {
msg.reply(&ctx.http, "No story in progress!").await?; msg.reply(&ctx.http, "No story in progress!").await?;
return Ok(()); return Ok(());
} }
let story_channel = data.get::<Channel<String>>().unwrap();
let story_send = story_channel.send.lock().await; let story_send = story_channel.send.lock().await;
let resp = MessageBuilder::default().push_safe(args.rest()).build(); let resp = MessageBuilder::default().push_safe(args.rest()).build();

30
toml_output.py Normal file
View File

@ -0,0 +1,30 @@
import tomlkit.items
from tomlkit import parse
def parse_templates(cfg, key):
for insult in cfg[key]:
print(f"template\t{insult['template']}")
for k, v in insult["word_bank"].items():
if type(v) == tomlkit.items.Array:
print(f"{k}\t", end="")
for p in v:
print(f"{p}\t", end="")
print()
else:
print(f"{k}\t{v}")
def main():
with open("config.toml", "r") as f:
o = f.read()
cfg = parse(o)
parse_templates(cfg, "compliments")
print()
parse_templates(cfg, "insults")
if __name__ == "__main__":
main()