-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
219 lines (197 loc) · 6.07 KB
/
main.rs
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
#![warn(clippy::cargo, clippy::nursery, clippy::pedantic, clippy::restriction)]
#![allow(
clippy::blanket_clippy_restriction_lints,
clippy::missing_docs_in_private_items,
clippy::implicit_return,
clippy::shadow_same,
clippy::pattern_type_mismatch
)]
use std::{env, sync::Arc};
use anyhow::{Context as _, Result};
use futures_util::StreamExt;
use sqlx::PgPool;
use twilight_cache_inmemory::{InMemoryCache, ResourceType};
use twilight_gateway::{Cluster, EventTypeFlags};
use twilight_http::{client::ClientBuilder, Client};
use twilight_model::{
application::component::Component,
channel::{message::AllowedMentions, Channel},
gateway::{event::Event, Intents},
guild::{PartialMember, Permissions},
id::{
marker::{ApplicationMarker, ChannelMarker, GuildMarker, UserMarker},
Id,
},
user::User,
};
use twilight_webhook::{
cache::WebhooksCache,
util::{MinimalMember, MinimalWebhook},
};
mod auto_spoiler;
mod database;
mod interaction;
pub type Context = Arc<ContextInner>;
pub struct ContextInner {
http: Client,
cache: InMemoryCache,
db: PgPool,
webhooks: WebhooksCache,
application_id: Id<ApplicationMarker>,
user_id: Id<UserMarker>,
owner_channel_id: Id<ChannelMarker>,
}
#[tokio::main]
async fn main() -> Result<()> {
dotenvy::dotenv()?;
let intents = Intents::GUILDS
| Intents::GUILD_WEBHOOKS
| Intents::GUILD_MESSAGES
| Intents::MESSAGE_CONTENT;
let event_types = EventTypeFlags::INTERACTION_CREATE
| EventTypeFlags::WEBHOOKS_UPDATE
| EventTypeFlags::MESSAGE_CREATE
| EventTypeFlags::GUILD_CREATE
| EventTypeFlags::GUILD_UPDATE
| EventTypeFlags::GUILD_DELETE
| EventTypeFlags::CHANNEL_CREATE
| EventTypeFlags::CHANNEL_UPDATE
| EventTypeFlags::CHANNEL_DELETE
| EventTypeFlags::THREAD_CREATE
| EventTypeFlags::THREAD_DELETE
| EventTypeFlags::THREAD_UPDATE
| EventTypeFlags::THREAD_LIST_SYNC
| EventTypeFlags::THREAD_MEMBER_UPDATE
| EventTypeFlags::THREAD_MEMBERS_UPDATE
| EventTypeFlags::ROLE_CREATE
| EventTypeFlags::ROLE_UPDATE
| EventTypeFlags::ROLE_DELETE
| EventTypeFlags::MEMBER_ADD
| EventTypeFlags::MEMBER_UPDATE
| EventTypeFlags::MEMBER_REMOVE;
let resource_types =
ResourceType::GUILD | ResourceType::CHANNEL | ResourceType::MEMBER | ResourceType::ROLE;
let token = env::var("BOT_TOKEN")?;
let (cluster, mut events) = Cluster::builder(token.clone(), intents)
.event_types(event_types)
.build()
.await?;
let cluster_spawn = Arc::new(cluster);
tokio::spawn(async move { cluster_spawn.up().await });
let http = ClientBuilder::new()
.token(token)
.default_allowed_mentions(AllowedMentions::default())
.build();
let application = http
.current_user_application()
.exec()
.await?
.model()
.await?;
let owner_channel_id = http
.create_private_channel(
application
.owner
.context("application doesn't include owner")?
.id,
)
.exec()
.await?
.model()
.await?
.id;
interaction::create(&http, application.id).await?;
let ctx = Arc::new(ContextInner {
cache: InMemoryCache::builder()
.resource_types(resource_types)
.build(),
db: PgPool::connect(&env::var("DATABASE_URL")?).await?,
webhooks: WebhooksCache::new(),
user_id: http.current_user().exec().await?.model().await?.id,
application_id: application.id,
owner_channel_id,
http,
});
while let Some((_, event)) = events.next().await {
ctx.cache.update(&event);
tokio::spawn(handle_event(Arc::clone(&ctx), event));
}
Ok(())
}
#[allow(clippy::print_stderr, clippy::wildcard_enum_match_arm)]
async fn handle_event(ctx: Context, event: Event) {
ctx.cache.update(&event);
ctx.webhooks.update(&event);
if let Err(err) = _handle_event(Arc::clone(&ctx), event).await {
eprintln!("{err:#?}");
}
}
#[allow(clippy::print_stderr, clippy::wildcard_enum_match_arm)]
async fn _handle_event(ctx: Context, event: Event) -> Result<()> {
match event {
Event::InteractionCreate(interaction) => interaction::handle(ctx, interaction.0).await?,
Event::WebhooksUpdate(update) => {
ctx.webhooks
.validate(
&ctx.http,
update.channel_id,
ctx.cache
.permissions()
.in_channel(ctx.user_id, update.channel_id)?,
)
.await?;
}
Event::MessageCreate(message) => auto_spoiler::edit(ctx, (*message).0).await?,
_ => (),
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
async fn webhook(
ctx: &Context,
member: &PartialMember,
user: &User,
guild_id: Id<GuildMarker>,
channel_id: Id<ChannelMarker>,
thread_id: Option<Id<ChannelMarker>>,
content: &str,
components: &[Component],
) -> Result<()> {
MinimalWebhook::try_from(
&*ctx
.webhooks
.get_infallible(&ctx.http, channel_id, "tw or tag sender")
.await?,
)?
.execute_as_member(
&ctx.http,
thread_id,
&MinimalMember::from_partial_member(member, Some(guild_id), user),
)?
.content(content)?
.components(components)?
.exec()
.await?;
Ok(())
}
fn has_permissions(
ctx: &Context,
channel_id: Id<ChannelMarker>,
permissions: Permissions,
) -> Result<bool> {
Ok(ctx
.cache
.permissions()
.in_channel(ctx.user_id, channel_id)?
.contains(permissions))
}
fn channel_pair(channel: &Channel) -> Result<(Id<ChannelMarker>, Option<Id<ChannelMarker>>)> {
Ok(if channel.kind.is_thread() {
(
channel.parent_id.context("thread doesn't have a parent")?,
Some(channel.id),
)
} else {
(channel.id, None)
})
}