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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
use std::{str::FromStr, time::Duration};
use crate::{models::ReactionRole, Context, Error};
use ::serenity::{
model::{guild::Role, id::ChannelId},
};
use poise::serenity_prelude::{self as serenity, ArgumentConvert, Emoji, ReactionType, RoleId};
#[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).await?;
let member_permissions = member.permissions(ctx.discord())?;
Ok(member_permissions.manage_roles())
}
} else {
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! Send the first emoji you want to add.", 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();
loop {
if let Some(emoji) = ctx
.author()
.clone()
.await_reply(ctx.discord())
.timeout(Duration::from_secs(30))
.await
{
let content = emoji.content.clone();
if content == "done" {
menu.edit(ctx.discord(), |m| {
m.embed(|e| {
e.title("Reaction Role Setup");
e.description("Nice work! You're all set up! Use `~rroles add` and `~rroles del` to manage the roles in this menu!");
e
});
m
}).await?;
break;
}
let reaction = ReactionType::from_str(&emoji.content)?;
menu.edit(ctx.discord(), |m| {
m.embed(|e| {
e.title("Reaction Role Setup");
e.description(format!("Sounds good! Let's give {} a role, okay? Reply to this message with the name of the role you'd like to assign to this emoji", reaction.clone()));
e
});
m
}).await?;
if let Some(role) = ctx
.author()
.clone()
.await_reply(ctx.discord())
.timeout(Duration::from_secs(30))
.await
{
if let Some(role) = ctx.guild().unwrap().role_by_name(&role.content) {
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?;
menu.edit(ctx.discord(), |m| {
m.embed(|e| {
e.title("Reaction Role Setup");
e.description(format!("Great! I've added that to the menu.\n\nLet's keep adding roles! Send the next emoji you want to add, or 'done' to finish setup."));
e
});
m
}).await?;
} else {
menu.edit(ctx.discord(), |m| {
m.embed(|e| {
e.title("Reaction Role Setup");
e.description("Whoops! I couldn't find that role! Let's try again! Send the emoji you want to assign to a role");
e
});
m
}).await?;
continue;
}
}
let reactions = sqlx::query_as!(
ReactionRole,
"SELECT * FROM reaction_roles WHERE message_id=$1",
rolemenu_msg.id.0.to_string()
)
.fetch_all(&pool)
.await?;
let rolelist_formatted = gen_reaction_list(reactions);
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(())
}
/// Adds a reaction role to the message
///
/// Usage:
/// ~rroles add <Message ID> <emoji> <role>
#[poise::command(prefix_command, check = "allowed_to_create_roles")]
pub async fn add(
ctx: Context<'_>,
#[description = "The Message ID"] message_id: u64,
#[description = "The emoji to assign to the role"] emoji: ReactionType,
#[description = "The role to assign to the emoji"] role_name: String,
) -> Result<(), Error> {
{
let pool = ctx.data().pg.lock().unwrap().clone();
// Make sure the emoji doesn't already exist
if let Ok(_) = sqlx::query!("SELECT * FROM reaction_roles WHERE message_id=$1 AND reaction=$2", message_id.to_string(), emoji.to_string()).fetch_one(&pool).await {
ctx.say("Whoops! That emoji already has something assigned to it! Try either removing it or picking a different emoji").await?;
return Ok(());
}
let role_menu = sqlx::query_as!(
ReactionRole,
"SELECT * FROM reaction_roles WHERE message_id=$1",
message_id.to_string()
)
.fetch_one(&pool)
.await?;
let guild = ctx.guild().unwrap();
let role = guild.role_by_name(&role_name).clone();
if let Some(r) = role.clone() {
let r = r.clone();
let channel_id = ChannelId(role_menu.channel_id.parse::<u64>()?);
sqlx::query!("INSERT INTO reaction_roles (channel_id, message_id, guild_id, reaction, role_id) VALUES ($1, $2, $3, $4, $5)", role_menu.channel_id.to_string(), role_menu.message_id.to_string(), ctx.guild_id().unwrap().0.to_string(), emoji.to_string(), r.id.to_string()).execute(&pool).await?;
let all_reactions = sqlx::query_as!(
ReactionRole,
"SELECT * FROM reaction_roles WHERE message_id=$1",
message_id.to_string()
)
.fetch_all(&pool)
.await?;
let channel = channel_id.to_channel(&ctx.discord()).await?;
let mut menu_msg = channel
.guild()
.unwrap()
.message(ctx.discord(), role_menu.message_id.parse::<u64>()?)
.await?;
update_menu(ctx, menu_msg).await?;
ctx.say("Done! I've added that to the list for you").await?;
} else {
ctx.say("Whoops! That role doesn't exist!").await?;
return Ok(());
}
}
Ok(())
}
/// Removes a reaction from the menu
///
/// Usage:
/// ~rroles del <Message ID> <emoji>
#[poise::command(prefix_command, check = "allowed_to_create_roles")]
pub async fn del(ctx: Context<'_>,
#[description = "The Message ID of the menu"]
message_id: u64,
#[description = "The emoji you want to remove"]
emoji: ReactionType,
) -> Result<(), Error> {
{
let pool = ctx.data().pg.lock().unwrap().clone();
let reaction_with_menu = sqlx::query_as!(ReactionRole, "SELECT * FROM reaction_roles WHERE message_id=$1 AND reaction=$2", message_id.to_string(), emoji.to_string()).fetch_one(&pool).await?;
let channel_id = ChannelId(reaction_with_menu.channel_id.parse::<u64>()?);
let channel = channel_id.to_channel(ctx.discord()).await?;
let mut message = channel.guild().unwrap().message(ctx.discord(), message_id).await?;
// Delete from DB
// We can just use `ReactionRole.id` here to avoid having to do more complex conditionals
sqlx::query!("DELETE FROM reaction_roles WHERE id=$1", reaction_with_menu.id).execute(&pool).await?;
message.delete_reaction_emoji(ctx.discord(), emoji).await?;
update_menu(ctx, message).await?;
}
ctx.say("Alright! I've removed that emoji from the menu.").await?;
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(),
}
}
fn gen_reaction_list(reacts: Vec<ReactionRole>) -> String {
let mut rolelist_formatted =
String::from("Choose the appropriate reaction to gain the role!\n\n");
for r in reacts {
rolelist_formatted.push_str(&format!("{} - <@&{}>\n", r.reaction, r.role_id));
}
rolelist_formatted
}
async fn update_menu(ctx: Context<'_>, mut msg: serenity::Message) -> Result<serenity::Message, Error> {
{
let pool = ctx.data().pg.lock().unwrap().clone();
let all_reactions = sqlx::query_as!(ReactionRole, "SELECT * FROM reaction_roles WHERE message_id=$1", msg.id.0.to_string()).fetch_all(&pool).await?;
let rolelist_formatted = gen_reaction_list(all_reactions.clone());
let title = msg.clone().embeds[0].title.clone().unwrap();
msg.edit(ctx.discord(), |m| {
m.embed(|e| {
e.title(title);
e.description(rolelist_formatted)
})
}).await?;
for r in all_reactions.iter() {
msg.react(ctx.discord(), ReactionType::from_str(&r.reaction)?).await?;
}
}
Ok(msg)
}
|