aboutsummaryrefslogtreecommitdiff
path: root/src/commands/reactionroles.rs
blob: eb5f0985d7b66044fcd8e4dc5e45a5db18ffc372 (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
use std::{time::Duration, str::FromStr};

use crate::{Context, Error};
use poise::serenity_prelude::{self as serenity, ReactionType, Emoji, ArgumentConvert};
use ::serenity::framework::standard::{Args, Delimiter};

#[cfg(debug_assertions)]
async fn allowed_to_create_roles(ctx: Context<'_>) -> Result<bool, Error> {
    if ctx.author().id.0 == 118455061222260736u64 {
        Ok(true)
    } else {
        Ok(false)
    }
}

#[cfg(not(debug_assertions))]
async fn allowed_to_create_roles(ctx: Context<'_>) -> Result<bool, Error> {
    if let Some(guild) = ctx.guild() {
        if guild.owner.id == ctx.author().id {
            Ok(true)
        } else {
            let member = guild.member(ctx.discord(), ctx.author().id)?;
            let member_permissions = member.permissions().await?;

            Ok(member_permissions.manage_roles())
    }
    Ok(false)
}
}

/// Manages reaction role menus
///
/// Subcommands:
///     - init
///     - add
///     - remove
#[poise::command(prefix_command, ephemeral,
                 check="allowed_to_create_roles")]
pub async fn rroles(ctx: Context<'_>) -> Result<(), Error> {
    ctx.say("Maybe you meant to request help for this? Try `/help rroles`").await?;
    Ok(())
}

/// Initializes a reaction role menu in the given channel
///
/// Usage:
///     rroles init <#channel>
/// Example:
///     rroles init #get-roles
#[poise::command(prefix_command, ephemeral,
                 check="allowed_to_create_roles")]
pub async fn init(ctx: Context<'_>,
                  #[description = "The channel to create a new role menu in"]
                  channel: serenity::ChannelId) -> Result<(), Error> {
    let mut rolemenu_msg = channel.send_message(ctx.discord(), |m| {
        m.embed(|e| {
            e.title("Reaction Role Menu");
            e.description("I haven't been initialized yet! Hold on just a second");
            e
        });
        m
    }).await?;

    let mut menu = ctx.send(|m| {
        m.embed(|e| {
            e.title("Reaction Role Setup");
            e.description("Welcome to the setup menu! I'm going to help guide you through setting up your reaction roles.\n\nFirst, what should the title of your menu?");
            e
        });
        m
    }).await?.unwrap().message().await?;

    if let Some(title) = ctx.author().clone().await_reply(ctx.discord()).timeout(Duration::from_secs(10)).await {
        rolemenu_msg.edit(ctx.discord(), |m| {
            m.embed(|e| {
                e.title(title.content.clone());
                e
            });
            m
        }).await?;

        menu.edit(ctx.discord(), |m| {
            m.embed(|e| {
                e.title("Reaction Role Setup");
                e.description(format!("Great! I've set the title of your menu to `{}`.\n\nNext, let's add some roles! Reply to this message with a list of role names and reactions, like this:\n\n:female_sign::She/Her,:male_sign::He/Him", title.content.clone()));
                e
            });
            m
        }).await?;
    } else {
        ctx.say("No response within 10 seconds").await?;
        return Ok(());
    }
    {
        let pool = ctx.data().pg.lock().unwrap().clone();
        if let Some(roles) = ctx.author().clone().await_reply(ctx.discord()).timeout(Duration::from_secs(30)).await {
            let mut args = Args::new(&roles.content, &[Delimiter::Single(',')]);

            let mut rolelist_formatted = String::from("Choose the appropriate reaction to gain the role!\n");

            for a in args.iter::<String>() {
                if let Ok(tuple) = a {
                    let split_str: Vec<&str> = tuple.split(':').collect();
                    let reaction = ReactionType::from_str(split_str[0])?;
                    let role_name = split_str[1];
                    if let Some(role) = ctx.guild().unwrap().role_by_name(role_name) {
                        sqlx::query!("INSERT INTO reaction_roles (channel_id, message_id, guild_id, reaction, role_id) VALUES ($1, $2, $3, $4, $5)", rolemenu_msg.channel_id.0.to_string(), rolemenu_msg.id.0.to_string(), ctx.guild_id().unwrap().0.to_string(), reaction.to_string(), role.id.0.to_string()).execute(&pool).await?;
                        rolemenu_msg.react(ctx.discord(), reaction.clone()).await?;
                        rolelist_formatted.push_str(&format!("{} - <@&{}>\n", get_reactiontype_display(&reaction), role.id.0.clone()));
                    } else {
                        ctx.say(format!("Invalid role provided: {}", role_name)).await?;
                        return Ok(());
                    }
                }
            }
            let title = rolemenu_msg.clone().embeds[0].title.clone().unwrap_or("Reaction Role Menu".to_string());
            rolemenu_msg.edit(ctx.discord(), |m| {
                m.embed(|e| {
                    e.title(title); 
                    e.description(rolelist_formatted);
                    e
                });
                m
            }).await?;
        } else {
            ctx.say("No response within 30 seconds").await?;
            return Ok(());
        }
    }
     
    Ok(())
}

fn get_reactiontype_display(rt: &ReactionType) -> String {
    match rt {
        ReactionType::Unicode(emote) => emote.clone(),
        ReactionType::Custom { id, name, .. } => {
            if let Some(name) = name {
                format!("<:{}:{}>", name, id)
            } else {
                format!("<{}>", id)
            }
        },
        _ => String::new(),
    }
}