Skip to content

Commit

Permalink
Merge pull request Rdeisenroth#39 from Rdeisenroth/feature/improved-r…
Browse files Browse the repository at this point in the history
…ole-management

Improve role management
  • Loading branch information
Rdeisenroth authored Nov 30, 2023
2 parents 75bfc4d + c987d2e commit 35e08fc
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 181 deletions.
107 changes: 0 additions & 107 deletions src/commands/admin/fixverify.ts

This file was deleted.

74 changes: 0 additions & 74 deletions src/commands/admin/genverifyroles.ts

This file was deleted.

81 changes: 81 additions & 0 deletions src/commands/admin/setinternalrole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Message, ApplicationCommandOptionType } from "discord.js";
import { Command } from "../../../typings";
import "moment-duration-format";
import { GuildModel } from "../../models/guilds";
import { InternalGuildRoles, InternalRoles, RoleScopes } from "../../models/bot_roles";
import { mongoose } from "@typegoose/typegoose";



/**
* The Command Definition
*/
const command: Command = {
name: "setinternalrole",
guildOnly: false,
description: "This command generates the database entries for the internal roles",
options: [
{
name: "internal-role-name",
description: "The Internal Role Name used for tracking",
type: ApplicationCommandOptionType.String,
choices: InternalGuildRoles.map(x => ({ name: Object.keys(InternalRoles).find(y => InternalRoles[y as keyof typeof InternalRoles] === x)!, value: x })),
required: true,
},
{
name: "guild-role",
description: "The Guild Role to use for the Internal Role",
type: ApplicationCommandOptionType.Role,
required: true,
},
],
async execute(client, interaction, args) {
if (!interaction || !interaction.guild) {
return;
}
if (interaction instanceof Message) {
client.utils.embeds.SimpleEmbed(interaction, "Slash Only Command", "This Command is Slash only but you Called it with The Prefix. use the slash Command instead.");
return;
}
await interaction.deferReply();
const guild = interaction.guild;
const internalRoleName = interaction.options.getString("internal-role-name", true) as typeof InternalGuildRoles[number];
const guildRole = interaction.options.getRole("guild-role", true);
const dbGuild = await GuildModel.findById(guild.id);
if(!dbGuild) {
return await client.utils.embeds.SimpleEmbed(interaction, "Adminstration", "Guild not found in Database.");
}
let roles = dbGuild.guild_settings.roles;
if (!roles) {
dbGuild.guild_settings.roles = new mongoose.Types.DocumentArray([]);
roles = dbGuild.guild_settings.roles;
}
const role = roles.find(x => x.internal_name === internalRoleName);
if (role) {
// Update
role.role_id = guildRole.id;
role.server_role_name = guildRole.name;
} else {
roles.push({
internal_name: internalRoleName,
role_id: guildRole.id,
scope: RoleScopes.SERVER,
server_id: guild.id,
server_role_name: guildRole.name,
});
}

await dbGuild.save();

await client.utils.embeds.SimpleEmbed(
interaction,
"Adminstration",
`Internal Role ${internalRoleName} set to <@&${guildRole.id}>. Note that orphans are not automatically removed.`,
);
},
};

/**
* Exporting the Command using CommonJS
*/
module.exports = command;
110 changes: 110 additions & 0 deletions src/commands/admin/updatebotroles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { InternalGuildRoles } from "../../models/bot_roles";
import { Message, ApplicationCommandOptionType } from "discord.js";
import { Command } from "../../../typings";
import "moment-duration-format";
import { GuildModel } from "../../models/guilds";
import { RoleScopes } from "../../models/bot_roles";
import { mongoose } from "@typegoose/typegoose";



/**
* The Command Definition
*/
const command: Command = {
name: "updatebotroles",
guildOnly: false,
description: "This command updates or generates the database entries for the internal roles",
options: [
{
name: "create-if-not-exists",
description: "Create the Roles on the guild with the default name if they don't exist, defaults to true",
type: ApplicationCommandOptionType.Boolean,
required: false,
},
],
async execute(client, interaction, args) {
if (!interaction || !interaction.guild) {
return;
}
if (interaction instanceof Message) {
client.utils.embeds.SimpleEmbed(interaction, "Slash Only Command", "This Command is Slash only but you Called it with The Prefix. use the slash Command instead.");
return;
}

await interaction.deferReply();
const guild = interaction.guild;
const dbGuild = await GuildModel.findById(guild.id);
if (!dbGuild) {
return await client.utils.embeds.SimpleEmbed(interaction, "Adminstration", "Guild not found in Database.");
}

const createIfNotExists = interaction.options.getBoolean("create-if-not-exists", false) ?? true;


// Create the roles if they don't exist
// iterate over type InternalGuildRoles
for (const irn of InternalGuildRoles) {
let r = guild.roles.cache.find(x => x.name === irn);
const dbr = dbGuild.guild_settings.roles?.find(x => x.internal_name === irn);
if (!r) {
// try to find role by id from db
if (dbr) {
const newR = interaction.guild.roles.resolve(dbr.role_id!);
if (newR) {
r = newR;
if (dbr.server_role_name !== r.name) {
console.log(`Role "${irn}" was renamed from "${dbr.server_role_name}" to "${r.name}". Updating DB`);
// update db
dbr.server_role_name = r.name;
await dbGuild.save();
}
continue;
}
}
if (!createIfNotExists) {
console.log(`role ${irn} not found in guild ${guild.name}. Skipping`);
continue;
}
console.log(`role ${irn} not found in guild ${guild.name}. Creating`);
const newRole = await guild.roles.create({
name: irn,
mentionable: false,
});
if (!newRole) {
console.log(`Could not create role ${irn}`);
continue;
}
console.log(`created role ${irn}`);
r = newRole;
}
if (!dbr) {
console.log(`creating role ${irn}`);
if (!dbGuild.guild_settings.roles) {
dbGuild.guild_settings.roles = new mongoose.Types.DocumentArray([]);
}
dbGuild.guild_settings.roles.push({
internal_name: irn,
role_id: r.id,
scope: RoleScopes.SERVER,
server_id: guild.id,
server_role_name: r.name,
});
await dbGuild.save();
}
}
await client.utils.embeds.SimpleEmbed(
interaction,
"Adminstration",
`Done generating internal Roles. Internal Roles: \n
${(dbGuild.guild_settings.roles?.map(x => `❯ ${x.internal_name}: <@&${x.role_id}>`).join("\n") ?? "None")}
\nUnassigned Roles: \n
${InternalGuildRoles.filter(x => !dbGuild.guild_settings.roles?.find(y => y.internal_name === x)).map(x => `❯ ${x}`).join("\n") ?? "None"}`,
);
},
};

/**
* Exporting the Command using CommonJS
*/
module.exports = command;
Loading

0 comments on commit 35e08fc

Please sign in to comment.