Skip to content

Commit

Permalink
Merge pull request #121 from ItsGamerik/done-messages
Browse files Browse the repository at this point in the history
fix-minor-issues
  • Loading branch information
ItsGamerik authored Feb 27, 2024
2 parents ef6b8ed + 2bf9554 commit 370a853
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/target
/download
/.vscode
/.watchers
/**watchers
/.idea
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "get-img"
version = "0.4.0-hotfix"
version = "0.4.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ $ docker run -it -e "DISCORD_TOKEN=<your_token_here>" -e "GUILD_ID=<your_gid_her
9. Now paste the link into your browser and add the bot to your discord server. If everything worked, the bot should now be on your discord server!


*required permissions are: `Send Messages, Send Messages in Threads, Embed Links, Attach Files, Read Message History, Use Slash Commands`
*required permissions are: `bot, applications.commands, Read Messages/View Channels, Send Messages, Attach Files, Read Message History`

## Additional useful links

Expand Down
17 changes: 11 additions & 6 deletions src/commands/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ pub async fn run(ctx: Context, interaction: &CommandInteraction, options: &[Reso

// this is still way too convoluted

if let Ok(meta) = fs::metadata(path2.to_string() + "/output.txt").await {
if let Ok(meta) = fs::metadata(path2.to_string() + "output.txt").await {
if meta.is_file() {
if *option_bool {
// case if dltodisk is true
let output_file = File::open(path2.to_string() + "/output.txt").await.unwrap();
info!("started to download attachments/uploading output.txt...");

let output_file = File::open(path2.to_string() + "output.txt").await.unwrap();
let attachment = CreateAttachment::file(&output_file, "output.txt")
.await
.unwrap();
read_file().await;

info!("started to download attachments");
interaction
.create_followup(
&ctx.http,
Expand All @@ -60,13 +61,15 @@ pub async fn run(ctx: Context, interaction: &CommandInteraction, options: &[Reso
)
.await
.unwrap();
info!("done downloading attachments/uploading output.txt");
ctx.set_presence(
Some(ActivityData::watching("Ready to go :D")),
OnlineStatus::Online,
);
} else {
// if it is false
let output_file = File::open(path2.to_string() + "/output.txt").await.unwrap();
info!("starting to upload output.txt...");
let output_file = File::open(path2.to_string() + "output.txt").await.unwrap();
let attachment = CreateAttachment::file(&output_file, "output.txt")
.await
.unwrap();
Expand All @@ -80,13 +83,15 @@ pub async fn run(ctx: Context, interaction: &CommandInteraction, options: &[Reso
)
.await
.unwrap();
info!("Done uploading output.txt");
ctx.set_presence(
Some(ActivityData::watching("Ready to go :D")),
OnlineStatus::Online,
);
}
}
} else {
error!("tried to download messages, but found no index!");
followup_status_message(
&ctx,
"not indexed yet. Try using `/index` to index first.",
Expand All @@ -100,7 +105,7 @@ async fn read_file() {
let lock = CONFIG.lock().await;
let cfg = lock.get().unwrap();
let path = &cfg.directories.downloads;
let file = match File::open(path.to_string() + "/output.txt").await {
let file = match File::open(path.to_string() + "output.txt").await {
Ok(f) => f,
Err(e) => {
error!("error reading output.txt: {e}");
Expand Down Expand Up @@ -149,7 +154,7 @@ pub async fn download_file(url: String) {

let cleansed_file_name = re.replace(&file_name, "").to_string();

let root_path = path.to_string() + "/attachments/";
let root_path = path.to_string() + "attachments/";
if fs::metadata(&root_path).await.is_err() {
match fs::create_dir_all(&root_path).await {
Ok(_) => info!("created attachment download dir, as it did not exist"),
Expand Down
10 changes: 6 additions & 4 deletions src/commands/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ pub async fn run(ctx: Context, interaction: &CommandInteraction, options: &[Reso
..
}) = options.first()
{
info!(
"trying to parse channel: {}",
channel.name.as_ref().unwrap()
);
info!("indexing channel: {}", channel.name.as_ref().unwrap());
target_channel = channel;
target_confirmed_message = format!(
"added messages from channel **{}** to index",
Expand All @@ -38,6 +35,11 @@ pub async fn run(ctx: Context, interaction: &CommandInteraction, options: &[Reso

index(&ctx, target_channel).await;

info!(
"successfully added \"{}\" to index",
target_channel.name.as_ref().unwrap()
);

edit_status_message(&ctx, &target_confirmed_message, interaction).await;
}

Expand Down
10 changes: 7 additions & 3 deletions src/commands/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ pub async fn run(ctx: Context, interaction: &CommandInteraction, options: &[Reso
let path = &cfg.directories.watchfile;

let mut watch_track_file = match OpenOptions::new()
.write(true)
.create(true)
.append(true)
.open(path.to_string() + ".watchers")
Expand Down Expand Up @@ -97,7 +96,7 @@ pub async fn run(ctx: Context, interaction: &CommandInteraction, options: &[Reso
};
// note: using format! is apparently slower than using .as_str etc.
if json.id == option_channel.id {
delete_line_from_file(&format!("{path}/.watchers"), option_channel.id)
delete_line_from_file(&format!("{path}.watchers"), option_channel.id)
.await
.unwrap();
}
Expand Down Expand Up @@ -131,7 +130,12 @@ pub async fn run(ctx: Context, interaction: &CommandInteraction, options: &[Reso
while let Some(line) = lines.next_line().await.unwrap() {
let json: WatcherEntry = serde_json::from_str(&line).unwrap();
if json.id == option_channel.id {
if let Err(e) = delete_line_from_file("./.watchers", option_channel.id).await {
if let Err(e) = delete_line_from_file(
(path.to_string() + ".watchers").as_str(),
option_channel.id,
)
.await
{
error!("an error occurred writing to file: {e}")
}
}
Expand Down
19 changes: 6 additions & 13 deletions src/config/config_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::config::config_struct::Config;
use lazy_static::lazy_static;
use std::cell::OnceCell;
use std::error::Error;
use std::ops::Add;
use tokio::sync::Mutex;

lazy_static! {
Expand All @@ -12,21 +13,13 @@ pub async fn read_config() -> Result<(), Box<dyn Error>> {
let config_file = std::fs::read_to_string("./config.toml")?;
let mut config: Config = toml::from_str(&config_file)?;

// trim trailing slashes
if config.directories.downloads.ends_with('/') {
config.directories.downloads = config
.directories
.downloads
.trim_end_matches('/')
.to_string();
// add trailing slashes
if !config.directories.downloads.ends_with('/') {
config.directories.downloads = config.directories.downloads.add("/").to_string()
}

if config.directories.watchfile.ends_with('/') {
config.directories.watchfile = config
.directories
.watchfile
.trim_end_matches('/')
.to_string();
if !config.directories.watchfile.ends_with('/') {
config.directories.watchfile = config.directories.watchfile.add("/").to_string()
}

let config_cell = CONFIG.lock().await;
Expand Down
3 changes: 1 addition & 2 deletions src/helper_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ pub async fn universal_message_writer(message: Message) {
}

let mut file = match OpenOptions::new()
.write(true)
.create(true)
.append(true)
.open(path.to_string() + "/output.txt")
.open(path.to_string() + "output.txt")
{
Ok(file) => file,
Err(e) => {
Expand Down
31 changes: 26 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;
use std::{env, fs};

use log::{error, info, warn, LevelFilter};
use log::{error, info, LevelFilter};
use serenity::all::{GuildId, Interaction};
use serenity::async_trait;
use serenity::model::channel::Message;
Expand Down Expand Up @@ -29,13 +29,16 @@ impl EventHandler for Handler {

let path = &cfg.directories.watchfile;

let watcher_file = match File::open(path.to_string() + "/.watchers").await {
let watcher_file = match File::open(path.to_string() + ".watchers").await {
Ok(f) => f,
Err(_) => {
Err(e) => {
error!("watchfile not found: {e}");
return;
}
};

drop(lock);

let mut lines = tokio::io::BufReader::new(watcher_file).lines();
while let Some(line) = lines.next_line().await.unwrap() {
let json: WatcherEntry = match serde_json::from_str(&line) {
Expand Down Expand Up @@ -105,7 +108,7 @@ impl EventHandler for Handler {
commands::download::run(ctx, &command, &command.data.options()).await;
Some(())
}
_ => Some(warn!(
_ => Some(error!(
"command not implemented: {}",
format!("{}", command.data.name)
)),
Expand All @@ -129,6 +132,24 @@ async fn main() {
} else {
info!("successfully read config file!")
};

let lock = CONFIG.lock().await;
let cfg = lock.get().unwrap();
let path = &cfg.directories.watchfile;

match fs::OpenOptions::new()
.write(true)
.create(true)
.open(path.to_string() + ".watchers")
{
Ok(_) => (),
Err(e) => {
error!("could not create watchfile: {e}");
}
}

drop(lock);

let token: String = match env::var("DISCORD_TOKEN") {
Ok(token) => {
info!("token found in env!");
Expand Down

0 comments on commit 370a853

Please sign in to comment.