autorole start

This commit is contained in:
nora 2021-06-12 22:58:38 +02:00
parent 03087702af
commit 120521e68b
6 changed files with 127 additions and 26 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
/target
.idea
*.iml
token

28
Cargo.lock generated
View file

@ -6,6 +6,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "async-trait"
version = "0.1.50"
@ -456,6 +465,8 @@ dependencies = [
name = "killjoy_turret"
version = "0.1.0"
dependencies = [
"lazy_static",
"regex",
"serde",
"serde_json",
"serenity",
@ -708,6 +719,23 @@ dependencies = [
"rand_core",
]
[[package]]
name = "regex"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "reqwest"
version = "0.11.3"

View file

@ -10,9 +10,10 @@ edition = "2018"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
regex = "1.5.4"
lazy_static = "1.4.0"
[dependencies.serenity]
version = "0.10.7"
default-features = false
features = ["client", "gateway", "rustls_backend", "model", "framework", "standard_framework", "cache", "http", "utils"]
features = ["client", "gateway", "rustls_backend", "model", "framework", "standard_framework", "cache", "http", "utils", "collector"]

View file

@ -1,10 +1,96 @@
use serenity::framework::standard::Args;
use serde::{Deserialize, Serialize};
use serenity::framework::standard::{
macros::{command, group},
Args, CommandResult,
};
use serenity::model::prelude::*;
use serenity::prelude::*;
use serenity::{
async_trait,
collector::MessageCollectorBuilder,
framework::standard::{help_commands, CommandGroup, HelpOptions, StandardFramework},
futures::stream::StreamExt,
http::Http,
model::prelude::*,
prelude::*,
};
use std::collections::HashMap;
use std::time::Duration;
pub struct AutoRoleDataKey;
impl TypeMapKey for AutoRoleDataKey {
type Value = AutoRoleData;
}
#[group]
#[commands(autorole)]
#[description = "Auto role related commands"]
struct AutoRole;
#[command]
#[desciption = "Add auto assign roles"]
async fn auto_role(ctx: &Context, msg: &Message, args: &Args) {}
#[description = "Add auto assign roles"]
#[usage = "sdakhnfj"]
async fn autorole(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let http = &ctx.http;
let mut data = ctx.data.write().await;
let mut auto_roles = data.get_mut::<AutoRoleDataKey>();
msg.channel_id
.send_message(http, |m| {
m.embed(|e| {
e.title("Started auto role assignment.");
e.field(
"Enter an emote and the assigned role seperated by a space.",
"Write one message per role reaction.\n\n\
A non-matching message finishes the process. Cancel with 'cancel', or after 30 seconds timeout\n\n\
The emotes have to be from a server I'm in!",
false,
)
})
})
.await?;
while let Some(answer) = &msg
.author
.await_reply(&ctx)
.timeout(Duration::from_secs(30))
.await
{
if let Some(emote) = answer.content.split(' ').next() {
lazy_static! {
}
println!("should add emote: {}", emote);
answer.react(http, '☑').await?;
}
}
Ok(())
}
#[derive(Serialize, Deserialize)]
pub struct AutoRoleData {
messages: Vec<HashMap<DEmote, DRole>>,
}
impl Default for AutoRoleData {
fn default() -> Self {
Self {
messages: Vec::default(),
}
}
}
#[derive(Serialize, Deserialize, Eq, PartialEq, Hash)]
struct DEmote {
name: String,
id: String,
}
#[derive(Serialize, Deserialize, Eq, PartialEq)]
struct DRole {
name: String,
id: String,
}

View file

@ -1,7 +1,5 @@
use std::collections::HashSet;
use lazy_static::lazy_static;
use rand::Rng;
use serenity::client::Context;
use serenity::framework::standard::{
help_commands,

View file

@ -2,7 +2,6 @@ mod autorole;
mod commands;
mod general;
use serde::{Serialize, Deserialize};
use std::collections::{HashMap, HashSet};
use std::fs;
@ -11,17 +10,8 @@ use serenity::framework::StandardFramework;
use serenity::http::Http;
use serenity::model::id::{ChannelId, UserId};
use serenity::{async_trait, model::gateway::Ready, prelude::*};
#[derive(Serialize, Deserialize)]
struct AutoRoleData {
}
pub struct AutoRoleDataKey;
impl TypeMapKey for AutoRoleDataKey {
type Value = AutoRoleData;
}
use crate::autorole::{AUTOROLE_GROUP, AutoRoleDataKey, AutoRoleData};
use crate::commands::MY_HELP;
struct Handler;
@ -34,7 +24,7 @@ impl EventHandler for Handler {
#[tokio::main]
async fn main() {
let token = fs::read_to_string("bot_token").expect("Expected bot token in file 'bot_token'");
let token = fs::read_to_string("token").expect("Expected bot token in file 'bot_token'");
let http = Http::new_with_token(&token);
@ -58,11 +48,8 @@ async fn main() {
.delimiter(" ")
.owners(owners)
})
.normal_message(normal_message)
.help(&MY_HELP)
.group(&GENERAL_GROUP)
.group(&MEME_GROUP)
.group(&ADMIN_GROUP);
.group(&AUTOROLE_GROUP);
let mut client = Client::builder(&token)
.event_handler(Handler)
@ -72,7 +59,7 @@ async fn main() {
{
let mut data = client.data.write().await;
data.insert::<LastMessageInChannel>(HashMap::default());
data.insert::<AutoRoleDataKey>(AutoRoleData::default());
}
if let Err(why) = client.start().await {