Added discord bot functionality through serenity

This commit is contained in:
Ådne E. Moldesæter 2020-08-27 22:21:27 +02:00
parent 56a37a738f
commit f4ce6a3d81
2 changed files with 95 additions and 12 deletions

View File

@ -8,7 +8,7 @@ use thiserror::Error;
mod lib_test;
#[derive(Debug, PartialEq, Clone)]
enum DiceFilter {
pub enum DiceFilter {
DropLowest(usize),
DropHighest(usize),
KeepLowest(usize),
@ -16,7 +16,7 @@ enum DiceFilter {
}
#[derive(Debug, PartialEq, Clone)]
enum Segment {
pub enum Segment {
DiceRoll {
op: char,
count: i32,
@ -166,10 +166,10 @@ fn group_modifiers_to_dicerolls(segments: &[Segment]) -> Vec<RollWithModifier> {
}
#[derive(Debug, PartialEq, Clone)]
struct Roll {
operator: char,
results: Vec<i32>,
total: i32,
pub struct Roll {
pub operator: char,
pub results: Vec<i32>,
pub total: i32,
}
#[derive(Error, Debug, PartialEq, Clone)]
@ -265,12 +265,12 @@ fn roll_dice_segments<R: Rng>(
}
#[derive(Debug, PartialEq, Clone)]
struct RollSet {
total: i32,
rolls: Vec<(Roll, Vec<Segment>)>,
pub struct RollSet {
pub total: i32,
pub rolls: Vec<(Roll, Vec<Segment>)>,
}
fn roll_dice<R: Rng>(s: &str, mut rng: R) -> Result<RollSet> {
pub fn roll_dice<R: Rng>(s: &str, mut rng: R) -> Result<RollSet> {
let segments = parse_dice_segments(s)?;
let groups = group_modifiers_to_dicerolls(&segments);

View File

@ -1,3 +1,86 @@
fn main() {
println!("Hello, world!");
mod lib;
use rand::thread_rng;
use serenity::{
client::{Context, EventHandler},
framework::{
standard::{
macros::{command, group},
CommandResult,
},
StandardFramework,
},
model::{prelude::Ready, channel::Message},
Client,
};
#[group]
#[commands(hi, roll)]
struct General;
#[command]
fn hi(ctx: &mut Context, msg: &Message) -> CommandResult {
msg.reply(ctx, "Hello world!")?;
Ok(())
}
#[command]
fn roll(ctx: &mut Context, msg: &Message) -> CommandResult {
let content = msg.content_safe(&ctx.cache);
let mut rng = thread_rng();
let roll_result = lib::roll_dice(&content, &mut rng)?;
let result : String = roll_result.rolls.iter().map(|i| {
let operator = &i.0.operator;
let rolls = &i.0.results;
let modifiers : String = i.1.iter().map(|seg| {
match seg {
lib::Segment::Modifier {op, amount} => format!("{}{}", op, amount),
_ => unreachable!()
}
})
.collect::<Vec<_>>().join("");
format!("{}{:?} {}", operator, rolls, modifiers)
}).collect::<Vec<_>>().join(" ");
let total = roll_result.total;
let result = format!("{}= {}", result, total);
if let Err(e) = msg.reply(ctx, result) {
eprintln!("Failed to respond to command: {:?}", e)
}
Ok(())
}
struct Handler;
impl EventHandler for Handler {
fn ready(&self, ctx: Context, _: Ready) {
use serenity::model::{gateway::Activity, user::OnlineStatus};
ctx.set_presence(Some(Activity::playing("with dice | /roll")), OnlineStatus::Online);
}
}
fn main() {
let discord_token = std::env::var("DISCORD_TOKEN").expect("failed to read DISCORD_TOKEN environment variable");
let mut client = Client::new(&discord_token, Handler).expect("Error creating client");
client.with_framework(
StandardFramework::new()
.configure(|c| c.prefix("/"))
.group(&GENERAL_GROUP),
);
if let Err(e) = client.start() {
eprintln!("Error occured starting discord client: {:?}", e);
}
}