Skip to content

Commit

Permalink
feat: Add initial components
Browse files Browse the repository at this point in the history
Also formats code & imports humantime for nicer tll.
  • Loading branch information
alyti committed Jul 29, 2023
1 parent 1d919fb commit 2a50c2d
Show file tree
Hide file tree
Showing 16 changed files with 272 additions and 112 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ authors = ["Raphael Darley <[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 Down
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
11 changes: 7 additions & 4 deletions src/commands/config_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -44,18 +44,21 @@ pub async fn run(
let msg = match updated {
Ok(response) => match response {
Some(c) => {
format!(":white_check_mark: This server is now configured with: {:?}", c)
format!(
":white_check_mark: This server is now configured with: {:?}",
c
)
}

None => {
warn!("error updating configuration");
":x: Error updating configuration".to_string()
},
}
},
Err(e) => {
error!(error = %e, "database error");
format!(":x: Database error: {}", e)
},
}
};
interaction_reply(command, ctx.clone(), msg).await
}
Expand Down
9 changes: 6 additions & 3 deletions src/commands/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,21 @@ pub async fn run(
let msg = match created {
Ok(response) => match response {
Some(c) => {
format!(":information_source: This server is now configured with: {:?}", c)
format!(
":information_source: This server is now configured with: {:?}",
c
)
}

None => {
warn!("Error adding configuration");
":x: Error adding configuration".to_string()
},
}
},
Err(e) => {
error!(error = %e, "database error");
format!(":x: Database error: {}", e)
},
}
};
interaction_reply(command, ctx.clone(), msg).await
}
Expand Down
8 changes: 4 additions & 4 deletions src/commands/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ pub async fn run(
}
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?,
};
return Ok(());
Ok(())
}
None => {
return interaction_reply(
interaction_reply(
command,
ctx,
":warning: Direct messages are not currently supported".to_string(),
)
.await;
.await
}
}
}
Expand Down Expand Up @@ -182,7 +182,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
20 changes: 12 additions & 8 deletions src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub async fn run(
.union(Permissions::READ_MESSAGE_HISTORY),
deny: Permissions::empty(),
kind: serenity::model::prelude::PermissionOverwriteType::Member(UserId(
command.application_id.as_u64().clone(),
*command.application_id.as_u64(),
)),
},
PermissionOverwrite {
Expand Down Expand Up @@ -102,7 +102,7 @@ pub async fn run(
channel
.send_files(
ctx,
[AttachmentType::Path(&Path::new(
[AttachmentType::Path(Path::new(
"premade/surreal_deal.png",
))],
|m| m.content("schema:"),
Expand All @@ -123,7 +123,7 @@ pub async fn run(
channel
.send_files(
ctx,
[AttachmentType::Path(&Path::new(
[AttachmentType::Path(Path::new(
"premade/surreal_deal.png",
))],
|m| m.content("schema:"),
Expand Down Expand Up @@ -200,8 +200,12 @@ pub async fn run(
}
}
_ => {
interaction_reply_ephemeral(command, ctx, ":x: Unsupported option type")
.await?;
interaction_reply_ephemeral(
command,
ctx,
":x: Unsupported option type",
)
.await?;
return Ok(());
}
}
Expand All @@ -221,15 +225,15 @@ pub async fn run(
false,
)
.await?;
return Ok(());
Ok(())
}
None => {
return interaction_reply(
interaction_reply(
command,
ctx,
":warning: Direct messages are not currently supported".to_string(),
)
.await;
.await
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/commands/create_db_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serenity::model::prelude::application_command::ApplicationCommandInteraction
use memorable_wordlist::kebab_case;
use serenity::prelude::Context;

use crate::components::show_configurable_session;
use crate::utils::*;

use crate::config::Config;
Expand Down Expand Up @@ -38,19 +39,20 @@ pub async fn run(

let db = create_db_instance(&config).await?;

channel.say(&ctx, format!(":information_source: This public thread is now connected to a SurrealDB instance. Try writing some SurrealQL! \nIf you want, you can use `/load` to load a premade dataset or your own SurrealQL from a file. \n_Please note this channel will expire after {:#?} of inactivity._", config.ttl)).await?;
// channel.say(&ctx, format!(":information_source: This public thread is now connected to a SurrealDB instance. Try writing some SurrealQL! \nIf you want, you can use `/load` to load a premade dataset or your own SurrealQL from a file. \n_Please note this channel will expire after {:#?} of inactivity._", config.ttl)).await?;
show_configurable_session(&ctx, &channel, crate::ConnType::Thread, config.ttl).await?;
interaction_reply_ephemeral(command, ctx.clone(), format!(":information_source: You now have your own database instance! Head over to <#{}> to start writing SurrealQL!", channel.id.as_u64())).await?;

register_db(ctx, db, channel, config, crate::ConnType::Thread, true).await?;
return Ok(());
Ok(())
}
None => {
return interaction_reply(
interaction_reply(
command,
ctx,
":warning: Direct messages are not currently supported".to_string(),
)
.await;
.await
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/commands/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ pub async fn run(
return Ok(());
}
};
interaction_reply(command, ctx.clone(), ":information_source: Exporting database").await?;
interaction_reply(
command,
ctx.clone(),
":information_source: Exporting database",
)
.await?;

let base_path = match env::var("TEMP_DIR_PATH") {
Ok(p) => p,
Expand Down Expand Up @@ -71,7 +76,8 @@ pub async fn run(
fs::remove_file(path).await?;
}
Err(why) => {
interaction_reply_edit(command, ctx, format!(":x: Database export failed: {why}")).await?
interaction_reply_edit(command, ctx, format!(":x: Database export failed: {why}"))
.await?
}
};
Ok(())
Expand Down
18 changes: 11 additions & 7 deletions src/commands/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub async fn run(
command: &ApplicationCommandInteraction,
ctx: Context,
) -> Result<(), anyhow::Error> {
if command.data.options.len() == 0 {
if command.data.options.is_empty() {
interaction_reply_ephemeral(
command,
ctx,
Expand Down Expand Up @@ -97,24 +97,28 @@ pub async fn run(
load_attachment(op_option, command, ctx, db, channel).await?
}
_ => {
interaction_reply_ephemeral(command, ctx, ":x: Unsupported option type")
.await?;
interaction_reply_ephemeral(
command,
ctx,
":x: Unsupported option type",
)
.await?;
return Ok(());
}
}
}
Ordering::Less => panic!(),
};

return Ok(());
Ok(())
}
None => {
return interaction_reply(
interaction_reply(
command,
ctx,
":warning: Direct messages are not currently supported".to_string(),
)
.await;
.await
}
}
}
Expand Down Expand Up @@ -171,7 +175,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

0 comments on commit 2a50c2d

Please sign in to comment.