This commit is contained in:
parent
fca4872a73
commit
7d098f6b5d
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
/target
|
||||
.env
|
1077
Cargo.lock
generated
1077
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -6,5 +6,10 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
serenity = { version = "0.11.2", features = ["model", "client", "gateway", "rustls_backend"], default-features = false }
|
||||
tokio = { version = "1.19.2", features = ["macros", "rt-multi-thread"] }
|
||||
serenity = { version = "0.12", features = [
|
||||
"model",
|
||||
"client",
|
||||
"gateway",
|
||||
"rustls_backend",
|
||||
], default-features = false, git = "https://github.com/serenity-rs/serenity.git", branch = "current" }
|
||||
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from rust:1.61.0-alpine as builder
|
||||
FROM rust:1.61.0-alpine as builder
|
||||
|
||||
|
||||
WORKDIR /app
|
||||
|
@ -14,7 +14,7 @@ COPY src /app/src
|
|||
|
||||
RUN cargo build --release
|
||||
|
||||
from alpine as runtime
|
||||
FROM alpine as runtime
|
||||
|
||||
COPY --from=builder /app/target/release/pinbot-rs /app/pinbot-rs
|
||||
|
||||
|
|
47
src/main.rs
47
src/main.rs
|
@ -1,8 +1,9 @@
|
|||
use serenity::{
|
||||
all::ActivityData,
|
||||
client::{Context, EventHandler},
|
||||
model::{
|
||||
channel::Reaction,
|
||||
gateway::{Activity, Ready},
|
||||
gateway::Ready,
|
||||
id::{ChannelId, MessageId},
|
||||
},
|
||||
prelude::GatewayIntents,
|
||||
|
@ -14,26 +15,28 @@ struct Handler;
|
|||
#[serenity::async_trait]
|
||||
impl EventHandler for Handler {
|
||||
async fn ready(&self, c: Context, r: Ready) {
|
||||
let name = r.user.name;
|
||||
c.set_activity(Activity::playing("5x📌 pins a message!"))
|
||||
.await;
|
||||
let name = &r.user.name;
|
||||
c.set_activity(Some(ActivityData::custom("5x📌 pins a message!")));
|
||||
println!("{} is online!", name);
|
||||
}
|
||||
|
||||
async fn reaction_add(&self, c: Context, r: Reaction) {
|
||||
// println!("Reaction added: {:?}", r);
|
||||
if let Err(e) = handle_react(c, r).await {
|
||||
println!("Error: {:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
async fn reaction_remove(&self, c: Context, r: Reaction) {
|
||||
// println!("Reaction removed: {:?}", r);
|
||||
if let Err(e) = handle_react(c, r).await {
|
||||
println!("Error: {:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
async fn reaction_remove_all(&self, c: Context, c_id: ChannelId, m_id: MessageId) {
|
||||
let msg = match c.http.get_message(c_id.0, m_id.0).await {
|
||||
// println!("All reactions removed from message: {:?}", m_id);
|
||||
let msg = match c.http.get_message(c_id, m_id).await {
|
||||
Ok(m) => m,
|
||||
Err(e) => {
|
||||
println!("Error: {:?}", e);
|
||||
|
@ -49,19 +52,23 @@ impl EventHandler for Handler {
|
|||
}
|
||||
|
||||
async fn handle_react(c: Context, r: Reaction) -> Result<(), serenity::Error> {
|
||||
let emoji = r.emoji.as_data();
|
||||
if emoji == "📌" {
|
||||
let msg = r.message(c.http.clone()).await?;
|
||||
for r in &msg.reactions {
|
||||
let emoji = r.reaction_type.as_data();
|
||||
if emoji == "📌" {
|
||||
let count = r.count;
|
||||
if count >= 5 {
|
||||
msg.pin(c.http.clone()).await?;
|
||||
} else if count < 3 {
|
||||
msg.unpin(&c.http).await?;
|
||||
}
|
||||
}
|
||||
println!("Handling reaction: {:?}", r);
|
||||
|
||||
if r.emoji.unicode_eq("📌") {
|
||||
println!("Pin reaction detected!");
|
||||
let msg = r.message(&c).await?;
|
||||
// println!("Message: {:?}", msg);
|
||||
let count = msg
|
||||
.reactions
|
||||
.iter()
|
||||
.find(|r| r.reaction_type.unicode_eq("📌"))
|
||||
.map(|r| r.count)
|
||||
.unwrap_or_default();
|
||||
println!("Pin count: {}", count);
|
||||
if count >= 1 {
|
||||
msg.pin(&c).await?;
|
||||
} else if count < 3 {
|
||||
msg.unpin(&c).await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
@ -70,7 +77,9 @@ async fn handle_react(c: Context, r: Reaction) -> Result<(), serenity::Error> {
|
|||
#[tokio::main]
|
||||
async fn main() {
|
||||
let token = std::env::var("DISCORD_TOKEN").expect("Where discord token?");
|
||||
let intents = GatewayIntents::GUILD_MESSAGE_REACTIONS | GatewayIntents::GUILD_MESSAGES;
|
||||
let intents = GatewayIntents::GUILD_MESSAGE_REACTIONS
|
||||
| GatewayIntents::GUILD_MESSAGES
|
||||
| GatewayIntents::GUILD_MESSAGE_REACTIONS;
|
||||
|
||||
let mut client = Client::builder(token, intents)
|
||||
.event_handler(Handler)
|
||||
|
|
Loading…
Reference in a new issue