mirror of
https://github.com/dsec-hub/dsec-discord-bot.git
synced 2026-09-22 07:44:26 +00:00
fix variables, added events
This commit is contained in:
parent
54ba36ee1f
commit
163be0c990
6 changed files with 100 additions and 40 deletions
|
|
@ -1,4 +1,10 @@
|
||||||
DISCORD_TOKEN=""
|
DISCORD_TOKEN=""
|
||||||
|
SUPABASE_URL=""
|
||||||
|
SUPABASE_KEY=""
|
||||||
|
|
||||||
|
# Optional
|
||||||
|
VERIFIED_ROLE_ID=""
|
||||||
|
GUILD_ID=""
|
||||||
WEATHER_TOKEN=""
|
WEATHER_TOKEN=""
|
||||||
|
|
||||||
# Weather API token from: https://www.weatherapi.com/
|
# Weather API token from: https://www.weatherapi.com/
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
use crate::{Data, Error};
|
use crate::{ApplicationContext, Error};
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use poise::{CreateReply, Modal};
|
use poise::{CreateReply, Modal};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serenity::all::{CreateEmbed, CreateEmbedFooter, GuildId, RoleId};
|
use serenity::all::{
|
||||||
|
CreateActionRow, CreateButton, CreateEmbed, CreateEmbedFooter, GuildId, RoleId,
|
||||||
type ApplicationContext<'a> = poise::ApplicationContext<'a, Data, Error>;
|
};
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
struct StudentRow {
|
struct StudentRow {
|
||||||
|
|
@ -24,47 +24,48 @@ struct VerificationModal {
|
||||||
student_id: String,
|
student_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[poise::command(slash_command, subcommands("embed", "myself"))]
|
#[poise::command(slash_command, subcommands("embed", "myself"))]
|
||||||
// pub async fn verify(_: ApplicationContext<'_>) -> Result<(), Error> {
|
pub async fn verify(_: ApplicationContext<'_>) -> Result<(), Error> {
|
||||||
// Ok(())
|
Ok(())
|
||||||
// }
|
}
|
||||||
|
|
||||||
/// Verify your DSEC club membership to obtain role
|
/// Verify your DSEC club membership to obtain role
|
||||||
#[poise::command(slash_command)]
|
#[poise::command(slash_command)]
|
||||||
pub async fn verify(ctx: ApplicationContext<'_>) -> Result<(), Error> {
|
pub async fn myself(ctx: ApplicationContext<'_>) -> Result<(), Error> {
|
||||||
verify_member(ctx).await?;
|
verify_member(ctx).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[poise::command(slash_command)]
|
#[poise::command(slash_command)]
|
||||||
// pub async fn verify(ctx: ApplicationContext<'_>) -> Result<(), Error> {
|
pub async fn embed(ctx: ApplicationContext<'_>) -> Result<(), Error> {
|
||||||
// let reply: CreateReply = {
|
let reply: CreateReply = {
|
||||||
// let embed: CreateEmbed = CreateEmbed::new()
|
let embed: CreateEmbed = CreateEmbed::new()
|
||||||
// .title("Verify your DSEC membership")
|
.title("Verify your DSEC membership")
|
||||||
// .description("Click **Verify Here** and enter your **Full name** and **Student ID** (e.g., s123456789). Your responses are private.");
|
.description("Click **Verify Here** and enter your **Full name** and **Student ID** (e.g., s123456789). Your responses are private.");
|
||||||
|
|
||||||
// let button: CreateButton = CreateButton::new("verify").label("Verify Here");
|
let button: CreateButton = CreateButton::new("verify").label("Verify Here");
|
||||||
|
|
||||||
// let components = vec![CreateActionRow::Buttons(vec![button])];
|
let components = vec![CreateActionRow::Buttons(vec![button])];
|
||||||
|
|
||||||
// CreateReply::default()
|
CreateReply::default()
|
||||||
// .ephemeral(false)
|
.ephemeral(false)
|
||||||
// .embed(embed)
|
.embed(embed)
|
||||||
// .components(components)
|
.components(components)
|
||||||
// };
|
};
|
||||||
|
|
||||||
// ctx.send(reply).await?;
|
ctx.send(reply).await?;
|
||||||
|
|
||||||
// while let Some(_) = ComponentInteractionCollector::new(ctx.serenity_context())
|
// let mut stream = ComponentInteractionCollector::new(ctx.serenity_context())
|
||||||
// .filter(move |mci| mci.data.custom_id == "verify")
|
// .filter(move |mci| mci.data.custom_id == "verify")
|
||||||
// .await
|
// .stream();
|
||||||
// {
|
|
||||||
|
// while let Some(_) = stream.next().await {
|
||||||
// verify_member(ctx).await?;
|
// verify_member(ctx).await?;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Ok(())
|
Ok(())
|
||||||
// }
|
}
|
||||||
|
|
||||||
// flow:
|
// flow:
|
||||||
// 1. check correct discord server -> layer 1
|
// 1. check correct discord server -> layer 1
|
||||||
|
|
|
||||||
2
src/events.rs
Normal file
2
src/events.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod ready;
|
||||||
|
pub mod interaction_create;
|
||||||
26
src/events/interaction_create.rs
Normal file
26
src/events/interaction_create.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
use crate::Error;
|
||||||
|
use poise::serenity_prelude as serenity;
|
||||||
|
|
||||||
|
#[derive(Debug, poise::Modal)]
|
||||||
|
#[allow(dead_code)] // fields only used for Debug print
|
||||||
|
struct MyModal {
|
||||||
|
first_input: String,
|
||||||
|
second_input: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn on_interaction_create(
|
||||||
|
_ctx: &serenity::Context,
|
||||||
|
interaction: &serenity::Interaction,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
let some_message_component = interaction.as_message_component();
|
||||||
|
|
||||||
|
if !some_message_component.is_none() {
|
||||||
|
let component = some_message_component.unwrap();
|
||||||
|
|
||||||
|
if component.data.custom_id == "verify" {
|
||||||
|
println!("Component data: {:?}", component);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
10
src/events/ready.rs
Normal file
10
src/events/ready.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
use crate::Error;
|
||||||
|
use poise::serenity_prelude as serenity;
|
||||||
|
|
||||||
|
pub async fn on_ready(
|
||||||
|
_ctx: &serenity::Context,
|
||||||
|
data_about_bot: &serenity::Ready,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
println!("Logged in as {}", data_about_bot.user.name);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
33
src/main.rs
33
src/main.rs
|
|
@ -4,8 +4,9 @@ use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
use supabase::prelude::*;
|
use supabase::Client;
|
||||||
mod commands;
|
mod commands;
|
||||||
|
mod events;
|
||||||
|
|
||||||
pub struct Data {
|
pub struct Data {
|
||||||
pub state: AppState,
|
pub state: AppState,
|
||||||
|
|
@ -14,6 +15,7 @@ pub struct Data {
|
||||||
// Types used by all command functions
|
// Types used by all command functions
|
||||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||||
type Context<'a> = poise::Context<'a, Data, Error>;
|
type Context<'a> = poise::Context<'a, Data, Error>;
|
||||||
|
type ApplicationContext<'a> = poise::ApplicationContext<'a, Data, Error>;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
|
|
@ -22,7 +24,7 @@ pub struct AppState {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
pub fn new() -> Result<Self> {
|
pub fn new() -> supabase::Result<Self> {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
let supabase_url = std::env::var("SUPABASE_URL").expect("missing SUPABASE_URL");
|
let supabase_url = std::env::var("SUPABASE_URL").expect("missing SUPABASE_URL");
|
||||||
let supabase_key = std::env::var("SUPABASE_KEY").expect("missing SUPABASE_KEY");
|
let supabase_key = std::env::var("SUPABASE_KEY").expect("missing SUPABASE_KEY");
|
||||||
|
|
@ -35,6 +37,24 @@ impl AppState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn event_handler(
|
||||||
|
ctx: &serenity::Context,
|
||||||
|
event: &serenity::FullEvent,
|
||||||
|
_framework: poise::FrameworkContext<'_, Data, Error>,
|
||||||
|
_data: &Data,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
match event {
|
||||||
|
serenity::FullEvent::Ready { data_about_bot, .. } => {
|
||||||
|
events::ready::on_ready(ctx, data_about_bot).await?;
|
||||||
|
}
|
||||||
|
serenity::FullEvent::InteractionCreate { interaction } => {
|
||||||
|
events::interaction_create::on_interaction_create(ctx, interaction).await?;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
dotenv().ok(); // load env
|
dotenv().ok(); // load env
|
||||||
|
|
@ -57,19 +77,14 @@ async fn main() {
|
||||||
commands::verification::verify(),
|
commands::verification::verify(),
|
||||||
commands::mods_only::embed(),
|
commands::mods_only::embed(),
|
||||||
],
|
],
|
||||||
event_handler: |_ctx, _event, _framework, _data| {
|
event_handler: |ctx, event, framework, data| {
|
||||||
Box::pin(async move {
|
Box::pin(event_handler(ctx, event, framework, data))
|
||||||
// println!("Got an event in event handler: {:?}", event);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.setup(|ctx, _ready, framework| {
|
.setup(|ctx, _ready, framework| {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
println!("Logged in as {}", _ready.user.name);
|
|
||||||
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
||||||
Ok(Data { state: app_state })
|
Ok(Data { state: app_state })
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue