Commit 6bf06e23 authored by Yashin's avatar Yashin
Browse files

Adding permission system.

parent d73437f7
......@@ -3,4 +3,5 @@ node_modules
npm-debug.log
.env
.env.example
.DS_store
\ No newline at end of file
.DS_store
mongo
\ No newline at end of file
......@@ -235,4 +235,5 @@ dist
.pnp.*
.env
.DS_store
\ No newline at end of file
.DS_store
mongo
\ No newline at end of file
......@@ -5,6 +5,7 @@
<excludeFolder url="file://$MODULE_DIR$/temp" />
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
<excludeFolder url="file://$MODULE_DIR$/mongo" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
......
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = new SlashCommandBuilder()
.setName('assign_permission')
.setDescription('Assign a permission to a role')
.addStringOption(option =>
option.setName('permission')
.setDescription('Permission to assign.')
.setRequired(true)
.addChoice("Role Menus", 'role_menu'))
.addRoleOption(option =>
option.setName('role')
.setDescription('Role to assign permission to.')
.setRequired(true))
\ No newline at end of file
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = new SlashCommandBuilder()
.setName('list_permissions')
.setDescription('List all Permissions for this Server.');
\ No newline at end of file
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = new SlashCommandBuilder()
.setName('revoke_permission')
.setDescription('Revoke a permission from a role')
.addStringOption(option =>
option.setName('permission')
.setDescription('Permission to revoke.')
.setRequired(true)
.addChoice("Role Menus", 'role_menu'))
.addRoleOption(option =>
option.setName('role')
.setDescription('Role to revoke permission from.')
.setRequired(true))
\ No newline at end of file
const ServerConfig = require('../models/ServerConfig');
const {Permissions} = require('discord.js');
async function assignPermission(interaction) {
if (interaction.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) {
const role = interaction.options.getRole('role');
const perm = interaction.options.getString('permission');
let config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
if (config == null) {
config = new ServerConfig();
config.guildID = interaction.guild.id.toString();
config.permissions.roleMenu = [];
}
if (perm === 'role_menu') {
if (!config.permissions.roleMenu.includes(role.id.toString())) {
config.permissions.roleMenu.push(role.id.toString());
}
}
await config.save();
await interaction.reply({content: `Role ${role.name} has been added to ${perm} permission.`, ephemeral: true});
} else {
await interaction.reply({content: 'Only administrators can access this command.', ephemeral: true});
}
}
async function revokePermission(interaction) {
if (interaction.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) {
const role = interaction.options.getRole('role');
const perm = interaction.options.getString('permission');
let config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
if (config == null) {
config = new ServerConfig();
config.guildID = interaction.guild.id.toString();
config.permissions.roleMenu = [];
}
if (perm === 'role_menu') {
if (config.permissions.roleMenu.includes(role.id.toString())) {
config.permissions.roleMenu.splice(config.permissions.roleMenu.indexOf(role.id.toString()), 1);
}
}
await config.save();
await interaction.reply({
content: `Role ${role.name} has been removed from ${perm} permission.`,
ephemeral: true
});
} else {
await interaction.reply({content: 'Only administrators can access this command.', ephemeral: true});
}
}
async function listPermissions(interaction) {
if (interaction.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) {
let config = await ServerConfig.findOne({guildID: interaction.guild.id.toString()});
if (config == null) {
config = new ServerConfig();
config.guildID = interaction.guild.id.toString();
config.permissions.roleMenu = [];
}
const roles = await Promise.all(config.permissions.roleMenu.map(async (roleID) => (await interaction.guild.roles.fetch(roleID)).name));
await interaction.reply({content: `List of Permissions:\n\nRole Menu: ${roles.join(',')}`, ephemeral: true});
}
}
const slashCommandHandler = async (interaction) => {
if (interaction.commandName === 'assign_permission') {
await assignPermission(interaction);
} else if (interaction.commandName === 'revoke_permission') {
await revokePermission(interaction);
} else if (interaction.commandName === 'list_permissions') {
await listPermissions(interaction);
}
}
module.exports = {
slashCommandHandler
}
\ No newline at end of file
const RoleMenu = require('../models/RoleMenu');
const {MessageActionRow, MessageSelectMenu} = require("discord.js");
const checkPerm = require('../util/perm');
// slashcommand handler
async function addRoleMenu(interaction) {
console.log('test');
if (!(await checkPerm(interaction.member, 'role_menu'))) {
await interaction.reply({content: 'You are not authorized to do this.', ephemeral: true})
return;
}
const channel = interaction.options.getChannel('channel');
const messageContent = interaction.options.getString('message');
const type = interaction.options.getString('type');
......@@ -32,6 +38,10 @@ async function addRoleMenu(interaction) {
}
async function deleteRoleMenu(interaction) {
if (!(await checkPerm(interaction.member, 'role_menu'))) {
await interaction.reply({content: 'You are not authorized to do this.', ephemeral: true})
return;
}
const channel = interaction.options.getChannel('channel');
const messageID = interaction.options.getString('message_id');
let menu = await RoleMenu.findOne({guildID: channel.guild.id, channelID: channel.id, messageID});
......@@ -58,6 +68,10 @@ async function deleteRoleMenu(interaction) {
}
async function addRoleToMenu(interaction) {
if (!(await checkPerm(interaction.member, 'role_menu'))) {
await interaction.reply({content: 'You are not authorized to do this.', ephemeral: true})
return;
}
const channel = interaction.options.getChannel('channel');
const messageID = interaction.options.getString('message_id');
const role = interaction.options.getRole('role')
......@@ -123,6 +137,10 @@ async function addRoleToMenu(interaction) {
}
async function deleteRoleFromMenu(interaction) {
if (!(await checkPerm(interaction.member, 'role_menu'))) {
await interaction.reply({content: 'You are not authorized to do this.', ephemeral: true})
return;
}
const channel = interaction.options.getChannel('channel');
const messageID = interaction.options.getString('message_id');
const role = interaction.options.getRole('role');
......@@ -183,6 +201,10 @@ async function deleteRoleFromMenu(interaction) {
}
async function editRoleMenu(interaction) {
if (!(await checkPerm(interaction.member, 'role_menu'))) {
await interaction.reply({content: 'You are not authorized to do this.', ephemeral: true})
return;
}
const channel = interaction.options.getChannel('channel');
const messageID = interaction.options.getString('message_id');
const messageContent = interaction.options.getString('message');
......
......@@ -18,6 +18,8 @@ services:
mongo:
image: mongo
volumes:
- "./mongo:/data/db"
ports:
- "${MONGO_PORT}:${MONGO_PORT}"
restart: always
......
const connectDB = require('./config/db')
const connectDB = require('./util/db')
const {Client, Intents, MessageActionRow, MessageButton, MessageSelectMenu, MessageEmbed} = require('discord.js');
const fs = require('fs');
const botConfig = {
......
const {Schema, model} = require('mongoose');
const ServerConfigSchema = new Schema({
guildID: {
type: String,
unique: true,
required: true
},
permissions: {
roleMenu: [{
type: String
}]}
});
module.exports = model('serverconfig', ServerConfigSchema);
\ No newline at end of file
{
"name": "finf-discord",
"version": "0.1.0",
"version": "0.1.1",
"description": "",
"main": "index.ts",
"scripts": {
......
const ServerConfig = require('../models/ServerConfig');
const {Permissions} = require('discord.js');
module.exports = async (member, permission) => {
const guildID = member.guild.id;
const config = await ServerConfig.findOne({guildID});
if (config == null) {
return false;
}
if (permission === 'role_menu') {
console.log(member.roles.cache);
console.log(config.permissions.roleMenu);
return member.roles.cache.some((role) => config.permissions.roleMenu.some((prole) => prole == role.id.toString()))
|| member.permissions.has(Permissions.FLAGS.ADMINISTRATOR);
}
return false;
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment