added new config option, refactored other code

This commit is contained in:
2025-06-05 23:32:33 +02:00
parent 7922bc50a9
commit 71da587242
6 changed files with 61 additions and 44 deletions
+10 -10
View File
@@ -30,16 +30,16 @@ This program uses Rust (v1.82.0) and Python (v3.11.4), you can likely use other
You can install Python modules by running `$ pip install {module}` or `$ python -m pip install {module}` in a terminal.
### Environment variables:
| **Name** | **Description** |
| --- | --- |
| `ASSISTANT_TOKEN` | The Discord bot token. (Create a Discord bot [here](https://discord.com/developers/docs/intro)!) |
| **Name** | **Description** |
| ---------------------- | --- |
| `ASSISTANT_TOKEN` | The Discord bot token. (Create a Discord bot [here](https://discord.com/developers/docs/intro)!) |
| `ASSISTANT_TOKEN_TEST` | (Optional) A testing Discord bot token. This is only needed when the program is run with `-t` or `--test`. |
| `ASSISTANT_R_ID` | The id for the Reddit bot/account. (Create a Reddit bot [here](https://www.reddit.com/prefs/apps)!) |
| `ASSISTANT_R_TOKEN` | The token for the Reddit bot/account. |
| `ASSISTANT_R_NAME` | The username of the Reddit bot/account. |
| `ASSISTANT_R_PASS` | The password for the Reddit bot/account. |
| `ASSISTANT_OWNERS` | (OPTIONAL) A list of Discord user IDs that "own" the bot. Separate each ID with a single comma and **no** spaces. This will allow the specified user IDs to run root commands such as `/stop`, it will also DM these users when *certain* errors occur. |
| `ASSISTANT_BK_MODS` | (OPTIONAL) Same format as `ASSISTANT_OWNERS` but for people who are allowed to use the `/re_*` commands. |
| `ASSISTANT_R_ID` | The id for the Reddit bot/account. (Create a Reddit bot [here](https://www.reddit.com/prefs/apps)!) |
| `ASSISTANT_R_TOKEN` | The token for the Reddit bot/account. |
| `ASSISTANT_R_NAME` | The username of the Reddit bot/account. |
| `ASSISTANT_R_PASS` | The password for the Reddit bot/account. |
| `ASSISTANT_OWNERS` | (OPTIONAL) A list of Discord user IDs that "own" the bot. Separate each ID with a single comma and **no** spaces. This will allow the specified user IDs to run root commands such as `/stop`, it will also DM these users when *certain* errors occur. |
| `ASSISTANT_BK_MODS` | (OPTIONAL) Same format as `ASSISTANT_OWNERS` but for people who are allowed to use the `/re_*` commands. |
### Required permissions:
**These are automatically set if you use the [official invite link](https://discord.com/oauth2/authorize?client_id=1212127255795335208&permissions=84992&integration_type=0&scope=bot) or an invite link with the permissions integer set to `84992`.** (The permission integer is this part of the URL `&permissions=84992`)
@@ -59,7 +59,7 @@ You can install Python modules by running `$ pip install {module}` or `$ python
* For help, run `cargo run -- -h` or `cargo run -- --help`.
* To only run the Python part, use `cargo run -- --py`, or for a better error output, `python ./src/python/main.py`
### Long answer for absolute beginners:
### Long answer for beginners:
* Download the code (and extract it if needed).
* Open a terminal.
* Set the environment variables (listed above).
-2
View File
@@ -1,7 +1,5 @@
### High priority:
- [ ] Reddit bot that scrapes images with tag "Original Art" and posts them in Discord server
- [ ] Rename commands from "bk_week_{name}" to "re_{name}"
- [ ] Allow disabling bk_week
- [ ] Allow updating the data autonomously and via manual commands.
- [ ] Automatically approve posts that don't get caught by reverse image search (ris)
<!-- - [ ] 10-minute schedule for updating Discord channel (IMPOSSIBLE / REALLY FUCKING HARD) -->
+2
View File
@@ -1,5 +1,7 @@
{
"comment": "IMPORTANT!!! Anything labeled with 'RESTART_{cfg-name-here}' requires a restart to apply",
"reddit": {
"RESTART_enabled": true,
"subreddits": "bytedicetesting",
"fetch_limit": 100
}
+2 -7
View File
@@ -1,8 +1,7 @@
use poise::Command;
use std::{collections::HashMap, error::Error as StdErr};
use std::collections::HashMap;
use tokio::fs;
use crate::{lang, messages::send_msg, Context, Data, Error};
use crate::{lang, messages::send_msg, Context, Cmd, Error};
#[derive(poise::ChoiceParameter, PartialEq)]
@@ -15,10 +14,6 @@ enum HelpOptions {
}
type Cmd = Command<Data, Box<dyn StdErr + Send + Sync>>;
#[poise::command(
slash_command,
prefix_command,
+42 -23
View File
@@ -4,7 +4,7 @@ use poise::serenity_prelude::UserId;
use poise::serenity_prelude as serenity;
use poise::serenity_prelude::Client;
use crate::{cmds, data, events, re_cmds, rs_println, Args, Data};
use crate::{cmds, data::{self, get_mutex_data}, events, re_cmds, rs_println, Args, Cmd, Data};
pub async fn gen_data(args: Args, owners: Vec<u64>) -> Data {
@@ -56,28 +56,7 @@ pub async fn gen_bot(data: Data, args: Args) -> Client {
let framework = poise::Framework::builder()
.options(poise::FrameworkOptions {
owners: own,
commands: vec![
cmds::help::cmd(),
cmds::ping::cmd(),
cmds::embed::cmd(),
cmds::send::cmd(),
cmds::stop::cmd(),
cmds::eight_ball::cmd(),
cmds::add_server::cmd(),
// reddit
re_cmds::add::cmd(),
re_cmds::approve::cmd(),
re_cmds::get::cmd(),
re_cmds::remove::cmd(),
re_cmds::top::cmd(),
re_cmds::update::cmd(),
re_cmds::vote::cmd(),
re_cmds::shorturl::cmd(),
// reddit admin
re_cmds::admin_bind::cmd(),
// cfg
cmds::reload_cfg::cmd()
],
commands: make_cmd_vec(&data).await,
event_handler: events::event_handler,
..Default::default()
})
@@ -94,3 +73,43 @@ pub async fn gen_bot(data: Data, args: Args) -> Client {
.await
.unwrap();
}
async fn make_cmd_vec(data: &Data) -> Vec<Cmd> {
let mut cmds = vec![];
let cfg = get_mutex_data(&data.cfg).await.unwrap();
// GENERIC
cmds.extend([
cmds::help::cmd(),
cmds::ping::cmd(),
cmds::embed::cmd(),
cmds::send::cmd(),
cmds::stop::cmd(),
cmds::eight_ball::cmd(),
cmds::add_server::cmd()
]);
// REDDIT
if cfg["RESTART_enabled"].as_bool().unwrap() {
cmds.extend([
re_cmds::add::cmd(),
re_cmds::approve::cmd(),
re_cmds::get::cmd(),
re_cmds::remove::cmd(),
re_cmds::top::cmd(),
re_cmds::update::cmd(),
re_cmds::vote::cmd(),
re_cmds::shorturl::cmd()
]);
}
cmds.extend([
// reddit admin
re_cmds::admin_bind::cmd(),
// cfg
cmds::reload_cfg::cmd()
]);
return cmds
}
+4 -1
View File
@@ -37,10 +37,12 @@ use std::process;
use std::thread;
use std::time::Duration;
use std::vec;
use std::error::Error as StdErr;
use clap::Parser;
use r#gen::gen_bot;
use r#gen::gen_data;
use poise::Command;
use schedule::run_schedules;
use serde::Serialize;
use serde_json::Value;
@@ -74,8 +76,9 @@ struct Args {
}
type Error = Box<dyn std::error::Error + Send + Sync>;
type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;
type Cmd = Command<Data, Box<dyn StdErr + Send + Sync>>;
struct Data {