aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: e859c3d90af5a062e2f718cb5a2e412d7605ff2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (C) 2021-2021 Fuwn
// SPDX-License-Identifier: GPL-3.0-only

#![feature(
  type_ascription,
  hash_set_entry,
  type_name_of_val,
  decl_macro,
  proc_macro_hygiene
)]
#![deny(
  warnings,
  nonstandard_style,
  unused,
  future_incompatible,
  rust_2018_idioms,
  unsafe_code
)]
#![deny(clippy::all, clippy::nursery, clippy::pedantic)]
#![recursion_limit = "128"]
#![doc(
  html_logo_url = "https://cdn.discordapp.com/icons/854071194453671976/bc8c80e4bcfa66ecd55da8ceaa80\
  f5a8.webp?size=128",
  html_favicon_url = "https://cdn.discordapp.com/icons/854071194453671976/bc8c80e4bcfa66ecd55da8cea\
  a80f5a8.webp?size=128"
)]

pub mod commands;
pub mod config;
pub mod database;

#[macro_use]
extern crate log;

use std::{collections::HashSet, sync::Arc};

#[allow(clippy::wildcard_imports)]
use commands::*;
use serenity::{
  async_trait,
  client::bridge::gateway::{GatewayIntents, ShardManager},
  framework::{standard::macros::group, StandardFramework},
  http::Http,
  model::{
    channel::Reaction,
    gateway::{Activity, Ready},
  },
  prelude::*,
};

use crate::config::Config;

pub struct ShardManagerContainer;
impl TypeMapKey for ShardManagerContainer {
  type Value = Arc<Mutex<ShardManager>>;
}

struct Handler;
#[async_trait]
impl EventHandler for Handler {
  async fn reaction_add(&self, ctx: Context, reaction: Reaction) {
    if let Some(guild) = reaction.guild_id {
      if let Ok(role) = database::Database::new().get_reaction_role(reaction.message_id.0) {
        guild
          .member(&ctx, reaction.user_id.expect("unable to locate user id"))
          .await
          .expect("unable to locate member")
          .add_role(&ctx, role)
          .await
          .expect("unable to add role to member");
      }
    }
  }

  async fn reaction_remove(&self, ctx: Context, reaction: Reaction) {
    if let Some(guild) = reaction.guild_id {
      if let Ok(role) = database::Database::new().get_reaction_role(reaction.message_id.0) {
        guild
          .member(&ctx, reaction.user_id.expect("unable to locate user id"))
          .await
          .expect("unable to locate member")
          .remove_role(&ctx, role)
          .await
          .expect("unable to add role to member");
      }
    }
  }

  async fn ready(&self, ctx: Context, ready: Ready) {
    info!("connected to discord gateway as {}", ready.user.name);

    ctx
      .set_activity(Activity::watching(">help - discord.io/assembly"))
      .await;
  }
}

#[group]
#[commands(ping, help, say, poll)]
struct General;

#[group]
#[commands(create, remove, count)]
struct RoleReactions;

#[tokio::main]
async fn main() {
  dotenv::dotenv().ok();

  std::env::set_var("RUST_LOG", "dos_bot=info");
  pretty_env_logger::init();

  let http = Http::new_with_token(Config::get().token.as_str());

  let (owners, _bot_id) = match http.get_current_application_info().await {
    Ok(info) => {
      let mut owners = HashSet::new();
      owners.insert(info.owner.id);

      (owners, info.id)
    }
    Err(why) => panic!("could not access application info: {:?}", why),
  };

  let framework = StandardFramework::new()
    .configure(|c| c.owners(owners).prefix(">"))
    .group(&GENERAL_GROUP)
    .group(&ROLEREACTIONS_GROUP);

  let mut client = Client::builder(Config::get().token.as_str())
    .framework(framework)
    .event_handler(Handler)
    .intents(GatewayIntents::all())
    .await
    .expect("error creating dos-bot");

  {
    let mut data = client.data.write().await;
    data.insert::<ShardManagerContainer>(client.shard_manager.clone());
  }

  let shard_manager = client.shard_manager.clone();

  tokio::spawn(async move {
    tokio::signal::ctrl_c()
      .await
      .expect("could not register ctrl+c handler");
    shard_manager.lock().await.shutdown_all().await;
  });

  if let Err(why) = client.start().await {
    error!("client error: {:?}", why);
  }
}