Skip to content

Commit

Permalink
move persistant roles to mongo!! #362
Browse files Browse the repository at this point in the history
Signed-off-by: RedGuy12 <paul@reid-family.org>
  • Loading branch information
RedGuy12 committed Oct 3, 2023
1 parent 5961652 commit 3ca6c3b
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 41 deletions.
21 changes: 17 additions & 4 deletions common/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,28 @@ declare global {
}
namespace NodeJS {
interface ProcessEnv {
/** The main guild ID for the bot to operate in. Requires Administrator permission in this server. */
GUILD_ID: Snowflake;
/** The bot's token. */
BOT_TOKEN: string;
// BOT_SECRET: string;
/** Defaults to `"development"` */
/** The URI to use when connecting to MongoDB. */
MONGO_URI: string;
/**
* The mode for the bot to run in. Defaults to `"development"`.
*
* For consistency, always compare against `"production"` in code.
*/
NODE_ENV?: "development" | "production";
/**
* Whether or not to enable features requiring `@napi-api/canvas`, which does not work on some devices. Defaults to `true`.
*
* For consistency, always compare against `"true"` in code.
*/
CANVAS?: `${boolean}`;
/** The port to run the web server on in production. Not used in development. */
PORT?: `${number}`;
/** The API key to force a database write in production. Not used in development. */
CDBL_AUTH?: string;
/** Defaults to `true` */
CANVAS?: `${boolean}`;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ActivityType, GatewayIntentBits } from "discord.js";
import pkg from "./package.json" assert { type: "json" };
import { login, client } from "strife.js";
import constants from "./common/constants.js";
import mongoose from "mongoose";

dns.setDefaultResultOrder("ipv4first");

Expand All @@ -15,6 +16,8 @@ if (
)
throw new Error("Refusing to run on production Scradd without `--production` flag");

await mongoose.connect(process.env.MONGO_URI);

if (process.env.CANVAS !== "false") {
const { GlobalFonts } = await import("@napi-rs/canvas");
GlobalFonts.registerFromPath(
Expand Down
45 changes: 17 additions & 28 deletions modules/roles/persisted.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
import type { GuildMember, PartialGuildMember, Snowflake } from "discord.js";
import type { GuildMember, PartialGuildMember } from "discord.js";
import config from "../../common/config.js";
import Database from "../../common/database.js";

export const rolesDatabase = new Database<{
id: Snowflake;
designer: boolean;
scradd: boolean;
formerAdmin: boolean;
formerMod: boolean;
dev: boolean;
translator: boolean;
contributor: boolean;
og: boolean;
epic: boolean;
booster: boolean;
}>("roles");
await rolesDatabase.init();
import mongoose from "mongoose";

const persistedRoles = {
designer: "916020774509375528",
scradd: "1008190416396484700",
formerAdmin: ["1069776422467555328", "806603332944134164"],
formerMod: ["881623848137682954", config.roles.mod?.id],
admin: ["1069776422467555328", "806603332944134164"],
mod: ["881623848137682954", config.roles.mod?.id],
dev: "806608777835053098",
translator: "841696608592330794",
contributor: config.roles.dev?.id,
Expand All @@ -30,27 +15,31 @@ const persistedRoles = {
og: "1107170572963684402",
} as const;

export function persistedLeave(member: PartialGuildMember | GuildMember) {
const rolesSchema = new mongoose.Schema({
id: String,
...Object.fromEntries(Object.keys(persistedRoles).map((role) => [role, Boolean])),
});
const RoleList = mongoose.model("RoleList", rolesSchema);

export async function persistedLeave(member: PartialGuildMember | GuildMember) {
if (member.guild.id !== config.guild.id) return;

const memberRoles = {
id: member.id,
...Object.fromEntries(
await RoleList.findOneAndUpdate(
{ id: member.id },
Object.fromEntries(
Object.entries(persistedRoles).map(([key, ids]) => [
key,
[ids].flat().some((id) => id && member.roles.resolve(id)),
]),
),
};

if (!Object.values(memberRoles).includes(false)) return;
rolesDatabase.updateById(memberRoles, {});
{ upsert: true },
);
}

export async function persistedRejoin(member: GuildMember) {
if (member.guild.id !== config.guild.id) return;

const memberRoles = rolesDatabase.data.find((entry) => entry.id === member.id);
const memberRoles = await RoleList.findOneAndDelete({ id: member.id });
for (const roleName of Object.keys(persistedRoles)) {
const role = [persistedRoles[roleName]].flat()[0];
if (memberRoles?.[roleName] && role) await member.roles.add(role, "Persisting roles");
Expand Down
Loading

0 comments on commit 3ca6c3b

Please sign in to comment.