Skip to content

Commit

Permalink
0.1.7
Browse files Browse the repository at this point in the history
multi-discord-channel support and much more. chech out /help and RECENT_CHANGES.md
  • Loading branch information
Vernoxvernax committed Jan 15, 2023
1 parent f8cefd1 commit ffc3269
Show file tree
Hide file tree
Showing 14 changed files with 730 additions and 212 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nyaa-notifs"
version = "0.1.6"
version = "0.1.7"
edition = "2021"

[dependencies]
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ ___
* If the input url contains search patterns (aside from "newest"), the script will download all pages to find a new release. This can get your ip **banned** if you input the wrong url. (`complete_result = false`: limits everything to the first page)

#### Config notes:
* To add multiple discord-notification channels, you can just continue the `channel_id` list.
* On the first run, I'd highly suggest you to keep all notification services deactivated, so you don't get spammed with outdated news.
* Discord channels can be configured seperately through the slash command framework
* On the first run, I'd highly suggest you to keep gotify&smtp notification services deactivated, so you don't get spammed with outdated news.

#### Misc:
* All web-requests are executed two seconds from each other.
* All web-requests are executed two seconds from each other. (hopefully)

___

Expand Down
13 changes: 7 additions & 6 deletions RECENT_CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* Attempt to fix multiple threads spawning when the discord bot reconnects
* Discord: If comments are too long, the message will be split and sent in different parts
* Discord: Removed "A new comment!" message for less bloat
* Discord: New releases messages now feature the actual uploaders gravatar instead of nyaa's default one.
* Comments in general: Markdown image embeds are now being stripped to only the url. f.e. `![](http://images.com/i.jpg)` -> `http://images.com/i.jpg`
* Cleaned up code a little bit
* Finally fixed the running loop when the bot gets reconnected
* Multiple discord server support
* Check out `/help` to for setup.
* Make sure the bot has permissions to read/write messages
* Only administrators have permissions to access the commands

##### slash commands are easy, no need for poise ha
8 changes: 8 additions & 0 deletions migrations/20230114135944_front.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Add migration script here
CREATE TABLE FRONT (
activated TEXT NO NULL,
comments TEXT NO NULL,
releases TEXT NO NULL,
channel_id INTEGER,
urls TEXT NO NULL
)
72 changes: 72 additions & 0 deletions sqlx-data.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,67 @@
{
"db": "SQLite",
"081dfdf28ab067034cbc80fe8105fca51124d1db9446282c7e8e426c241bdd1f": {
"describe": {
"columns": [
{
"name": "activated",
"ordinal": 0,
"type_info": "Text"
},
{
"name": "comments",
"ordinal": 1,
"type_info": "Text"
},
{
"name": "releases",
"ordinal": 2,
"type_info": "Text"
},
{
"name": "channel_id",
"ordinal": 3,
"type_info": "Int64"
},
{
"name": "urls",
"ordinal": 4,
"type_info": "Text"
}
],
"nullable": [
true,
true,
true,
true,
true
],
"parameters": {
"Right": 0
}
},
"query": "SELECT * FROM FRONT"
},
"0c63cfa40737ab6696c659cdf6c672d7a0ae004f6b6642bc094293525d4aa2d6": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 1
}
},
"query": "DELETE FROM FRONT WHERE channel_id=?"
},
"0e3d95114cb3809601f7c6716850c4081db1cd455b4ab117e25129c32249c607": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 5
}
},
"query": "INSERT INTO FRONT (activated, releases, comments, channel_id, urls) VALUES(?1, ?2, ?3, ?4, ?5)"
},
"180823bc2240d852547538c642379a36e09e8d7094a1d70a5b8613d3bb9d0dbd": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -85,5 +147,15 @@
}
},
"query": "SELECT * FROM Main"
},
"3ded2174058a711f06e601ab1a29d921ebcdf6478895487ea4dffc8e60115d39": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 2
}
},
"query": "UPDATE FRONT SET activated=? WHERE channel_id=?"
}
}
25 changes: 25 additions & 0 deletions src/commands/activity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use serenity::model::prelude::command::CommandOptionType;
use serenity::{builder::CreateApplicationCommand, prelude::Context};
use serenity::model::Permissions;
use serenity::model::prelude::Activity;
use serenity::model::prelude::interaction::application_command::CommandDataOption;

pub async fn run(options: &[CommandDataOption], ctx: &Context) -> String {
let act = &options.get(0).unwrap().value.as_ref().unwrap().as_str().unwrap();
ctx.set_activity(Activity::listening(act)).await;
"Activity changed.".to_string()
}

pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name("activity").description("Change the activity status of the bot.")
.default_member_permissions(Permissions::ADMINISTRATOR)
.create_option(|option| {
option
.name("listens to")
.description("listening to what")
.kind(CommandOptionType::String)
.min_length(2)
.required(true)
})
}
56 changes: 56 additions & 0 deletions src/commands/create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use serenity::builder::CreateApplicationCommand;
use serenity::model::Permissions;
use serenity::model::prelude::ChannelId;
use serenity::model::prelude::interaction::application_command::CommandDataOption;
use serenity::model::prelude::command::CommandOptionType;

