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