update to new mainline serenity version, other misc refactoring

main
xenofem 2 years ago
parent 2bbe730b29
commit 416a776339
  1. 959
      Cargo.lock
  2. 9
      Cargo.toml
  3. 99
      src/main.rs

959
Cargo.lock generated

File diff suppressed because it is too large Load Diff

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

@ -10,16 +10,24 @@ use purrchance::Purrchance;
use serde::Deserialize;
use serenity::{
async_trait,
model::{channel::Message, gateway::Ready, id::ChannelId, user::User},
model::{
channel::Message,
gateway::Ready,
id::{ChannelId, GuildId},
user::User,
},
prelude::*,
};
use std::fs::read_to_string;
use std::sync::Arc;
use std::sync::Once;
use std::time::Duration;
enum TextGenerator {
Perchance { grammar: purrchance::Grammar, symbol: purrchance::Symbol },
// Tracery(tracery::Grammar),
Perchance {
grammar: purrchance::Grammar,
symbol: purrchance::Symbol,
},
// Tracery(tracery::Grammar),
Markov(markov::Chain<String>),
}
@ -27,7 +35,7 @@ impl TextGenerator {
fn generate(&self) -> String {
match self {
TextGenerator::Perchance { grammar, symbol } => symbol.eval(&grammar).unwrap(),
// TextGenerator::Tracery(grammar) => grammar.flatten(),
// TextGenerator::Tracery(grammar) => grammar.flatten(),
TextGenerator::Markov(chain) => chain.generate_str(),
}
}
@ -37,7 +45,7 @@ impl TextGenerator {
#[serde(tag = "type", rename_all = "lowercase")]
enum GeneratorSpec {
Perchance { path: String, symbol: String },
// Tracery { path: String, rule: String },
// Tracery { path: String, rule: String },
Markov { path: String, order: usize },
}
@ -45,17 +53,18 @@ impl GeneratorSpec {
fn load(&self) -> TextGenerator {
match self {
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));
TextGenerator::Perchance { grammar, symbol }
}
/*
/*
GeneratorSpec::Tracery { path, rule } => {
let mut grammar = tracery::from_json(read_to_string(path).unwrap()).unwrap();
grammar.default_rule(rule);
TextGenerator::Tracery(grammar)
}
*/
*/
GeneratorSpec::Markov { path, order } => {
let mut chain = markov::Chain::of_order(*order);
for line in read_to_string(path).unwrap().lines() {
@ -75,7 +84,6 @@ struct Config {
interval: u64,
}
impl TypeMapKey for TextGenerator {
type Value = TextGenerator;
}
@ -92,18 +100,14 @@ impl TypeMapKey for ChanId {
type Value = ChannelId;
}
struct LoopStatus;
impl TypeMapKey for LoopStatus {
type Value = bool;
struct Handler {
start_loop: Once,
}
struct Handler;
async fn mentions_me(ctx: &Context, msg: &Message) -> bool {
let me = User::from(ctx.http.get_current_user().await.unwrap());
if msg.mentions_user(&me) {
return true
return true;
}
let guild = msg.guild(ctx).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))
}
async fn interval_loop(ctx: Arc<Context>) {
async fn interval_loop(ctx: Context) {
let channel_id = ctx.data.read().await.get::<ChanId>().unwrap().clone();
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();
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 {
async fn message(&self, ctx: Context, msg: Message) {
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();
}
}
async fn ready(&self, ctx: Context, ready: Ready) {
async fn ready(&self, _ctx: Context, ready: Ready) {
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 mut client = Client::new(&config.token)
.event_handler(Handler)
let mut client = Client::builder(&config.token)
.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
.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();
}

Loading…
Cancel
Save