-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
200 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use std::sync::Arc; | ||
|
||
use serenity::{ | ||
builder::CreateApplicationCommand, futures::StreamExt, model::prelude::application_command, | ||
prelude::Context, | ||
}; | ||
use tracing::Instrument; | ||
|
||
use crate::{ | ||
components::configurable_session::show, | ||
config::Config, | ||
utils::{ | ||
create_db_instance, ephemeral_interaction, ephemeral_interaction_edit, register_db, | ||
CmdError, | ||
}, | ||
DB, DBCONNS, | ||
}; | ||
|
||
pub async fn run( | ||
command: &application_command::ApplicationCommandInteraction, | ||
ctx: Context, | ||
) -> Result<(), anyhow::Error> { | ||
match command.guild_id { | ||
Some(guild_id) => { | ||
if let Some(_) = DBCONNS.lock().await.get_mut(command.channel_id.as_u64()) { | ||
CmdError::ExpectedNoSession.reply(&ctx, command).await | ||
} else { | ||
let result: Result<Option<Config>, surrealdb::Error> = | ||
DB.select(("guild_config", guild_id.to_string())).await; | ||
|
||
let config = match result { | ||
Ok(response) => match response { | ||
Some(c) => c, | ||
None => return CmdError::NoConfig.reply(&ctx, command).await, | ||
}, | ||
Err(e) => return CmdError::GetConfig(e).reply(&ctx, command).await, | ||
}; | ||
|
||
ephemeral_interaction( | ||
&ctx, | ||
command, | ||
"Looking for last export", | ||
"I will now check the last 20 messages for .surql attachments.", | ||
None, | ||
) | ||
.await?; | ||
|
||
let command = Arc::new(command.clone()); | ||
let channel = command.channel_id.to_channel(&ctx).await?.guild().unwrap(); | ||
tokio::spawn(async move { | ||
let mut messages = command.channel_id.messages_iter(&ctx).boxed(); | ||
let mut total = 0; | ||
let att = loop { | ||
total += 1; | ||
if total > 20 { | ||
break None; | ||
} | ||
if let Some(message_result) = messages.next().await { | ||
match message_result { | ||
Ok(message) => match message.attachments.first() { | ||
Some(att) => break Some(att.clone()), | ||
None => continue, | ||
}, | ||
Err(error) => { | ||
error!(error = %error, "Error getting message"); | ||
ephemeral_interaction_edit(&ctx, &command, "Failed to get message", format!("Couldn't load a message:\n```rust\n{error}\n```"), Some(false)).await.unwrap(); | ||
break None; | ||
} | ||
} | ||
} else { | ||
break None; | ||
} | ||
}; | ||
if let Some(att) = att { | ||
match create_db_instance(&config).await { | ||
Ok(db) => { | ||
match register_db( | ||
ctx.clone(), | ||
db.clone(), | ||
channel.clone(), | ||
config.clone(), | ||
crate::ConnType::ConnectedChannel, | ||
true, | ||
) | ||
.await { | ||
Ok(conn) => { | ||
ephemeral_interaction_edit(&ctx, &command, "Session loading!", "Bot has successfully created a new session, registered it with this channel and is now loading your export.", None).await.unwrap(); | ||
if let Err(err) = conn.import_from_attachment(&ctx, &command, &att).await { | ||
error!(error = %err, "Error importing from attachment") | ||
} | ||
show(&ctx, &channel, conn.conn_type, &config).await.unwrap(); | ||
}, | ||
Err(e) => { | ||
CmdError::RegisterDB(e).edit(&ctx, &command).await.unwrap(); | ||
} | ||
} | ||
}, | ||
Err(err) => { | ||
error!(error = %err, "Error creating DB instance"); | ||
CmdError::CreateDB(err).edit(&ctx, &command).await.unwrap(); | ||
}, | ||
} | ||
} else { | ||
ephemeral_interaction_edit(&ctx, &command, "No export found!", "Bot could not find any .surql attachments in the last 20 messages.", Some(false)).await.unwrap(); | ||
} | ||
}.in_current_span()); | ||
|
||
Ok(()) | ||
} | ||
} | ||
None => CmdError::NoGuild.reply(&ctx, command).await, | ||
} | ||
} | ||
|
||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { | ||
command | ||
.name("reconnect") | ||
.description("Recreates a SurrealDB instance using most recent export and associates it with the current channel") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters