-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
241 lines (214 loc) · 7.17 KB
/
main.rs
File metadata and controls
241 lines (214 loc) · 7.17 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
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
mod bot_types;
mod bot_utils;
mod commands;
mod emoji;
mod runescape_utils;
// Commands;
use crate::commands::admin::*;
use crate::commands::help::*;
use crate::commands::judge::*;
use crate::commands::runescape::*;
use crate::commands::score::*;
use crate::commands::shop::*;
use crate::commands::smash::*;
use crate::commands::trade::*;
use crate::bot_types::{Data, Error};
use crate::bot_utils::{get_count, is_bot, reset_count};
use crate::emoji::get_emoji;
use poise::serenity_prelude;
use rand::Rng;
use serenity::all::Member;
use serenity::async_trait;
use serenity::framework::standard::macros::hook;
use serenity::http::*;
use serenity::model::Timestamp;
use serenity::model::channel::{Message, Reaction, ReactionType};
use serenity::model::gateway::Ready;
use serenity::model::id::{ChannelId, GuildId, MessageId};
use serenity::prelude::*;
struct Handler;
const MAX_BOMB_RANGE: i64 = 300;
#[async_trait]
impl EventHandler for Handler {
async fn cache_ready(&self, _ctx: Context, _guilds: Vec<GuildId>) {
println!("Cache Ready - Environment: {}", bot_utils::get_env());
}
/**
Add new users to the database.
*/
async fn message(&self, _ctx: Context, msg: Message) {
bot_utils::create_in_db(&msg.author.id.to_string(), &msg.author.name).await;
if is_bot(msg.author.id.to_string()) {
return;
}
let mut _rng = rand::rng().random_range(0..MAX_BOMB_RANGE);
let current_number_of_bombs = get_count("mine").await;
if _rng <= current_number_of_bombs {
let mut member = get_member(_ctx.clone(), msg.clone()).await;
let time_out_time = get_time_out_time();
member
.disable_communication_until_datetime(&_ctx.http.clone(), time_out_time)
.await
.unwrap();
reset_count("mine").await;
msg.reply(
&_ctx.http,
format!(
"{} You're our lucky loser! See you in 10 minutes. :3 {}/{}",
get_emoji("winner"),
current_number_of_bombs,
MAX_BOMB_RANGE,
),
)
.await
.unwrap();
}
}
async fn reaction_add(&self, _ctx: Context, _add_reaction: Reaction) {
let reaction = _add_reaction.emoji;
let message = get_message_from_id(_add_reaction.channel_id, _add_reaction.message_id)
.await
.unwrap()
.author;
let score = get_points_from_emoji(&reaction);
if _add_reaction.user_id.unwrap().to_string() == message.id.to_string() {
// Don't let the message owner add a reaction to themselves.
return;
}
if score == 2 {
bot_utils::plus_two(
&_add_reaction.user_id.unwrap().to_string(),
&message.id.to_string(),
false,
)
.await;
}
if score == -2 {
bot_utils::minus_two(
&_add_reaction.user_id.unwrap().to_string(),
&message.id.to_string(),
false,
)
.await;
}
bot_utils::score_update(&message.id.to_string(), score).await;
}
async fn reaction_remove(&self, _ctx: Context, _removed_reaction: Reaction) {
let reaction = _removed_reaction.emoji;
let message =
get_message_from_id(_removed_reaction.channel_id, _removed_reaction.message_id)
.await
.unwrap()
.author;
let score = get_points_from_emoji(&reaction);
if _removed_reaction.user_id.unwrap().to_string() == message.id.to_string() {
// Don't let the message owner remove a reaction from themselves.
return;
}
if score == 2 {
bot_utils::plus_two(
&_removed_reaction.user_id.unwrap().to_string(),
&message.id.to_string(),
true,
)
.await;
}
if score == -2 {
bot_utils::minus_two(
&_removed_reaction.user_id.unwrap().to_string(),
&message.id.to_string(),
true,
)
.await;
}
bot_utils::score_update(&message.id.to_string(), -score).await;
}
async fn ready(&self, _: Context, ready: Ready) {
println!(
"{} is connected! Environment: {}",
ready.user.name,
bot_utils::get_env()
);
}
}
/**
Returns a time 10 minutes from now.
**/
fn get_time_out_time() -> Timestamp {
let current_time: i64 = Timestamp::now().unix_timestamp();
let time_out = 600;
Timestamp::from_unix_timestamp(current_time + time_out as i64).unwrap()
}
/**
Returns the Member of the message sent.
**/
async fn get_member(_ctx: Context, msg: Message) -> Member {
let guild_id = msg.guild_id.unwrap();
guild_id.member(&_ctx.http, msg.author.id).await.unwrap()
}
fn get_points_from_emoji(reaction: &ReactionType) -> i16 {
emoji::points_from_reaction(reaction)
}
#[hook]
async fn unknown_command(_ctx: &Context, _msg: &Message, unknown_command_name: &str) {
println!("Could not find command named '{}'", unknown_command_name);
}
#[tokio::main]
async fn main() {
let token = bot_utils::get_secret();
// Set gateway intents, which decides what events the bot will be notified about
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::GUILDS
| GatewayIntents::GUILD_MESSAGE_REACTIONS
| GatewayIntents::GUILD_VOICE_STATES
| GatewayIntents::MESSAGE_CONTENT;
// TODO make commands combine vectors from all the command files.
let framework = poise::Framework::<Data, Error>::builder()
.options(poise::FrameworkOptions {
commands: vec![
ping(),
version(),
judge(),
score(),
top(),
leader(),
smash(),
trade(),
wallet(),
shop(),
count(),
help(),
grand_exchange(),
grand_exchange_history(),
ge_set_alias(),
lookup_alias(),
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("!".into()),
..Default::default()
},
..Default::default()
})
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {})
})
})
.build();
let client = serenity_prelude::ClientBuilder::new(token, intents)
.framework(framework)
.event_handler(Handler)
.await;
client.unwrap().start().await.unwrap();
}
async fn get_message_from_id(
channel_id: ChannelId,
message_id: MessageId,
) -> serenity::Result<Message> {
let token = bot_utils::get_secret();
let http = Http::new(&token);
let message = channel_id.message(&http, message_id);
return message.await;
}