Update for Nix module
This commit is contained in:
parent
677a97d5cc
commit
b374c6ed66
8 changed files with 262 additions and 39 deletions
|
@ -1,4 +1,5 @@
|
|||
use crate::utils::*;
|
||||
use crate::ARGS;
|
||||
use serde_json::Map;
|
||||
|
||||
use crate::serenity;
|
||||
|
@ -21,7 +22,7 @@ use std::path::Path;
|
|||
pub async fn challenge(ctx: Context<'_>) -> Result<(), Error> {
|
||||
ctx.say(format!(
|
||||
"Tegaki Tuesday #{n}: <https://{domain}/{n}>",
|
||||
domain = get_domain(),
|
||||
domain = ARGS.domain,
|
||||
n = get_challenge_number()
|
||||
))
|
||||
.await?;
|
||||
|
@ -199,7 +200,7 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul
|
|||
let thank_you = &format!(
|
||||
"Thank you for submitting! You can view your submission at <https://{domain}/{}>",
|
||||
challenge_number,
|
||||
domain = get_domain()
|
||||
domain = ARGS.domain
|
||||
);
|
||||
let mut repost_here = true;
|
||||
match ctx {
|
||||
|
@ -262,7 +263,7 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul
|
|||
channel
|
||||
.send_message(&ctx.serenity_context().http, |m| {
|
||||
m.embed(|e| {
|
||||
let domain = get_domain();
|
||||
let domain = &ARGS.domain;
|
||||
let n = get_challenge_number();
|
||||
let mut description = format!(
|
||||
"New submission to [Tegaki Tuesday #{n}](https://{domain}/{n})!"
|
||||
|
@ -344,7 +345,7 @@ pub async fn images(ctx: Context<'_>) -> Result<(), Error> {
|
|||
to_fullwidth(&(i + 1).to_string()),
|
||||
challenge_number,
|
||||
image,
|
||||
domain = get_domain()
|
||||
domain = ARGS.domain
|
||||
));
|
||||
}
|
||||
ctx.say(message).await?;
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
use crate::utils::get_domain;
|
||||
use crate::Context;
|
||||
use crate::Error;
|
||||
use crate::ARGS;
|
||||
use poise::command;
|
||||
|
||||
use std::env;
|
||||
|
||||
// TODO: Implement proper help text for command-specific help,
|
||||
// see https://github.com/kangalioo/poise/blob/90ac24a8ef621ec6dc3fc452762dc9cfa144f693/examples/framework_usage/main.rs#L18-L38
|
||||
#[command(
|
||||
|
@ -33,8 +31,8 @@ __**Kanji 漢字**__
|
|||
:game_die: `{p}kyoiku <grade|all>` Random Kyōiku kanji
|
||||
:game_die: `{p}jlpt <level|all>` Random JLPT kanji
|
||||
:game_die: `{p}hyogai <group|all>` Random Hyōgai kanji",
|
||||
p = env::var("PREFIX").unwrap(),
|
||||
domain = get_domain(),
|
||||
p = ARGS.prefix,
|
||||
domain = ARGS.domain,
|
||||
);
|
||||
ctx.say(message).await?;
|
||||
Ok(())
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
use crate::Context;
|
||||
use crate::Error;
|
||||
use crate::ARGS;
|
||||
use poise::command;
|
||||
|
||||
use serde_json::json;
|
||||
use std::env;
|
||||
|
||||
use crate::utils::*;
|
||||
|
||||
|
@ -100,7 +100,7 @@ pub async fn announcechallenge(ctx: Context<'_>) -> Result<(), Error> {
|
|||
let message = format!("Welcome to the **{n}{th}** weekly **Tegaki Tuesday** (手書きの火曜日) handwriting challenge! :pen_fountain: The prompt is available in both Japanese and English on the website at <https://{domain}/{n}>.
|
||||
|
||||
You can make submissions in both languages, but please submit in your target language first. Submissions can be submitted by uploading the image to this channel along with the `{p}submit` command. By submitting, you agree to having your work posted to the website under the Attribution-ShareAlike 4.0 Unported (CC BY-SA 4.0) license, attributed to your Discord account. (<https://creativecommons.org/licenses/by-sa/4.0>).",
|
||||
domain = get_domain(),
|
||||
domain = ARGS.domain,
|
||||
n = challenge_number,
|
||||
th = match challenge_number % 10 {
|
||||
1 => "ˢᵗ",
|
||||
|
@ -108,7 +108,7 @@ You can make submissions in both languages, but please submit in your target lan
|
|||
3 => "ʳᵈ",
|
||||
_ => "ᵗʰ"
|
||||
},
|
||||
p = env::var("PREFIX").unwrap()
|
||||
p = ARGS.prefix
|
||||
);
|
||||
send(ctx, &message, true, false).await?;
|
||||
ctx.say("Announced!").await?;
|
||||
|
|
54
src/main.rs
54
src/main.rs
|
@ -1,7 +1,8 @@
|
|||
mod commands;
|
||||
mod utils;
|
||||
|
||||
use std::env;
|
||||
use std::env::{self, VarError};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
type Context<'a> = poise::Context<'a, Data, Error>;
|
||||
|
@ -12,6 +13,7 @@ pub struct Data {}
|
|||
use commands::{challenge::*, kanji::*, meta::*, owner::*};
|
||||
use poise::serenity_prelude as serenity;
|
||||
use poise::serenity_prelude::model::gateway::GatewayIntents;
|
||||
use clap::Parser;
|
||||
|
||||
#[poise::command(prefix_command)]
|
||||
async fn register(ctx: Context<'_>) -> Result<(), Error> {
|
||||
|
@ -19,12 +21,50 @@ async fn register(ctx: Context<'_>) -> Result<(), Error> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
#[clap(author, version, about)]
|
||||
struct Arguments {
|
||||
#[clap(long, help = "Website domain", default_value = "tegakituesday.com")]
|
||||
pub domain: String,
|
||||
#[clap(long = "token", help = "Discord token")]
|
||||
pub discord_token: String,
|
||||
#[clap(long, help = "Traditional text command prefix, including space, if any", default_value = "-h")]
|
||||
pub prefix: String,
|
||||
#[clap(long, help = "Path to Hugo project where site rebuilds")]
|
||||
pub hugo: String,
|
||||
#[clap(long = "guilds", help = "Guild data JSON file", default_value = "guilds.json")]
|
||||
pub guild_data: String,
|
||||
}
|
||||
|
||||
impl Arguments {
|
||||
fn from_dotenv() -> Result<Self, VarError> {
|
||||
Ok(Self {
|
||||
domain: env::var("DOMAIN")?,
|
||||
discord_token: env::var("DISCORD_TOKEN")?,
|
||||
prefix: env::var("PREFIX")?,
|
||||
hugo: env::var("HUGO")?.into(),
|
||||
guild_data: env::var("GUILD_DATA")?.into(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref ARGS: Arguments = {
|
||||
match env::args().count() {
|
||||
1 => {
|
||||
// This will load the environment variables located at `./.env`, relative to
|
||||
// the CWD. See `./.env.example` for an example on how to structure this.
|
||||
dotenv::dotenv().expect("Failed to load .env file");
|
||||
|
||||
Arguments::from_dotenv().unwrap()
|
||||
},
|
||||
_ => Arguments::parse(),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// This will load the environment variables located at `./.env`, relative to
|
||||
// the CWD. See `./.env.example` for an example on how to structure this.
|
||||
dotenv::dotenv().expect("Failed to load .env file");
|
||||
|
||||
// Initialize the logger to use environment variables.
|
||||
//
|
||||
// In this case, a good default is setting the environment variable
|
||||
|
@ -59,12 +99,12 @@ async fn main() {
|
|||
so(),
|
||||
],
|
||||
prefix_options: poise::PrefixFrameworkOptions {
|
||||
prefix: Some(env::var("PREFIX").expect("Expected a prefix in the environment")),
|
||||
prefix: Some(ARGS.prefix.clone()),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
})
|
||||
.token(std::env::var("DISCORD_TOKEN").expect("Expected a token in the environment"))
|
||||
.token(&ARGS.discord_token)
|
||||
.intents(
|
||||
GatewayIntents::GUILD_MESSAGES
|
||||
| GatewayIntents::DIRECT_MESSAGES
|
||||
|
|
25
src/utils.rs
25
src/utils.rs
|
@ -1,6 +1,7 @@
|
|||
use crate::serenity;
|
||||
use crate::Context;
|
||||
use crate::Error;
|
||||
use crate::ARGS;
|
||||
use rand::seq::IteratorRandom;
|
||||
use serde_json::Map;
|
||||
use serde_json::Value;
|
||||
|
@ -52,7 +53,7 @@ mod tests {
|
|||
}
|
||||
|
||||
pub fn get_challenge_number() -> i32 {
|
||||
let challenge_dir = format!("{}/content/challenges", env::var("HUGO").unwrap());
|
||||
let challenge_dir = format!("{}/content/challenges", ARGS.hugo);
|
||||
let paths = fs::read_dir(challenge_dir).unwrap();
|
||||
let mut max = 0;
|
||||
for path in paths {
|
||||
|
@ -72,20 +73,12 @@ pub fn get_challenge_number() -> i32 {
|
|||
max
|
||||
}
|
||||
|
||||
pub fn get_domain() -> String {
|
||||
env::var("DOMAIN").unwrap()
|
||||
}
|
||||
|
||||
pub fn get_hugo_path() -> String {
|
||||
env::var("HUGO").unwrap()
|
||||
}
|
||||
|
||||
pub fn get_submission_images_dir() -> String {
|
||||
format!("{}/assets/{}", get_hugo_path(), get_challenge_number())
|
||||
format!("{}/assets/{}", ARGS.hugo, get_challenge_number())
|
||||
}
|
||||
|
||||
pub fn get_submission_data_path(challenge: i32) -> String {
|
||||
format!("{}/data/challenges/{}.json", get_hugo_path(), challenge)
|
||||
format!("{}/data/challenges/{}.json", ARGS.hugo, challenge)
|
||||
}
|
||||
|
||||
pub fn get_current_submission_data_path() -> String {
|
||||
|
@ -153,17 +146,13 @@ pub fn to_fullwidth(string: &str) -> String {
|
|||
|
||||
pub fn rebuild_site() {
|
||||
Command::new("./build.sh")
|
||||
.current_dir(get_hugo_path())
|
||||
.current_dir(&ARGS.hugo)
|
||||
.status()
|
||||
.expect("Failed to rebuild site");
|
||||
}
|
||||
|
||||
pub fn get_guild_data_path() -> String {
|
||||
env::var("GUILD_DATA").unwrap()
|
||||
}
|
||||
|
||||
pub fn get_guild_data() -> Map<String, Value> {
|
||||
let guild_data_path = get_guild_data_path();
|
||||
let guild_data_path = &ARGS.guild_data;
|
||||
let guild_data_json = match File::open(&guild_data_path) {
|
||||
Ok(mut file) => {
|
||||
let mut json = String::new();
|
||||
|
@ -187,7 +176,7 @@ pub fn set_guild_data(guild_data: Map<String, Value>) {
|
|||
let mut guild_data_file = OpenOptions::new()
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.open(get_guild_data_path())
|
||||
.open(&ARGS.guild_data)
|
||||
.unwrap();
|
||||
guild_data_file
|
||||
.write_all(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue