error handling and security fixes

This commit is contained in:
Jelle van Snik 2022-05-30 15:07:59 +02:00
parent e3692207e2
commit 998d39b09e
6 changed files with 68 additions and 185 deletions

View file

@ -1,3 +1,4 @@
{
"bot_token": "TOKEN"
}
"bot_token": "TOKEN",
"guild_id": "GUILDID"
}

View file

@ -1,6 +1,10 @@
const Discord = require('discord.js');
const { SlashCommandBuilder } = require('@discordjs/builders');
const allowedSelfAssignRoles = [
"streamping",
]
/**
*
* @param {Discord.CommandInteraction} interaction
@ -16,6 +20,15 @@ async function toggleroleHandler(interaction) {
const roles = await guild.roles.fetch();
const role = roles.find(role => role.name.toLowerCase() === roleName);
if (!allowedSelfAssignRoles.includes(roleName)) {
await interaction.followUp({
content: "Requested role is not self-assignable.",
ephemeral: true,
});
return;
}
if (!role) {
await interaction.followUp({
content: 'Unable to find the requested role. Contact and admin as soon as possible',
@ -58,5 +71,8 @@ module.exports = {
name: command.name,
help: 'Toggle on/off a given user role.\n```\nUsage: /togglerole <role>\n```',
handler: toggleroleHandler,
deploy: command.toJSON()
deploy: command.toJSON(),
extra: {
allowedSelfAssignRoles
}
};

View file

@ -37,8 +37,8 @@ async function interactionCreateHander(interaction) {
} else {
await interaction.reply(payload);
}
} catch (error) {
console.log(error);
} catch (replyError) {
console.log(replyError, error);
}
}
}

View file

@ -19,7 +19,11 @@ async function modalSubmitHandler(interaction) {
}
// run the modal
modal.handler(interaction);
try {
await modal.handler(interaction);
} catch {
console.error("handler failed modal");
}
}
module.exports = modalSubmitHandler;

View file

@ -1,4 +1,5 @@
const Discord = require('discord.js');
const { allowedSelfAssignRoles } = require("../commands/togglerole").extra;
const roleSelectMenu = new Discord.MessageSelectMenu();
roleSelectMenu.setCustomId('role-self-assign');
@ -32,6 +33,15 @@ async function roleSelfAssignHandler(interaction) {
const roles = await guild.roles.fetch();
const role = roles.find(role => role.name.toLowerCase() === roleName);
if (!allowedSelfAssignRoles.includes(roleName)) {
await interaction.followUp({
content: "Requested role is not self-assignable.",
ephemeral: true,
});
return;
}
if (!role) {
await interaction.followUp({
content: 'Unable to find the requested role. Contact and admin as soon as possible',

View file

@ -1,29 +1,28 @@
const Discord = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');
const util = require('./util');
const { bot_token: botToken } = require('../config.json');
const Discord = require("discord.js");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v10");
const util = require("./util");
const { bot_token: botToken } = require("../config.json");
/**
*
*
* @param {Discord.Guild} guild
*/
async function setupGuild(guild) {
// do nothing if the bot does not have the correct permissions
if (!guild.me.permissions.has([Discord.Permissions.FLAGS.MANAGE_ROLES, Discord.Permissions.FLAGS.MANAGE_CHANNELS])) {
console.log('Bot does not have permissions to set up in guild', guild.name);
return;
}
// do nothing if the bot does not have the correct permissions
if (!guild.me.permissions.has([Discord.Permissions.FLAGS.MANAGE_CHANNELS])) {
console.log("Bot does not have permissions to set up in guild", guild.name);
return;
}
// Setup commands
await deployCommands(guild);
// If anyone has a better way of doing this I'm all ears
// names should explain what they do
await setupCategories(guild);
await setupVoiceChannels(guild);
await util.updateMemberCountChannels(guild);
// Setup commands
await deployCommands(guild);
try {
await util.updateMemberCountChannels(guild);
} catch {
// we dont care if it fails on setup
}
}
/**
@ -31,168 +30,21 @@ async function setupGuild(guild) {
* @param {Discord.Guild} guild
*/
async function deployCommands(guild) {
const deploy = [];
const deploy = [];
guild.client.commands.forEach(command => {
deploy.push(command.deploy);
});
guild.client.commands.forEach((command) => {
deploy.push(command.deploy);
});
guild.client.contextMenus.forEach(contextMenu => {
deploy.push(contextMenu.deploy);
});
guild.client.contextMenus.forEach((contextMenu) => {
deploy.push(contextMenu.deploy);
});
const rest = new REST({ version: '10' }).setToken(botToken);
const rest = new REST({ version: "10" }).setToken(botToken);
await rest.put(Routes.applicationGuildCommands(guild.me.id, guild.id), { body: deploy });
await rest.put(Routes.applicationGuildCommands(guild.me.id, guild.id), {
body: deploy,
});
}
/**
*
* @param {Discord.Guild} guild
*/
async function setupCategories(guild) {
await setupStatsCategory(guild);
}
/**
*
* @param {Discord.Guild} guild
*/
async function setupVoiceChannels(guild) {
await setupMembersCountChannel(guild);
await setupPeopleCountChannel(guild);
await setupBotsCountChannel(guild);
}
/***************************
* *
* CATEGORY CHANNELS *
* *
***************************/
/**
*
* @param {Discord.Guild} guild
*/
async function setupStatsCategory(guild) {
const channels = await guild.channels.fetch();
let category = channels.find(channel => channel.type === 'GUILD_CATEGORY' && channel.name === 'stats');
if (!category) {
category = await guild.channels.create('stats', {
type: 'GUILD_CATEGORY'
});
}
if (category.position !== 0) {
await category.setPosition(0);
}
}
/************************
* *
* VOICE CHANNELS *
* *
************************/
/**
*
* @param {Discord.Guild} guild
*/
async function setupMembersCountChannel(guild) {
const channels = await guild.channels.fetch();
const category = channels.find(channel => channel.type === 'GUILD_CATEGORY' && channel.name === 'stats');
let channel = channels.find(channel => channel.type === 'GUILD_VOICE' && channel.name.startsWith('Members'));
if (!channel) {
channel = await guild.channels.create('Members - 0', {
type: 'GUILD_VOICE',
});
}
if (channel.parentId !== category.id) {
await channel.setParent(category);
}
if (channel.position !== 0) {
await channel.setPosition(0);
}
const permissionOverwrites = [{
id: guild.roles.everyone,
deny: [
Discord.Permissions.FLAGS.CONNECT
]
}];
await channel.permissionOverwrites.set(permissionOverwrites);
}
/**
*
* @param {Discord.Guild} guild
*/
async function setupPeopleCountChannel(guild) {
const channels = await guild.channels.fetch();
const category = channels.find(channel => channel.type === 'GUILD_CATEGORY' && channel.name === 'stats');
let channel = channels.find(channel => channel.type === 'GUILD_VOICE' && channel.name.startsWith('People'));
if (!channel) {
channel = await guild.channels.create('People - 0', {
type: 'GUILD_VOICE',
});
}
if (channel.parentId !== category.id) {
await channel.setParent(category);
}
if (channel.position !== 1) {
await channel.setPosition(1);
}
const permissionOverwrites = [{
id: guild.roles.everyone,
deny: [
Discord.Permissions.FLAGS.CONNECT
]
}];
await channel.permissionOverwrites.set(permissionOverwrites);
}
/**
*
* @param {Discord.Guild} guild
*/
async function setupBotsCountChannel(guild) {
const channels = await guild.channels.fetch();
const category = channels.find(channel => channel.type === 'GUILD_CATEGORY' && channel.name === 'stats');
let channel = channels.find(channel => channel.type === 'GUILD_VOICE' && channel.name.startsWith('Bots'));
if (!channel) {
channel = await guild.channels.create('Bots - 0', {
type: 'GUILD_VOICE',
});
}
if (channel.parentId !== category.id) {
await channel.setParent(category);
}
if (channel.position !== 2) {
await channel.setPosition(2);
}
const permissionOverwrites = [{
id: guild.roles.everyone,
deny: [
Discord.Permissions.FLAGS.CONNECT
]
}];
await channel.permissionOverwrites.set(permissionOverwrites);
}
module.exports = setupGuild;
module.exports = setupGuild;