-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsupport.rs
More file actions
160 lines (145 loc) · 4.46 KB
/
support.rs
File metadata and controls
160 lines (145 loc) · 4.46 KB
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
use crate::config;
use crate::discord_bot::guild_storage::GuildStorage;
use futures::future::join_all;
use serenity::client::Context;
use serenity::model::channel::Message;
use serenity::model::id::GuildId;
use serenity::model::Timestamp;
const MAX_SUPPORT_USED_ON_TIME: i64 = 2 * 24 * 60 * 60; // 2 days
const MIN_SUPPORT_USE_TIME: i64 = 7 * 24 * 60 * 60; // 1 week
pub(crate) async fn run(
args: &str,
guild_id: GuildId,
ctx: Context,
message: &Message,
) -> Result<(), crate::Error> {
if args == "leaderboard" {
show_leaderboard(guild_id, ctx, message).await
} else {
run_normal(guild_id, ctx, message).await
}
}
async fn run_normal(
guild_id: GuildId,
ctx: Context,
message: &Message,
) -> Result<(), crate::Error> {
let Some(member) = &message.member else { return Ok(()) };
let now = Timestamp::now();
if member
.joined_at
.map(|joined_at| now.unix_timestamp() - joined_at.unix_timestamp() >= MIN_SUPPORT_USE_TIME)
!= Some(true)
{
message
.reply(
ctx.http,
"You haven't been in this Discord for long enough to use that command",
)
.await?;
return Ok(());
}
let Some(referenced_message) = &message.referenced_message else {
message.reply(ctx.http, "You need to reply to a message. Did you mean \"$support leaderboard\"?").await?;
return Ok(());
};
// Need to call http.get_member because referenced_message doesn't have enough information to
// obtain the member in any normal way
let referenced_member = ctx
.http
.get_member(guild_id.0, referenced_message.author.id.0)
.await?;
if referenced_member.joined_at.map(|joined_at| {
now.unix_timestamp() - joined_at.unix_timestamp() <= MAX_SUPPORT_USED_ON_TIME
}) != Some(true)
{
message
.reply(
ctx.http,
"This person has been in the Discord for too long to use this command on them",
)
.await?;
return Ok(());
}
if message
.channel(&ctx)
.await?
.guild()
.and_then(|channel| channel.parent_id)
== Some(config::get().support_channel)
{
message
.reply(ctx.http, "This is already a support channel")
.await?;
return Ok(());
}
referenced_message.reply_ping(&ctx.http, "Please read the message in the role reactions channel and react again. Questions should only go in the support channel").await?;
ctx.http
.remove_member_role(
guild_id.0,
referenced_member.user.id.0,
config::get().channel_access_role.0,
Some("support command"),
)
.await?;
let mut storage = GuildStorage::get_mut(guild_id).await;
if storage
.users_sent_to_support
.insert(referenced_member.user.id)
{
*storage
.send_to_support_leaderboard
.entry(message.author.id)
.or_default() += 1;
storage.save().await;
} else {
storage.discard();
}
Ok(())
}
async fn show_leaderboard(
guild_id: GuildId,
ctx: Context,
message: &Message,
) -> Result<(), crate::Error> {
let storage = GuildStorage::get(guild_id).await;
let mut entries: Vec<_> = storage
.send_to_support_leaderboard
.iter()
.map(|(user, count)| (*user, *count))
.collect();
drop(storage);
// sort in reverse by count
entries.sort_by_key(|(_, count)| !*count);
let embed_value = join_all(
entries
.iter()
.take(10)
.enumerate()
.map(|(i, (user, count))| {
let ctx = ctx.clone();
async move {
format!(
"{}. **{}**: {}",
i + 1,
user.to_user(&ctx)
.await
.map(|user| user.name)
.unwrap_or_else(|_| "<unknown>".to_owned()),
*count
)
}
}),
)
.await
.join("\n");
message
.channel_id
.send_message(&ctx, |new_message| {
new_message
.embed(|embed| embed.field("Send-to-support leaderboard:", embed_value, false))
.reference_message(message)
})
.await?;
Ok(())
}