update to new mainline serenity version, other misc refactoring

This commit is contained in:
xenofem 2021-03-29 04:04:10 -04:00
parent 2bbe730b29
commit 416a776339
3 changed files with 441 additions and 640 deletions

973
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
[package] [package]
name = "generator-bot" name = "generator-bot"
version = "0.2.0" version = "0.2.1"
authors = ["xenofem <xenofem@xeno.science>"] authors = ["xenofem <xenofem@xeno.science>"]
license = "MIT" license = "MIT"
edition = "2018" edition = "2018"
@ -15,8 +15,7 @@ toml = "0.5.6"
# tracery = "0.1.0" # tracery = "0.1.0"
[dependencies.serenity] [dependencies.serenity]
git = "https://github.com/acdenisSK/serenity/" version = "0.10.4"
branch = "await_next"
default-features = false default-features = false
features = [ features = [
"builder", "builder",
@ -30,5 +29,5 @@ features = [
] ]
[dependencies.tokio] [dependencies.tokio]
version = "0.2" version = "1.4.0"
features = ["macros"] features = ["macros", "rt-multi-thread", "time"]

View file

@ -10,16 +10,24 @@ use purrchance::Purrchance;
use serde::Deserialize; use serde::Deserialize;
use serenity::{ use serenity::{
async_trait, async_trait,
model::{channel::Message, gateway::Ready, id::ChannelId, user::User}, model::{
channel::Message,
gateway::Ready,
id::{ChannelId, GuildId},
user::User,
},
prelude::*, prelude::*,
}; };
use std::fs::read_to_string; use std::fs::read_to_string;
use std::sync::Arc; use std::sync::Once;
use std::time::Duration; use std::time::Duration;
enum TextGenerator { enum TextGenerator {
Perchance { grammar: purrchance::Grammar, symbol: purrchance::Symbol }, Perchance {
// Tracery(tracery::Grammar), grammar: purrchance::Grammar,
symbol: purrchance::Symbol,
},
// Tracery(tracery::Grammar),
Markov(markov::Chain<String>), Markov(markov::Chain<String>),
} }
@ -27,7 +35,7 @@ impl TextGenerator {
fn generate(&self) -> String { fn generate(&self) -> String {
match self { match self {
TextGenerator::Perchance { grammar, symbol } => symbol.eval(&grammar).unwrap(), TextGenerator::Perchance { grammar, symbol } => symbol.eval(&grammar).unwrap(),
// TextGenerator::Tracery(grammar) => grammar.flatten(), // TextGenerator::Tracery(grammar) => grammar.flatten(),
TextGenerator::Markov(chain) => chain.generate_str(), TextGenerator::Markov(chain) => chain.generate_str(),
} }
} }
@ -37,7 +45,7 @@ impl TextGenerator {
#[serde(tag = "type", rename_all = "lowercase")] #[serde(tag = "type", rename_all = "lowercase")]
enum GeneratorSpec { enum GeneratorSpec {
Perchance { path: String, symbol: String }, Perchance { path: String, symbol: String },
// Tracery { path: String, rule: String }, // Tracery { path: String, rule: String },
Markov { path: String, order: usize }, Markov { path: String, order: usize },
} }
@ -45,17 +53,18 @@ impl GeneratorSpec {
fn load(&self) -> TextGenerator { fn load(&self) -> TextGenerator {
match self { match self {
GeneratorSpec::Perchance { path: p, symbol: s } => { GeneratorSpec::Perchance { path: p, symbol: s } => {
let grammar = purrchance::parser::load_grammar(&read_to_string(p).unwrap()).unwrap(); let grammar =
purrchance::parser::load_grammar(&read_to_string(p).unwrap()).unwrap();
let symbol = purrchance::Symbol::NonTerminal(String::from(s)); let symbol = purrchance::Symbol::NonTerminal(String::from(s));
TextGenerator::Perchance { grammar, symbol } TextGenerator::Perchance { grammar, symbol }
} }
/* /*
GeneratorSpec::Tracery { path, rule } => { GeneratorSpec::Tracery { path, rule } => {
let mut grammar = tracery::from_json(read_to_string(path).unwrap()).unwrap(); let mut grammar = tracery::from_json(read_to_string(path).unwrap()).unwrap();
grammar.default_rule(rule); grammar.default_rule(rule);
TextGenerator::Tracery(grammar) TextGenerator::Tracery(grammar)
} }
*/ */
GeneratorSpec::Markov { path, order } => { GeneratorSpec::Markov { path, order } => {
let mut chain = markov::Chain::of_order(*order); let mut chain = markov::Chain::of_order(*order);
for line in read_to_string(path).unwrap().lines() { for line in read_to_string(path).unwrap().lines() {
@ -75,7 +84,6 @@ struct Config {
interval: u64, interval: u64,
} }
impl TypeMapKey for TextGenerator { impl TypeMapKey for TextGenerator {
type Value = TextGenerator; type Value = TextGenerator;
} }
@ -92,18 +100,14 @@ impl TypeMapKey for ChanId {
type Value = ChannelId; type Value = ChannelId;
} }
struct LoopStatus; struct Handler {
start_loop: Once,
impl TypeMapKey for LoopStatus {
type Value = bool;
} }
struct Handler;
async fn mentions_me(ctx: &Context, msg: &Message) -> bool { async fn mentions_me(ctx: &Context, msg: &Message) -> bool {
let me = User::from(ctx.http.get_current_user().await.unwrap()); let me = User::from(ctx.http.get_current_user().await.unwrap());
if msg.mentions_user(&me) { if msg.mentions_user(&me) {
return true return true;
} }
let guild = msg.guild(ctx).await.unwrap(); let guild = msg.guild(ctx).await.unwrap();
let member = guild.member(ctx, me).await.unwrap(); let member = guild.member(ctx, me).await.unwrap();
@ -111,12 +115,21 @@ async fn mentions_me(ctx: &Context, msg: &Message) -> bool {
msg.mention_roles.iter().any(|r| my_roles.contains(r)) msg.mention_roles.iter().any(|r| my_roles.contains(r))
} }
async fn interval_loop(ctx: Arc<Context>) { async fn interval_loop(ctx: Context) {
let channel_id = ctx.data.read().await.get::<ChanId>().unwrap().clone(); let channel_id = ctx.data.read().await.get::<ChanId>().unwrap().clone();
loop { loop {
let sentence = ctx.data.read().await.get::<TextGenerator>().unwrap().generate(); let sentence = ctx
.data
.read()
.await
.get::<TextGenerator>()
.unwrap()
.generate();
channel_id.say(&ctx.http, sentence).await.unwrap(); channel_id.say(&ctx.http, sentence).await.unwrap();
tokio::time::delay_for(Duration::from_secs(*ctx.data.read().await.get::<Interval>().unwrap())).await; tokio::time::sleep(Duration::from_secs(
*ctx.data.read().await.get::<Interval>().unwrap(),
))
.await;
} }
} }
@ -124,27 +137,25 @@ async fn interval_loop(ctx: Arc<Context>) {
impl EventHandler for Handler { impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) { async fn message(&self, ctx: Context, msg: Message) {
if mentions_me(&ctx, &msg).await { if mentions_me(&ctx, &msg).await {
let sentence = ctx.data.read().await.get::<TextGenerator>().unwrap().generate(); let sentence = ctx
.data
.read()
.await
.get::<TextGenerator>()
.unwrap()
.generate();
msg.channel_id.say(&ctx.http, sentence).await.unwrap(); msg.channel_id.say(&ctx.http, sentence).await.unwrap();
} }
} }
async fn ready(&self, ctx: Context, ready: Ready) { async fn ready(&self, _ctx: Context, ready: Ready) {
eprintln!("{} is connected!", ready.user.name); eprintln!("{} is connected!", ready.user.name);
let ctx = Arc::new(ctx);
let ctx_clone = Arc::clone(&ctx);
if !ctx.data.read().await.get::<LoopStatus>().unwrap() {
let interval_loop = tokio::spawn(async move {interval_loop(ctx_clone).await});
{
let mut data = ctx.data.write().await;
data.insert::<LoopStatus>(true);
}
let _ = interval_loop.await;
{
let mut data = ctx.data.write().await;
data.insert::<LoopStatus>(false);
}
} }
async fn cache_ready(&self, ctx: Context, _guilds: Vec<GuildId>) {
self.start_loop.call_once(move || {
tokio::spawn(async move { interval_loop(ctx).await });
});
} }
} }
@ -154,17 +165,15 @@ async fn main() {
let generator = config.generator.load(); let generator = config.generator.load();
let mut client = Client::new(&config.token) let mut client = Client::builder(&config.token)
.event_handler(Handler) .event_handler(Handler {
start_loop: Once::new(),
})
.type_map_insert::<TextGenerator>(generator)
.type_map_insert::<Interval>(config.interval)
.type_map_insert::<ChanId>(config.channel_id)
.await .await
.expect("err creating client"); .expect("err creating client");
{
let mut data = client.data.write().await;
data.insert::<TextGenerator>(generator);
data.insert::<Interval>(config.interval);
data.insert::<ChanId>(config.channel_id);
data.insert::<LoopStatus>(false);
}
client.start().await.unwrap(); client.start().await.unwrap();
} }