Skip to content

Commit

Permalink
Implement blackball election algorithm for discord
Browse files Browse the repository at this point in the history
  • Loading branch information
Zemnmez committed Aug 14, 2024
1 parent 08db9ab commit f487658
Showing 1 changed file with 64 additions and 4 deletions.
68 changes: 64 additions & 4 deletions rs/cmd/discord_blackball_bot/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,78 @@
use std::env;

use serenity::all::{GuildId, UserId};
use serenity::async_trait;
use serenity::model::channel::Message;
use serenity::prelude::*;

struct Handler;

#[derive(Debug)]
enum BotError {
MissingInviteTarget,
MissingGuildId,
}


impl Handler {
async fn attempt_election(&self, ctx: Context, who: UserId) -> Result<()> {

}

async fn handle_message(&self, ctx: Context, msg: Message) -> Result<()> {
if !msg.content.starts_with("!invite") {
return;
}

match msg.content.split(" ").nth(1) {
None => Err(BotError::MissingInviteTarget),
Some(v) => serenity
}
}

async fn n_members(&self, ctx: Context, guild: GuildId) -> Result<u64> {
guild.members(
&ctx.http,
None,
None,
).await?.into_iter().count()
}

async fn n_sponsors(&self, ctx: Context, n_members: u64) -> Result<u64> {
match n_members {
n if n < 5 => 1,
_ => 2
}
}

/// Calculate the number of votes needed to elect a new member.
async fn quorum(&self, ctx: Context, n_members: u64) -> Result<u64> {
// at least 12 members are needed if there are more than
// 12 members in the club, otherwise all members must vote.
match n_members {
n if n < 12 => n,
_ => 12
}
}

/// how many black balls at minimum are required to reject an election.
async fn n_balls_exclude(&self, ctx: Context, ballot_size: u64) -> Result<u64> {
if ballot_size > 18 {
2
}

1
}
}

#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == "!ping" {
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await {
println!("Error sending message: {why:?}");
}
match self.handle_message(ctx, msg) {
Ok => (),
Err(e) => msg.channel_id.say(&ctx.http, e).await {
println!("Error sending message: {why?:}")
},
}
}
}
Expand Down

0 comments on commit f487658

Please sign in to comment.