summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 61a00c1287c48406db3d0a21331611b682126621 (plain)
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
use lazy_static::lazy_static;
use regex::Regex;
use serenity::async_trait;
use serenity::framework::standard::macros::{command, group};
use serenity::framework::standard::{CommandResult, StandardFramework};
use serenity::model::application::command::Command;
use serenity::model::application::interaction::{Interaction, InteractionResponseType};
use serenity::model::channel::Message;
use serenity::model::id::GuildId;
use serenity::model::prelude::Ready;
use serenity::prelude::*;
use std::{env, vec};

mod api;
mod commands;
mod consts;
mod utils;

const TEAWIE_GUILD: GuildId = GuildId(1055663552679137310);
const ALLOWED_GUILDS: [GuildId; 2] = [TEAWIE_GUILD, GuildId(1091969030694375444)];
const BOT: u64 = 1056467120986271764;

#[group]
#[commands(bing, ask, random_lore, random_teawie, teawiespam)]
struct General;

struct Handler;

#[async_trait]
impl EventHandler for Handler {
	/*
	 * echo some messages when they're sent
	 */
	async fn message(&self, ctx: Context, msg: Message) {
		let author = msg.author.id.as_u64();

		if author == &BOT
			|| !ALLOWED_GUILDS.contains(&msg.guild_id.unwrap_or_else(|| GuildId::from(0)))
		{
			return;
		}

		let mut echo_msgs = vec!["🗿", "Twitter's Recommendation Algorithm"];

		for emoji in consts::TEAMOJIS {
			// i was also lazy here
			echo_msgs.push(emoji);
		}

		let mut should_echo = echo_msgs.contains(&msg.content.as_str());

		if !should_echo {
			lazy_static! {
				static ref EMOJI_RE: Regex = Regex::new(r"^<a?:(\w+):\d+>$").unwrap();
			}
			if let Some(cap) = EMOJI_RE.captures(msg.content.as_str()) {
				if let Some(emoji_name) = cap.get(1) {
					let emoji_name = emoji_name.as_str();
					should_echo = emoji_name.contains("moai") || emoji_name.contains("moyai");
				}
			}
		}

		if should_echo {
			let send = msg.reply(&ctx, msg.content.as_str());
			if let Err(why) = send.await {
				println!("error when replying to {:?}: {:?}", msg.content, why);
			}
		}
	}

	async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
		if let Interaction::ApplicationCommand(command) = interaction {
			println!("Received command interaction: {:#?}", command);
			let content = match command.data.name.as_str() {
				"ask" => commands::ask::run(&command.data.options).await,
				"bottom" => commands::bottom::run(&command.data.options).await,
				"convertto" => commands::convert::run(&command.data.options).await,
				"copypasta" => {
					commands::copypasta::run(&command.data.options, command.channel_id, &ctx.http)
						.await
				}
				"random_lore" => commands::random_lore::run(&command.data.options).await,
				"random_teawie" => commands::random_teawie::run(&command.data.options).await,
				_ => "not implemented :(".to_string(),
			};

			if let Err(why) = command
				.create_interaction_response(&ctx.http, |response| {
					response
						.kind(InteractionResponseType::ChannelMessageWithSource)
						.interaction_response_data(|message| message.content(content))
				})
				.await
			{
				println!("cannot respond to slash command: {}", why);
			}
		}
	}

	async fn ready(&self, ctx: Context, ready: Ready) {
		println!("connected as {:?}", ready.user.name);

		let guild_commands =
			GuildId::set_application_commands(&TEAWIE_GUILD, &ctx.http, |commands| {
				commands
					.create_application_command(|command| commands::copypasta::register(command))
			})
			.await;

		println!("registered guild commands: {:#?}", guild_commands);

		let commands = Command::set_global_application_commands(&ctx.http, |commands| {
			commands
				.create_application_command(|command| commands::ask::register(command))
				.create_application_command(|command| commands::bottom::register(command))
				.create_application_command(|command| commands::convert::register(command))
				.create_application_command(|command| commands::random_lore::register(command))
				.create_application_command(|command| commands::random_teawie::register(command))
		})
		.await;

		println!("registered global commands: {:#?}", commands);
	}
}

#[tokio::main]
async fn main() {
	let framework = StandardFramework::new()
		.configure(|c| c.prefix("!"))
		.group(&GENERAL_GROUP);

	let token = env::var("TOKEN").expect("couldn't find token in environment.");

	let intents = GatewayIntents::all();
	let mut client = Client::builder(token, intents)
		.event_handler(Handler)
		.framework(framework)
		.await
		.expect("error creating client");

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

#[command]
async fn bing(ctx: &Context, msg: &Message) -> CommandResult {
	msg.channel_id
		.send_message(&ctx.http, |m| m.content("bong"))
		.await?;

	Ok(())
}

#[command]
async fn ask(ctx: &Context, msg: &Message) -> CommandResult {
	let resp = utils::get_random_response().await;
	msg.channel_id
		.send_message(&ctx.http, |m| m.content(resp))
		.await?;

	Ok(())
}

#[command]
async fn random_lore(ctx: &Context, msg: &Message) -> CommandResult {
	let resp = utils::get_random_lore().await;
	msg.channel_id
		.send_message(&ctx.http, |m| m.content(resp))
		.await?;

	Ok(())
}

#[command]
async fn random_teawie(ctx: &Context, msg: &Message) -> CommandResult {
	let resp = api::guzzle::get_random_teawie().await;
	msg.channel_id
		.send_message(&ctx.http, |m| m.content(resp))
		.await?;

	Ok(())
}

#[command]
async fn teawiespam(ctx: &Context, msg: &Message) -> CommandResult {
	if !ALLOWED_GUILDS.contains(&msg.guild_id.unwrap_or_else(|| GuildId::from(0))) {
		return Ok(());
	}

	let mut resp = String::new();

	for _ in 0..50 {
		resp += "<:teawiesmile:1056438046440042546>";
	}

	msg.channel_id
		.send_message(&ctx.http, |m| m.content(resp))
		.await?;

	Ok(())
}