aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCara Salter <cara@devcara.com>2022-04-14 13:06:54 -0400
committerCara Salter <cara@devcara.com>2022-04-18 23:05:25 -0400
commitffacf1c8fc820de3890ba6231b644e9e4a65ce28 (patch)
tree475537cef44f8eb0e7ed1269fd8c76e700c62918
parent9b4707192557cdd64dd82c5883e54758d0d66512 (diff)
downloadglitch-ng-ffacf1c8fc820de3890ba6231b644e9e4a65ce28.tar.gz
glitch-ng-ffacf1c8fc820de3890ba6231b644e9e4a65ce28.zip
FLAKE
-rw-r--r--.envrc1
-rw-r--r--.gitignore2
-rw-r--r--Cargo.lock2
-rw-r--r--Dockerfile13
-rw-r--r--flake.lock74
-rw-r--r--flake.nix97
-rw-r--r--src/commands/actions.rs2
-rw-r--r--src/commands/osu.rs3
-rw-r--r--src/handler.rs10
-rw-r--r--src/main.rs26
-rw-r--r--src/models.rs10
11 files changed, 225 insertions, 15 deletions
diff --git a/.envrc b/.envrc
new file mode 100644
index 0000000..3550a30
--- /dev/null
+++ b/.envrc
@@ -0,0 +1 @@
+use flake
diff --git a/.gitignore b/.gitignore
index fedaa2b..58e4501 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,4 @@
/target
.env
+result*
+.direnv/
diff --git a/Cargo.lock b/Cargo.lock
index 0bccd81..791625a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -474,7 +474,7 @@ dependencies = [
[[package]]
name = "glitch-ng"
-version = "0.5.0"
+version = "0.6.0"
dependencies = [
"chrono",
"dotenv",
diff --git a/Dockerfile b/Dockerfile
deleted file mode 100644
index 2a46a40..0000000
--- a/Dockerfile
+++ /dev/null
@@ -1,13 +0,0 @@
-FROM rust as builder
-
-WORKDIR /src/glitch
-
-COPY . /src/glitch
-
-RUN cargo build --release
-
-FROM archlinux
-
-COPY --from=builder /src/glitch/target/release/glitch-ng /bin/glitch/glitch-ng
-
-ENTRYPOINT [ "/bin/glitch/glitch-ng" ]
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..ce6187f
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,74 @@
+{
+ "nodes": {
+ "flake-utils": {
+ "locked": {
+ "lastModified": 1648297722,
+ "narHash": "sha256-W+qlPsiZd8F3XkzXOzAoR+mpFqzm3ekQkJNa+PIh1BQ=",
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "rev": "0f8662f1319ad6abf89b3380dd2722369fc51ade",
+ "type": "github"
+ },
+ "original": {
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "type": "github"
+ }
+ },
+ "naersk": {
+ "inputs": {
+ "nixpkgs": "nixpkgs"
+ },
+ "locked": {
+ "lastModified": 1639947939,
+ "narHash": "sha256-pGsM8haJadVP80GFq4xhnSpNitYNQpaXk4cnA796Cso=",
+ "owner": "nix-community",
+ "repo": "naersk",
+ "rev": "2fc8ce9d3c025d59fee349c1f80be9785049d653",
+ "type": "github"
+ },
+ "original": {
+ "owner": "nix-community",
+ "repo": "naersk",
+ "type": "github"
+ }
+ },
+ "nixpkgs": {
+ "locked": {
+ "lastModified": 1648219316,
+ "narHash": "sha256-Ctij+dOi0ZZIfX5eMhgwugfvB+WZSrvVNAyAuANOsnQ=",
+ "owner": "NixOS",
+ "repo": "nixpkgs",
+ "rev": "30d3d79b7d3607d56546dd2a6b49e156ba0ec634",
+ "type": "github"
+ },
+ "original": {
+ "id": "nixpkgs",
+ "type": "indirect"
+ }
+ },
+ "nixpkgs_2": {
+ "locked": {
+ "lastModified": 1648219316,
+ "narHash": "sha256-Ctij+dOi0ZZIfX5eMhgwugfvB+WZSrvVNAyAuANOsnQ=",
+ "owner": "NixOS",
+ "repo": "nixpkgs",
+ "rev": "30d3d79b7d3607d56546dd2a6b49e156ba0ec634",
+ "type": "github"
+ },
+ "original": {
+ "id": "nixpkgs",
+ "type": "indirect"
+ }
+ },
+ "root": {
+ "inputs": {
+ "flake-utils": "flake-utils",
+ "naersk": "naersk",
+ "nixpkgs": "nixpkgs_2"
+ }
+ }
+ },
+ "root": "root",
+ "version": 7
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..b559c45
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,97 @@
+{
+ inputs = {
+ flake-utils.url = "github:numtide/flake-utils";
+ naersk.url = "github:nix-community/naersk";
+ };
+
+ outputs = { self, nixpkgs, flake-utils, naersk }:
+ flake-utils.lib.eachDefaultSystem (
+ system: let
+ pkgs = nixpkgs.legacyPackages."${system}";
+ naersk-lib = naersk.lib."${system}";
+ in
+ rec {
+ # `nix build`
+ deps = with pkgs; [
+ pkg-config
+ openssl
+ gcc
+ ];
+ packages.glitch-ng = naersk-lib.buildPackage {
+ pname = "glitch-ng";
+ root = ./.;
+ buildInputs = deps;
+ };
+ defaultPackage = packages.glitch-ng;
+
+ # `nix run`
+ apps.glitch-ng = flake-utils.lib.mkApp {
+ drv = packages.glitch-ng;
+ };
+ defaultApp = apps.glitch-ng;
+
+ nixosModules.glitch = { config, lib, ... }: {
+ options = {
+ services.glitch-ng.enable = lib.mkEnableOption "enable glitch NG";
+ services.glitch-ng.environment-file-location = lib.mkOption {
+ type = lib.types.path;
+ default = "/var/lib/glitch-ng/.env";
+ description = "The location of the environment file";
+ };
+ };
+
+ config = lib.mkIf config.services.glitch-ng.enable {
+ users.groups.glitch-ng = {
+ members = [ "glitch-ng" ];
+ };
+ users.users.glitch-ng = {
+ createHome = true;
+ isSystemUser = true;
+ home = "/var/lib/glitch-ng";
+ group = "glitch-ng";
+ };
+
+ systemd.services.glitch-ng = {
+ wantedBy = [ "multi-user.target" ];
+ after = [ "glitch-ng-init.service" ];
+ requires = [ "glitch-ng-init.service" ];
+ serviceConfig = {
+ User = "glitch-ng";
+ Group = "glitch-ng";
+ Restart = "always";
+ WorkingDirectory = "${defaultPackage}";
+ ExecStart = "${defaultPackage}/bin/glitch-ng";
+ EnvironmentFile = "${config.services.glitch-ng.environment-file-location}";
+ };
+ };
+
+ systemd.services.glitch-ng-init = {
+ wantedBy = [ "multi-user.target" ];
+ requires = [ "postgresql.service" ];
+ after = [ "postgresql.service" ];
+ description = "Initialize for glitch-ng";
+
+ script = with pkgs; ''
+ if ! [ -e /var/lib/glitch-ng/.db-created ]; then
+ runuser -u ${config.services.postgresql.superUser} -- ${config.services.postgresql.package}/bin/createuser glitch-ng
+ runuser -u ${config.services.postgresql.superUser} -- ${config.services.postgresql.package}/bin/createdb -O glitch-ng glitch
+ touch /var/lib/glitch-ng/.db-created
+ fi
+ '';
+ serviceConfig = {
+ Type = "oneshot";
+ };
+ };
+
+ services.postgresql.enable = true;
+
+ };
+ };
+
+ # `nix develop`
+ devShell = pkgs.mkShell {
+ nativeBuildInputs = with pkgs; [ rustc cargo ] ++ deps;
+ };
+ }
+ );
+}
diff --git a/src/commands/actions.rs b/src/commands/actions.rs
index 63d00fb..a283238 100644
--- a/src/commands/actions.rs
+++ b/src/commands/actions.rs
@@ -114,6 +114,8 @@ pub async fn hug(
Ok(())
}
+
+/// Takes in a specific vector of URLs and returns a random one
fn get_random_url_from_vec(vec: Vec<&str>) -> &str {
let mut url = "";
let rand = rand::thread_rng().gen_range(0..vec.len());
diff --git a/src/commands/osu.rs b/src/commands/osu.rs
index 8d91f56..5e0b563 100644
--- a/src/commands/osu.rs
+++ b/src/commands/osu.rs
@@ -18,6 +18,9 @@ struct OsuTokenRequest {
pub scope: String,
}
+/// This is kinda loose, and we should really be caching the osu token
+///
+/// Eh well, this *works* (sort of)
async fn setup_reqwest() -> Result<reqwest::Client, Error> {
let client_id = std::env::var("OSU_CLIENT_ID").unwrap();
let client_secret = std::env::var("OSU_CLIENT_SECRET").unwrap();
diff --git a/src/handler.rs b/src/handler.rs
index ef8fc00..7b4568f 100644
--- a/src/handler.rs
+++ b/src/handler.rs
@@ -3,6 +3,12 @@ use poise::serenity_prelude as serenity;
use crate::models::ReactionRole;
use crate::{Data, Error};
+use tracing::info;
+
+/**
+ * Handles specific events, including ReactionAdd, which is needed for the reaction role handler to
+ * function properly
+ */
pub async fn event_handler(
ctx: &serenity::Context,
event: &poise::Event<'_>,
@@ -16,6 +22,7 @@ pub async fn event_handler(
if add_reaction.user_id.unwrap() == current_user.id {
return Ok(());
}
+ // This fetches the role and lets us query extra data including role ID
let rrole = sqlx::query_as!(
ReactionRole,
"SELECT * FROM reaction_roles WHERE message_id=$1 AND reaction=$2",
@@ -31,6 +38,7 @@ pub async fn event_handler(
add_reaction.user_id.unwrap().0,
)
.await?;
+ // Honestly, not really needed.
let member_roles = member.roles;
let role_id = serenity::RoleId(rrole.role_id.parse::<u64>()?);
if member_roles.contains(&role_id) {
@@ -60,7 +68,7 @@ pub async fn event_handler(
.await {
dm_chan.say(ctx, format!("Toggled the role!")).await?;
} else {
- println!("Could not DM user, but we did the role anyways");
+ info!("Could not DM user, but we did the role anyways");
}
add_reaction.delete(&ctx.http).await?;
diff --git a/src/main.rs b/src/main.rs
index 8693381..88d612e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,10 @@
+#![forbid(missing_docs)]
+/*!
+ * Full rewrite of the [Glitch](https://glitchbot.net) bot in Poise with slash commands
+ *
+ * This iteration will focus on code correctness and durability. The major goal is to avoid what
+ * happened to the Campmaster by avoiding code rot and forcing documentation on _everything_
+ */
use std::{sync::Mutex, time::Duration};
use dotenv::dotenv;
@@ -11,6 +18,7 @@ mod commands;
mod handler;
mod models;
+/// Contains data shared between all commands
pub struct Data {
pg: Mutex<PgPool>,
}
@@ -60,6 +68,7 @@ async fn register(ctx: Context<'_>, #[flag] global: bool) -> Result<(), Error> {
#[tokio::main]
#[instrument]
async fn main() {
+ // Initialize environment and logging
dotenv().unwrap();
tracing_subscriber::fmt::init();
info!("Initialized logging");
@@ -86,7 +95,9 @@ async fn main() {
..commands::reactionroles::rroles()
},
],
+ // This requires a closure, for some reason
on_error: |error| Box::pin(on_error(error)),
+ // Honestly could probably be removed, but it's kept in for ~reasons~
pre_command: |ctx| {
Box::pin(async move {
println!("Executing command {}...", ctx.command().name);
@@ -101,12 +112,15 @@ async fn main() {
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("~".into()),
edit_tracker: Some(poise::EditTracker::for_timespan(Duration::from_secs(3600))),
+ // These don't work, I thought they might but -\_()_/-
additional_prefixes: vec![
poise::Prefix::Literal("hey glitch"),
poise::Prefix::Literal("hey glitch,"),
],
..Default::default()
},
+ // For once, we abstracted the handler *out* of main.rs so we can actually read the damn
+ // file
listener: |ctx, event, _, data| Box::pin(handler::event_handler(ctx, event, data)),
..Default::default()
};
@@ -115,6 +129,18 @@ async fn main() {
.token(std::env::var("DISCORD_TOKEN").unwrap_or("BAD-TOKEN".into()))
.user_data_setup(move |_ctx, _ready, _framework| {
Box::pin(async move {
+ /*
+ * Hoo boy okay
+ *
+ * This sets up the postgres pool and adds it to the Data struct we defined
+ * earlier. Once that's done, it runs the migrations that have been embeded within
+ * the completed binary
+ *
+ * A sane default was chosen if DATABASE_URL doesn't exist
+ *
+ * If migrations fail, we panic and exit because then we're in an incorrect DB
+ * state and something needs to be fixed before any further work can be done.
+ */
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(
diff --git a/src/models.rs b/src/models.rs
index 431204a..09d5d7c 100644
--- a/src/models.rs
+++ b/src/models.rs
@@ -1,9 +1,19 @@
+/**
+ * Describes a Reaction Role as it appears in SQL
+ */
#[derive(Debug, Clone)]
pub struct ReactionRole {
+ /// The primary key
pub id: i32,
+ /// The ID of the channel where the menu is kept, turned into a String for ease of storage
pub channel_id: String,
+ /// The ID of the message within the channel containing the reaction menu
pub message_id: String,
+ /// The ID of the guild containing the channel
pub guild_id: String,
+ /// The String representation of the reaction, either as a unicode Emoji or a discord custom
+ /// emoji ID
pub reaction: String,
+ /// The ID of the role to be toggled by the menu option
pub role_id: String,
}