mirror of
https://git.vavakado.xyz/me/ewectionbot.git
synced 2026-11-23 20:37:17 -05:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ec7f987f3 |
||
|
|
ae7b9d77cb |
||
|
|
741ad678b6 |
||
|
|
dbe30de60d |
||
|
|
d5aed89e2b |
||
|
|
7c8f8965e2 |
||
|
|
9f5fc65077 |
||
|
|
25c9a4cd3b |
||
|
|
9eb5113dc5 |
11 changed files with 282 additions and 39 deletions
50
.sqlx/query-6c494c412486e6b8a03dc02986325c48e8c0b3d7005311129fdd68ab2c4dcd02.json
generated
Normal file
50
.sqlx/query-6c494c412486e6b8a03dc02986325c48e8c0b3d7005311129fdd68ab2c4dcd02.json
generated
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
{
|
||||
"db_name": "SQLite",
|
||||
"query": "SELECT id, mod_log_id, reason_ask_id FROM mod_actions\n WHERE mod_id = ? AND reason_response_id = ?\n ORDER BY id DESC",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "id",
|
||||
"ordinal": 0,
|
||||
"type_info": "Integer",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "mod_actions",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "mod_log_id",
|
||||
"ordinal": 1,
|
||||
"type_info": "Integer",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "mod_actions",
|
||||
"name": "mod_log_id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "reason_ask_id",
|
||||
"ordinal": 2,
|
||||
"type_info": "Integer",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "mod_actions",
|
||||
"name": "reason_ask_id"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Right": 2
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
true,
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "6c494c412486e6b8a03dc02986325c48e8c0b3d7005311129fdd68ab2c4dcd02"
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"db_name": "SQLite",
|
||||
"query": "SELECT\n e.*,\n (\n SELECT COUNT(*)\n FROM candidates c\n WHERE c.election_id = e.id\n ) AS candidate_count,\n (\n SELECT COUNT(DISTINCT v.voter_id) \n FROM votes v\n WHERE v.election_id = e.id\n ) AS voter_count\n FROM elections e\n ORDER BY e.started_at DESC\n LIMIT 1",
|
||||
"query": "SELECT\n e.*,\n (\n SELECT COUNT(*)\n FROM candidates c\n WHERE c.election_id = e.id\n ) AS candidate_count,\n (\n SELECT COUNT(DISTINCT v.voter_id)\n FROM votes v\n WHERE v.election_id = e.id\n ) AS voter_count,\n (\n SELECT COUNT(*)\n FROM votes v\n WHERE v.election_id = e.id\n ) AS vote_count\n FROM elections e\n ORDER BY e.started_at DESC\n LIMIT 1",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
|
|
@ -80,6 +80,12 @@
|
|||
"ordinal": 7,
|
||||
"type_info": "Integer",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"name": "vote_count",
|
||||
"ordinal": 8,
|
||||
"type_info": "Integer",
|
||||
"origin": "Expression"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
|
|
@ -93,8 +99,9 @@
|
|||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "f3d520befae1cf53ca6190159f7cbb036992d0e4d73f8fa4c3f756aa1fa65477"
|
||||
"hash": "a80835b719070df4c5a326a4fe2c8db962089f6171ff516e7edd6c9516732779"
|
||||
}
|
||||
12
.sqlx/query-c4042a2e022b60756a97ea1aa1e921af330da85e97df0c7733802db18e3bebf2.json
generated
Normal file
12
.sqlx/query-c4042a2e022b60756a97ea1aa1e921af330da85e97df0c7733802db18e3bebf2.json
generated
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"db_name": "SQLite",
|
||||
"query": "UPDATE mod_actions SET reason = ?, reason_response_id = ? WHERE id = ?",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Right": 3
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "c4042a2e022b60756a97ea1aa1e921af330da85e97df0c7733802db18e3bebf2"
|
||||
}
|
||||
26
.sqlx/query-c74328cadb06d199340dcb0330f98d3dc68318568c8b0d87d03778b387212520.json
generated
Normal file
26
.sqlx/query-c74328cadb06d199340dcb0330f98d3dc68318568c8b0d87d03778b387212520.json
generated
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"db_name": "SQLite",
|
||||
"query": "\n SELECT term_start FROM council_members\n WHERE user_id = ? AND term_end IS NULL\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "term_start",
|
||||
"ordinal": 0,
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "council_members",
|
||||
"name": "term_start"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Right": 1
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "c74328cadb06d199340dcb0330f98d3dc68318568c8b0d87d03778b387212520"
|
||||
}
|
||||
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -672,6 +672,7 @@ dependencies = [
|
|||
"proptest",
|
||||
"rand 0.10.2",
|
||||
"rayon",
|
||||
"regex",
|
||||
"reqwest 0.13.4",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ license-file = "LICENSE"
|
|||
|
||||
[features]
|
||||
default = []
|
||||
resign = []
|
||||
|
||||
[dependencies]
|
||||
bitvec = "1.1.1"
|
||||
|
|
@ -17,6 +16,7 @@ mimalloc = "0.1.52"
|
|||
poise = { version = "0.6.2", default-features = false, features = ["cache", "handle_panics"] }
|
||||
rand = "0.10.2"
|
||||
rayon = "1.12.0"
|
||||
regex = "1.13.1"
|
||||
reqwest = { version = "0.13.4", features = ["json"] }
|
||||
serde = { version = "1.0.229", features = ["derive"] }
|
||||
serde_json = "1.0.151"
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE mod_actions DROP COLUMN reason_response_id;
|
||||
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE mod_actions ADD COLUMN reason_response_id INTEGER;
|
||||
|
|
@ -21,10 +21,15 @@ pub async fn election(ctx: Context<'_>) -> Result<(), Error> {
|
|||
WHERE c.election_id = e.id
|
||||
) AS candidate_count,
|
||||
(
|
||||
SELECT COUNT(DISTINCT v.voter_id)
|
||||
SELECT COUNT(DISTINCT v.voter_id)
|
||||
FROM votes v
|
||||
WHERE v.election_id = e.id
|
||||
) AS voter_count
|
||||
) AS voter_count,
|
||||
(
|
||||
SELECT COUNT(*)
|
||||
FROM votes v
|
||||
WHERE v.election_id = e.id
|
||||
) AS vote_count
|
||||
FROM elections e
|
||||
ORDER BY e.started_at DESC
|
||||
LIMIT 1"
|
||||
|
|
@ -63,11 +68,12 @@ pub async fn election(ctx: Context<'_>) -> Result<(), Error> {
|
|||
.color(0x49b3a3);
|
||||
|
||||
if !election.is_finished && ended_at > now {
|
||||
embed = embed.footer(CreateEmbedFooter::new("Result's aren't calculated yet!"));
|
||||
embed = embed.footer(CreateEmbedFooter::new("Results aren't calculated yet!"));
|
||||
}
|
||||
|
||||
if ended_at < now {
|
||||
embed = embed.field("Voters", election.voter_count.to_string(), true);
|
||||
embed = embed.field("Votes", election.vote_count.to_string(), true);
|
||||
}
|
||||
|
||||
embed = embed.field("Kind", election.kind, false);
|
||||
|
|
|
|||
93
src/main.rs
93
src/main.rs
|
|
@ -4,7 +4,7 @@ use ::serenity::{
|
|||
gateway::ActivityData,
|
||||
model::{
|
||||
channel::MessageType,
|
||||
id::{ChannelId, GuildId, UserId},
|
||||
id::{ChannelId, GuildId, RoleId, UserId},
|
||||
},
|
||||
};
|
||||
use ewectionbot::pav::{self, TieSituation};
|
||||
|
|
@ -51,6 +51,12 @@ pub struct Config {
|
|||
council: CouncilConfig,
|
||||
union: HashMap<String, UnionConfig>,
|
||||
poll: Option<PollConfig>,
|
||||
#[serde(default = "default_max_attach_redownload_size")] // 12 megs
|
||||
max_attach_redownload_size: u32, // in bytes, stored in memory
|
||||
}
|
||||
|
||||
fn default_max_attach_redownload_size() -> u32 {
|
||||
12681408
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone)]
|
||||
|
|
@ -220,6 +226,21 @@ async fn handle_event(
|
|||
sleep(StdDuration::from_secs(15)).await;
|
||||
let _ = temporary?.delete(&ctx.http).await;
|
||||
}
|
||||
} else if let FullEvent::MessageUpdate {
|
||||
old_if_available: _,
|
||||
new: _,
|
||||
event,
|
||||
} = event
|
||||
{
|
||||
let msg = event
|
||||
.channel_id
|
||||
.message(&ctx.http(), event.id.get())
|
||||
.await?;
|
||||
|
||||
if event.guild_id.is_none() {
|
||||
mod_log::edit_dm(ctx, &msg, data).await?;
|
||||
return Ok(());
|
||||
}
|
||||
} else if let FullEvent::GuildAuditLogEntryCreate { guild_id, entry } = event
|
||||
&& guild_id.get() == config.guild_id
|
||||
{
|
||||
|
|
@ -380,7 +401,6 @@ pub async fn activity(
|
|||
}
|
||||
|
||||
/// Resign from your Council position
|
||||
#[cfg(feature = "resign")]
|
||||
#[instrument(skip(ctx), err)]
|
||||
#[poise::command(slash_command, prefix_command)]
|
||||
pub async fn resign(ctx: Context<'_>) -> Result<(), Error> {
|
||||
|
|
@ -433,21 +453,6 @@ pub async fn resign(ctx: Context<'_>) -> Result<(), Error> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Resign from your Council position
|
||||
#[cfg(not(feature = "resign"))]
|
||||
#[instrument(skip(ctx), err)]
|
||||
#[poise::command(slash_command, prefix_command)]
|
||||
pub async fn resign(ctx: Context<'_>) -> Result<(), Error> {
|
||||
ctx.send(
|
||||
poise::CreateReply::default()
|
||||
.content("This feature isn't done yet, only available in debug builds")
|
||||
.ephemeral(true),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the current Council status
|
||||
#[instrument(skip(ctx), err)]
|
||||
#[poise::command(slash_command, prefix_command)]
|
||||
|
|
@ -635,7 +640,19 @@ async fn handle_resign_component(
|
|||
),
|
||||
)
|
||||
.await?;
|
||||
// TODO: trigger a by-election
|
||||
|
||||
ctx.http
|
||||
.remove_member_role(
|
||||
GuildId::from(config.guild_id),
|
||||
user.id,
|
||||
RoleId::new(config.council.role_id),
|
||||
Some("removing resigned council member"),
|
||||
)
|
||||
.await?;
|
||||
|
||||
if let Err(e) = maybe_create_by_election(pool, config, ctx.http.clone()).await {
|
||||
error!("Failed to trigger a by-election: {e}");
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
@ -1307,9 +1324,13 @@ async fn schedule_end_of_election(
|
|||
.await?;
|
||||
|
||||
for person in people_out {
|
||||
if let Ok(member) = guild_id.member(&http, person as u64).await {
|
||||
member.remove_role(&http, config.council.role_id).await?;
|
||||
}
|
||||
http.remove_member_role(
|
||||
guild_id,
|
||||
UserId::new(person as u64),
|
||||
RoleId::new(config.council.role_id),
|
||||
Some("removing former council members"),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
for winner in winners {
|
||||
|
|
@ -1321,9 +1342,13 @@ async fn schedule_end_of_election(
|
|||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
if let Ok(member) = guild_id.member(&http, winner).await {
|
||||
member.add_role(&http, config.council.role_id).await?;
|
||||
}
|
||||
http.add_member_role(
|
||||
guild_id,
|
||||
UserId::new(winner),
|
||||
RoleId::new(config.council.role_id),
|
||||
Some("adding new council members"),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
tx.commit().await?;
|
||||
|
|
@ -2071,9 +2096,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
.filter(|member| member.roles.contains(&config.council.role_id.into()));
|
||||
|
||||
for person in fake_council {
|
||||
if let Ok(member) = guild_id.member(&http, person.user.id.get()).await {
|
||||
member.remove_role(&http, config.council.role_id).await?;
|
||||
}
|
||||
http.remove_member_role(
|
||||
guild_id,
|
||||
person.user.id,
|
||||
RoleId::new(config.council.role_id),
|
||||
Some("unfucking council: removing all council members"),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
let real_council = sqlx::query_scalar!(
|
||||
|
|
@ -2086,9 +2115,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
.await?;
|
||||
|
||||
for person in &real_council {
|
||||
if let Ok(member) = guild_id.member(&http, *person as u64).await {
|
||||
member.add_role(&http, config.council.role_id).await?;
|
||||
}
|
||||
http.add_member_role(
|
||||
guild_id,
|
||||
UserId::new(*person as u64),
|
||||
RoleId::new(config.council.role_id),
|
||||
Some("unfucking council: adding all council members from db"),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
|
|
|
|||
112
src/mod_log.rs
112
src/mod_log.rs
|
|
@ -1,6 +1,11 @@
|
|||
use std::sync::LazyLock;
|
||||
|
||||
use regex::Regex;
|
||||
use serenity::{
|
||||
all::prelude::CacheHttp,
|
||||
builder::{CreateAllowedMentions, CreateMessage, EditMessage},
|
||||
builder::{
|
||||
CreateAllowedMentions, CreateAttachment, CreateMessage, EditAttachments, EditMessage,
|
||||
},
|
||||
model::{
|
||||
channel::Message,
|
||||
guild::audit_log::AuditLogEntry,
|
||||
|
|
@ -9,6 +14,10 @@ use serenity::{
|
|||
};
|
||||
use tracing::{debug, instrument, warn};
|
||||
|
||||
static QUOTE_CONTENT_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"«([^»]*)»").unwrap());
|
||||
static ZSWPQUOTE_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\u{200B}[«»]").unwrap());
|
||||
static QUOTE_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[«»]").unwrap());
|
||||
|
||||
use crate::{Data, Error};
|
||||
|
||||
#[instrument(skip(ctx, data), err)]
|
||||
|
|
@ -245,8 +254,9 @@ pub async fn handle_dm(
|
|||
|
||||
if let Some(action) = action {
|
||||
sqlx::query!(
|
||||
"UPDATE mod_actions SET reason = ? WHERE id = ?",
|
||||
"UPDATE mod_actions SET reason = ?, reason_response_id = ? WHERE id = ?",
|
||||
message.content,
|
||||
message.id.get() as i64,
|
||||
action.id
|
||||
)
|
||||
.execute(&pool)
|
||||
|
|
@ -256,11 +266,24 @@ pub async fn handle_dm(
|
|||
let msg = MessageId::new(msg as u64);
|
||||
let channel = ChannelId::new(config.mod_log_id);
|
||||
let log = channel.message(&ctx.http(), msg).await?;
|
||||
let mut edit_attachs = EditAttachments::new();
|
||||
for att in &message.attachments {
|
||||
if att.size > config.max_attach_redownload_size {
|
||||
continue;
|
||||
}
|
||||
let bytes = att.download().await?;
|
||||
let attachment = CreateAttachment::bytes(bytes, &att.filename);
|
||||
edit_attachs = edit_attachs.add(attachment);
|
||||
}
|
||||
let edit = EditMessage::new()
|
||||
.content(log.content.replace(
|
||||
"no reason specified",
|
||||
&format!("reason «{}»", message.content.clone()),
|
||||
&format!(
|
||||
"reason «{}»",
|
||||
QUOTE_RE.replace_all(&message.content.clone(), "\u{200B}$0")
|
||||
),
|
||||
))
|
||||
.attachments(edit_attachs)
|
||||
.allowed_mentions(
|
||||
CreateAllowedMentions::new()
|
||||
.empty_users()
|
||||
|
|
@ -290,3 +313,86 @@ pub async fn handle_dm(
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument(skip(ctx, data, message), err)]
|
||||
pub async fn edit_dm(
|
||||
ctx: &serenity::client::Context,
|
||||
message: &Message,
|
||||
data: &Data,
|
||||
) -> Result<(), Error> {
|
||||
let config = data.config.clone();
|
||||
let pool = data.pool.clone();
|
||||
|
||||
let action = sqlx::query!(
|
||||
r"SELECT id, mod_log_id, reason_ask_id FROM mod_actions
|
||||
WHERE mod_id = ? AND reason_response_id = ?
|
||||
ORDER BY id DESC",
|
||||
message.author.id.get() as i64,
|
||||
message.id.get() as i64,
|
||||
)
|
||||
.fetch_optional(&pool)
|
||||
.await?;
|
||||
|
||||
if let Some(action) = action {
|
||||
sqlx::query!(
|
||||
"UPDATE mod_actions SET reason = ? WHERE id = ?",
|
||||
message.content,
|
||||
action.id
|
||||
)
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
|
||||
if let Some(msg) = action.mod_log_id {
|
||||
let msg = MessageId::new(msg as u64);
|
||||
let channel = ChannelId::new(config.mod_log_id);
|
||||
let log = channel.message(&ctx.http(), msg).await?;
|
||||
let mut edit_attachs = EditAttachments::new();
|
||||
for att in &message.attachments {
|
||||
if att.size > config.max_attach_redownload_size {
|
||||
continue;
|
||||
}
|
||||
let bytes = att.download().await?;
|
||||
let attachment = CreateAttachment::bytes(bytes, &att.filename);
|
||||
edit_attachs = edit_attachs.add(attachment);
|
||||
}
|
||||
let edit = EditMessage::new()
|
||||
.content(QUOTE_CONTENT_RE.replace(
|
||||
&ZSWPQUOTE_RE.replace_all(&log.content, ""),
|
||||
format!(
|
||||
"«{}»",
|
||||
QUOTE_RE.replace_all(&message.content.clone(), "\u{200B}$0")
|
||||
),
|
||||
))
|
||||
.attachments(edit_attachs)
|
||||
.allowed_mentions(
|
||||
CreateAllowedMentions::new()
|
||||
.empty_users()
|
||||
.empty_roles()
|
||||
.everyone(false),
|
||||
);
|
||||
channel.edit_message(&ctx.http(), log, edit).await?;
|
||||
} else {
|
||||
warn!("failed to update mod log");
|
||||
}
|
||||
|
||||
if let Some(msg) = action.reason_ask_id {
|
||||
let umsg = msg as u64;
|
||||
let prev_ask_msg = message.channel_id.message(&ctx.http(), umsg).await?;
|
||||
|
||||
let edit = EditMessage::new().content(format!("{}!", prev_ask_msg.content));
|
||||
message
|
||||
.channel_id
|
||||
.edit_message(&ctx.http(), umsg, edit)
|
||||
.await?;
|
||||
} else {
|
||||
warn!("failed to update ask");
|
||||
}
|
||||
} else if !message.author.bot {
|
||||
message
|
||||
.channel_id
|
||||
.say(&ctx.http(), "Yo what are you editing about?")
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue