From a6dc08df80fa1a7b52f2b0350364a94cf8444e58 Mon Sep 17 00:00:00 2001 From: kageru Date: Fri, 9 Apr 2021 17:32:03 +0200 Subject: [PATCH] initial? --- .gitignore | 1 + Cargo.toml | 11 ++++++++ src/main.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e83abb4 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "namebot" +version = "0.1.0" +authors = ["kageru "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serenity = "0.8.6" +time = "0.2.10" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..69bdcc3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,79 @@ +use serenity::client::Client; +use serenity::framework::standard::{ + macros::{command, group}, + CommandResult, StandardFramework, +}; +use serenity::model::{ + channel::Message, + id::{ChannelId, GuildId}, + prelude::User, +}; +use serenity::prelude::*; +use std::fs::File; +use std::io::{self, BufRead, BufReader}; +use time::Duration; + +#[group] +#[commands(name)] +struct Fluff; +struct Handler; +impl EventHandler for Handler { + fn guild_ban_addition(&self, ctx: Context, guild_id: GuildId, _: User) { + if guild_id == 427456811973738498 { + if let Err(e) = + ChannelId(562731470423064587).say(&ctx, "Das hier ist eine flauschige Diktatur!") + { + eprintln!("Couldn’t send message because {:?}", e); + } + } + } +} + +fn read_token() -> io::Result { + let reader = BufReader::new(File::open("secret")?); + reader.lines().next().unwrap() +} + +fn main() { + let mut client = Client::new(&read_token().expect("no secret file"), Handler) + .expect("Error creating client"); + client.with_framework( + StandardFramework::new() + .configure(|c| c.prefix("!")) + .group(&FLUFF_GROUP), + ); + + if let Err(why) = client.start() { + println!("An error occurred while running the client: {:?}", why); + } +} + +#[command] +fn name(ctx: &mut Context, msg: &Message) -> CommandResult { + if let Some((_, name)) = msg.content.split_once(' ') { + let (name, adjustment) = name.split_once(" -").unwrap_or((name, "0")); + let adjustment = adjustment.strip_suffix("h").unwrap_or(adjustment); + msg.guild(&ctx) + .unwrap() + .read() + .edit_member(&ctx, msg.author.id, |m| m.nickname(name))?; + let now = time::OffsetDateTime::now_utc() + - adjustment + .parse::() + .map(|a| (a * 3600.0) as i64) + .map(Duration::seconds) + .ok() + .unwrap_or_else(Duration::zero); + println!( + "{},{},{},{},{}", + now.format("%d.%m.%Y %H:%M"), + name, + now.format("%y%m"), + msg.author.id, + now.unix_timestamp() + ); + } else { + msg.reply(&ctx, "Please specify a new name.")?; + } + Ok(()) +}