use crate::database::{check_for_channel_id, add_discord_channel};
use crate::DiscordChannel;

pub async fn run(options: &[CommandDataOption], channel_id: ChannelId) -> String {
let url_input = &options.get(0).unwrap().value.as_ref().unwrap().as_str().unwrap();
let releases = &options.get(1).unwrap().value.as_ref().unwrap().as_bool().unwrap();
let comments = &options.get(2).unwrap().value.as_ref().unwrap().as_bool().unwrap();
let channel_id = channel_id.0 as i64;
if ! check_for_channel_id(channel_id).await.unwrap().is_empty()
{
return "This discord channel has already been configured, please make sure to `/reset` it before adding new settings.".to_string();
}
let urls: Vec<String> = url_input.split(", ").map(|str| str.to_string()).collect();
add_discord_channel(DiscordChannel {
activated: true,
releases: *releases,
comments: *comments,
channel_id,
urls: urls.clone()
}).await.unwrap();
println!("{:?} configured with {:?} | {} {}", channel_id, urls, releases, comments);
"Channel successfully configured.".to_string()
}

pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name("create").description("Setup notifications for the current channel")
.create_option(|option| {
option
.name("url")
.description("List of nyaa url's")
.kind(CommandOptionType::String)
.min_length(15)
.required(true)
})
.create_option(|option| {
option
.name("releases")
.description("Notifications for releases")
.kind(CommandOptionType::Boolean)
.required(true)
})
.create_option(|option| {
option
.name("comments")
.description("Notifications for comments")
.kind(CommandOptionType::Boolean)
.required(true)
}).default_member_permissions(Permissions::ADMINISTRATOR)
}
20 changes: 20 additions & 0 deletions src/commands/help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use serenity::builder::CreateApplicationCommand;
use serenity::model::Permissions;
use serenity::model::prelude::interaction::application_command::CommandDataOption;

pub fn run(_options: &[CommandDataOption]) -> String {
"```\
[Nyaa-Notifications]
Commands:
\"help\" - Print this help message
\"create\" - Setup notifications for the current channel
\"reset\" - Remove notifications for the current channel
\"pause\" - Pause all notifications for this channel
\"resume\" - Resume all notifications for this channel```".to_string()
}

pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command.name("help").description("Small description of available commands")
.default_member_permissions(Permissions::ADMINISTRATOR)
}
5 changes: 5 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod help;
pub mod create;
pub mod reset;
pub mod pause_unpause;
pub mod activity;
47 changes: 47 additions & 0 deletions src/commands/pause_unpause.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use serenity::{builder::CreateApplicationCommand, model::prelude::command::CommandOptionType};
use serenity::model::Permissions;
use serenity::model::prelude::ChannelId;
use serenity::model::prelude::interaction::application_command::CommandDataOption;

use crate::database::{check_for_channel_id, update_discord_bot};

pub async fn run(options: &[CommandDataOption], channel_id: ChannelId) -> String {
let channel_id = channel_id.0 as i64;
let input = &options.get(0).unwrap().value.as_ref().unwrap().as_bool().unwrap();
let check = check_for_channel_id(channel_id).await.unwrap();
if check.is_empty()
{
return "This discord channel has not been configured yet. Type `/create` to set it up.".to_string();
}
else if check.get(0).unwrap().activated && ! *input
{
return "This discord channel is not paused to begin with.\nYou might want to check out `/pause True`.".to_string();
}
else if ! check.get(0).unwrap().activated && *input
{
return "This discord channel is already paused.\nYou might want to check out `/pause False`.".to_string();
}
if *input
{
update_discord_bot(channel_id, true, false).await.unwrap();
}
else
{
update_discord_bot(channel_id, false, false).await.unwrap();
}
println!("Notifications now {:?} for {:?}", input, channel_id);
"Channel configuration successfully edited.".to_string()
}

pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name("pause").description("Change the notification-state for the current channel")
.default_member_permissions(Permissions::ADMINISTRATOR)
.create_option(|option| {
option
.name("yesno")
.description("Pause notifications or resume them")
.kind(CommandOptionType::Boolean)
.required(true)
})
}
23 changes: 23 additions & 0 deletions src/commands/reset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use serenity::builder::CreateApplicationCommand;
use serenity::model::Permissions;
use serenity::model::prelude::ChannelId;
use serenity::model::prelude::interaction::application_command::CommandDataOption;

use crate::database::{check_for_channel_id, update_discord_bot};

pub async fn run(_options: &[CommandDataOption], channel_id: ChannelId) -> String {
let channel_id = channel_id.0 as i64;
if check_for_channel_id(channel_id).await.unwrap().is_empty()
{
return "This discord channel has not been configured. Type `/create` to set it up.".to_string();
}
update_discord_bot(channel_id, false, true).await.unwrap();
println!("Configuration removed for {:?}", channel_id);
"Channel configuration successfully removed.".to_string()
}

pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name("reset").description("Remove configurations for the current channel")
.default_member_permissions(Permissions::ADMINISTRATOR)
}
Loading

0 comments on commit ffc3269

Please sign in to comment.