Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add components & embeds #9

Merged
merged 8 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[package]
name = "surreal_bot"
version = "0.2.3"
version = "0.3.0"
edition = "2021"
authors = ["Raphael Darley <[email protected]>"]
authors = ["Raphael Darley <[email protected]>", "alyti <[email protected]>"]

[dependencies]
anyhow = "1.0.71"
dotenv = "0.15.0"
humantime = "2.1.0"
memorable-wordlist = "0.1.7"
once_cell = "1.18.0"
serde = "1.0.166"
Expand All @@ -21,7 +22,7 @@ surrealdb = { git = "https://github.com/surrealdb/surrealdb.git", features = [
"kv-mem",
"kv-rocksdb",
] }
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
tokio = { version = "1.29.1", features = ["macros", "signal", "rt-multi-thread"] }
tracing = "0.1"
tracing-futures = "0.2.5"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
15 changes: 12 additions & 3 deletions src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,21 @@ pub async fn run(
let channel = match command.channel_id.to_channel(&ctx).await.unwrap() {
Channel::Guild(c) => c,
_ => {
interaction_reply_ephemeral(command, ctx, ":warning: This command only works in guild channels")
.await?;
interaction_reply_ephemeral(
command,
ctx,
":warning: This command only works in guild channels",
)
.await?;
return Ok(());
}
};
interaction_reply_ephemeral(command, ctx.clone(), ":white_check_mark: This channel should now be cleaned").await?;
interaction_reply_ephemeral(
command,
ctx.clone(),
":white_check_mark: This channel should now be cleaned",
)
.await?;

clean_channel(channel, &ctx).await;

Expand Down
21 changes: 16 additions & 5 deletions src/commands/clean_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,30 @@ pub async fn run(
) -> Result<(), anyhow::Error> {
let map = (*DBCONNS.lock().await).clone();
for id in map.keys() {
let channel = match ChannelId(id.clone()).to_channel(&ctx).await.unwrap() {
let channel = match ChannelId(*id).to_channel(&ctx).await.unwrap() {
Channel::Guild(c) => c,
_ => {
interaction_reply_ephemeral(command, ctx, ":warning: This command only works in guild channels")
.await?;
interaction_reply_ephemeral(
command,
ctx,
":warning: This command only works in guild channels",
)
.await?;
return Ok(());
}
};
let (channel, ctx) = (channel.clone(), ctx.clone());
tokio::spawn(async move { clean_channel(channel, &ctx).await }.instrument(tracing::Span::current()));
tokio::spawn(
async move { clean_channel(channel, &ctx).await }.instrument(tracing::Span::current()),
);
}

interaction_reply_ephemeral(command, ctx, ":white_check_mark: All channels should now be cleaned").await
interaction_reply_ephemeral(
command,
ctx,
":white_check_mark: All channels should now be cleaned",
)
.await
}

pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
Expand Down
23 changes: 12 additions & 11 deletions src/commands/config_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ use serenity::model::Permissions;
use serenity::builder::CreateApplicationCommand;
use serenity::prelude::*;

use crate::components::configurable_server::show;
use crate::config;
use crate::config::Config;
use crate::config::ConfigBuilder;
use crate::utils::interaction_reply;
use crate::DB;
use crate::utils::interaction_reply_ephemeral;

pub async fn run(
command: &ApplicationCommandInteraction,
ctx: Context,
) -> Result<(), anyhow::Error> {
debug!(command_options = ?command.data.options, "command options");

let result: Result<Option<Config>, surrealdb::Error> = DB
.select(("guild_config", command.guild_id.unwrap().to_string()))
.await;
Expand All @@ -24,7 +24,7 @@ pub async fn run(
Ok(response) => match response {
Some(c) => {c}

None => return interaction_reply(command, ctx.clone(), format!(":warning: This server is not yet configured, use `/configure` to add initial configuration")).await,
None => return interaction_reply(command, ctx.clone(), ":warning: This server is not yet configured, use `/configure` to add initial configuration".to_string()).await,
},
Err(e) => return interaction_reply(command, ctx.clone(), format!("Database error: {}", e)).await,
};
Expand All @@ -41,23 +41,24 @@ pub async fn run(
.content(config)
.await;

let msg = match updated {
match updated {
Ok(response) => match response {
Some(c) => {
format!(":white_check_mark: This server is now configured with: {:?}", c)
show(&ctx, &command.channel_id, &c).await?;
interaction_reply_ephemeral(command, ctx, ":white_check_mark: Configuration updated successfully".to_string()).await
}

None => {
warn!("error updating configuration");
":x: Error updating configuration".to_string()
},
interaction_reply_ephemeral(command, ctx.clone(), ":x: Error updating configuration".to_string()).await
}
},
Err(e) => {
error!(error = %e, "database error");
format!(":x: Database error: {}", e)
},
};
interaction_reply(command, ctx.clone(), msg).await
interaction_reply_ephemeral(command, ctx.clone(), format!(":x: Database error: {e}")).await
}
}
}

pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
Expand Down
18 changes: 9 additions & 9 deletions src/commands/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serenity::model::Permissions;
use serenity::builder::CreateApplicationCommand;
use serenity::prelude::*;

use crate::components::configurable_server::show;
use crate::config;
use crate::config::Config;
use crate::config::ConfigBuilder;
Expand Down Expand Up @@ -49,23 +50,22 @@ pub async fn run(
.content(config)
.await;

let msg = match created {
match created {
Ok(response) => match response {
Some(c) => {
format!(":information_source: This server is now configured with: {:?}", c)
show(&ctx, &command.channel_id, &c).await?;
interaction_reply_ephemeral(command, ctx, ":white_check_mark: Configuration added successfully".to_string()).await
}

None => {
warn!("Error adding configuration");
":x: Error adding configuration".to_string()
},
interaction_reply_ephemeral(command, ctx.clone(), ":x: Error adding configuration".to_string()).await
}
},
Err(e) => {
error!(error = %e, "database error");
format!(":x: Database error: {}", e)
},
};
interaction_reply(command, ctx.clone(), msg).await
interaction_reply_ephemeral(command, ctx.clone(), format!(":x: Database error: {}", e)).await
}
}
}

pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
Expand Down
37 changes: 29 additions & 8 deletions src/commands/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use surrealdb::engine::local::Db;
use surrealdb::Surreal;
use tracing::Instrument;

use crate::components::configurable_session::show;
use crate::{premade, utils::*, DBCONNS};

use crate::config::Config;
Expand Down Expand Up @@ -81,6 +82,7 @@ pub async fn run(
"surreal_deal_mini.surql",
"Surreal deal (mini)",
Some("surreal_deal.png"),
&config,
)
.await?;
}
Expand All @@ -93,6 +95,7 @@ pub async fn run(
"surreal_deal.surql",
"Surreal deal",
Some("surreal_deal.png"),
&config,
)
.await?;
}
Expand All @@ -107,25 +110,41 @@ pub async fn run(
}
}
}
CommandOptionType::Attachment => load_attachment(op_option, command, ctx, db, channel).await?,
_ => {
interaction_reply_ephemeral(command, ctx, ":x: Unsupported option type")
CommandOptionType::Attachment => {
show(&ctx, &channel, crate::ConnType::ConnectedChannel, &config)
.await?;
load_attachment(op_option, command, ctx, db, channel).await?
}
_ => {
interaction_reply_ephemeral(
command,
ctx,
":x: Unsupported option type",
)
.await?;
return Ok(());
}
}
}
Ordering::Less => interaction_reply(command, ctx, format!(":information_source: This channel is now connected to a SurrealDB instance, try writing some SurrealQL with the `/query` command! \n_Please note this channel will expire after {:#?} of inactivity._", config.ttl)).await?,
Ordering::Less => {
show(&ctx, &channel, crate::ConnType::ConnectedChannel, &config).await?;
interaction_reply_ephemeral(
command,
ctx,
":information_source: Your session is now available!",
)
.await?
}
};
return Ok(());
Ok(())
}
None => {
return interaction_reply(
interaction_reply(
command,
ctx,
":warning: Direct messages are not currently supported".to_string(),
)
.await;
.await
}
}
}
Expand All @@ -152,8 +171,10 @@ async fn load_premade(
file_name: &'static str,
name: &'static str,
schema_name: Option<&'static str>,
config: &Config,
) -> Result<(), anyhow::Error> {
{
show(&ctx, &channel, crate::ConnType::ConnectedChannel, &config).await?;
interaction_reply(
command,
ctx.clone(),
Expand Down Expand Up @@ -182,7 +203,7 @@ async fn load_premade(
channel
.send_files(
ctx,
[AttachmentType::Path(&Path::new(&format!(
[AttachmentType::Path(Path::new(&format!(
"premade/{}",
scheme_file_name
)))],
Expand Down
Loading