Skip to content

Instantly share code, notes, and snippets.

@iamnotacoder-djs
Created February 25, 2022 01:58
Show Gist options
  • Save iamnotacoder-djs/4b2dca184c48cc663a59079d96b47e6c to your computer and use it in GitHub Desktop.
Save iamnotacoder-djs/4b2dca184c48cc663a59079d96b47e6c to your computer and use it in GitHub Desktop.
Discord.js@13.6.0 Server lockdown
const { Client, Intents, Collection } = require('discord.js'),
client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES ] }),
fs = require('fs');
client.db = require("quick.db");
client.commands = new Collection();
client.applicationCommands = [];
var config = {
token: "your bot token",
prefix: "!",
adminID: "admin id",
embed_color: "#ffffff"
};
client.login(config.token);
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
loadCommands();
if ([null, undefined].includes(client.db.get(`lockdown`))) client.db.set(`lockdown`, {});
// client.db.set(`lockdown.g${guildID}`, {until: '5 mins', permissions: []});
checkFor10Minutes();
});
client.on('guildCreate', async (guild) => {
guild.commands.set(client.applicationCommands);
});
client.on('messageCreate', async (message) => {
if (!message.content.startsWith(config.prefix)) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift();
if (command == "update") return message.guild.commands.set(client.applicationCommands);;
const cmd = client.commands.get(`${command}`);
if (cmd) {
message.args = args;
message.isSlash = false;
cmd.messageExecute(client, message);
}
});
client.on('interactionCreate', async (interaction) => {
if (interaction.isCommand()) {
const cmd = client.commands.get(`${interaction.commandName}`);
if (!cmd) return;
try {
interaction.args = [];
interaction.options.data.forEach((option) => {
interaction.args.push(option.value);
});
interaction.isSlash = true;
await cmd.interactionExecute(client, interaction);
} catch (error) {
await interaction.reply({ content: 'Something went wrong...', ephemeral: true });
}
}
});
async function loadCommands() {
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.help.name, command);
// interaction
client.applicationCommands.push(command.application);
}
console.log(`Loaded ${commandFiles.length} commands.`)
}
async function checkFor10Minutes() {
const db = client.db.get(`lockdown`);
const map = new Map(Object.entries(db));
map.forEach((obj) => {
if (obj != null && obj.until != 0 && obj.until < Date.now()) {
client.commands.get(`stoplockdown`).direct(client);
}
})
setTimeout(checkFor10Minutes, 1000 * 60 * 10);
}
const { MessageEmbed, Permissions } = require('discord.js');
const help = {
name: "lockdown",
usage: "lockdown test"
};
module.exports = {
help: help,
messageExecute(client, message) {
exec(client, message);
},
application: {
name: help.name,
description: help.usage,
options: [{
name: "time",
description: "time in minutes",
type: "NUMBER"
}],
defaultPermission: true
},
interactionExecute(client, interaction) {
exec(client, interaction);
}
}
async function exec(client, command) {
/*
command?.guild
command?.member
command.channel
*/
if (!command?.member.permissions.has("ADMINISTRATOR")) {
return command.reply({
embeds: [
new MessageEmbed()
.setTitle(`Lockdown`)
.setDescription(`You don't have permission`)
],
ephemeral: true
});
}
// DB
if ([null, undefined].includes(client.db.get(`lockdown.g${command.guild.id}`))) client.db.set(`lockdown.g${command.guild.id}`, {});
client.db.set(`lockdown.g${command.guild.id}.until`, command.args.length == 0 ? 0 : Date.now() + 1000 * 60 * parseInt(command.args[0]));
const channels = await command.guild.channels.fetch();
channels.forEach((channel) => {
client.db.set(`lockdown.g${command.guild.id}.c${channel.id}`, {
hasParent: channel.parent != undefined,
permissionsLocked: channel.permissionsLocked,
permissions: channel.permissionOverwrites.cache
});
});
channels.forEach((channel) => {
channel.permissionOverwrites.set([
{
id: command.guild.id, // @everyone
deny: [Permissions.FLAGS.VIEW_CHANNEL]
},
{
id: client.user.id, // bot can see this channels
allow: [Permissions.FLAGS.VIEW_CHANNEL]
}
], 'Lockdown');
});
const chan = await command.guild.channels.create('Lockdown', {
type: 'GUILD_TEXT',
permissionOverwrites: [
{
id: command.guild.id,
allow: [Permissions.FLAGS.VIEW_CHANNEL],
deny: [Permissions.FLAGS.SEND_MESSAGES]
}
]
});
client.db.set(`lockdown.g${command.guild.id}.cInfo`, chan.id);
chan.send({
content: `@everyone Server is under lockdown!`
});
const embed = new MessageEmbed()
.setTitle(`Lockdown`)
.setDescription(`Done`);
command.reply({
embeds: [embed],
ephemeral: true
});
}
const { MessageEmbed, Permissions } = require('discord.js');
const help = {
name: "stoplockdown",
usage: "stoplockdown test"
};
module.exports = {
help: help,
messageExecute(client, message) {
exec(client, message);
},
application: {
name: help.name,
description: help.usage,
options: [],
defaultPermission: true
},
interactionExecute(client, interaction) {
exec(client, interaction);
},
direct(client, guildId) {
stopLockdown(client, guildId);
}
}
async function exec(client, command) {
/*
command?.guild
command?.member
command.channel
*/
if (!command?.member.permissions.has("ADMINISTRATOR")) {
return command.reply({
embeds: [
new MessageEmbed()
.setTitle(`Lockdown`)
.setDescription(`You don't have permission`)
],
ephemeral: true
});
}
stopLockdown(client, command.guild.id);
const embed = new MessageEmbed()
.setTitle(`Lockdown`)
.setDescription(`Server lockdown has ended`);
command.reply({
embeds: [embed],
ephemeral: true
});
}
async function stopLockdown(client, guildId) {
if (client.db.get(`lockdown.g${guildId}.cInfo`) == undefined) return;
const chan = await client.channels.fetch(client.db.get(`lockdown.g${guildId}.cInfo`));
await chan.delete();
const channels = await chan.guild.channels.fetch();
const categories = channels.filter(chan => chan.type == "GUILD_CATEGORY");
const others = channels.filter(chan => chan.type != "GUILD_CATEGORY");
categories.forEach((channel) => {
const perms = client.db.get(`lockdown.g${guildId}.c${channel.id}`);
channel.permissionOverwrites.set(perms.permissions);
});
others.forEach((channel) => {
const perms = client.db.get(`lockdown.g${guildId}.c${channel.id}`);
if (perms.hasParent && perms.permissionsLocked != false) {
channel.lockPermissions();
} else {
channel.permissionOverwrites.set(perms.permissions);
}
});
client.db.set(`lockdown.g${guildId}`, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment