Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as JavaScript by vlaz ( 7 years ago )
const Discord = require("discord.js");
const ChannelOverwrites = require("../models/ChannelOverwrites");
module.exports = {
name: 'lock',
description: 'Locks out everyone (except admin) from typing in a text channel',
cooldown: 3,
defaultPermissions: ["MANAGE_CHANNELS"],
needs: ["VIEW_CHANNEL", "SEND_MESSAGES", "MANAGE_CHANNELS", "MANAGE_ROLES"],
/**
* @param {Discord.Message} message
*/
async execute(client, message, args, config) {
var channel;
if (message.mentions.channels.first()) {
channel = message.mentions.channels.first();
}
else {
if (!args[0]) {
await message.react(client.emojis.get(config.emojis.fail));
return message.channel.send("No channel provided. Make sure you include it in the command.");
}
channel = message.guild.channels.find(channel => channel.type == 'text' && channel.name == args[0]);
if (!channel) {
await message.react(client.emojis.get(config.emojis.fail));
return message.channel.send("No channel with that name found. Make sure you spell it correctly.");
}
}
if (!channel) {
await message.react(client.emojis.get(config.emojis.fail));
return message.channel.send("No channel found.");
}
if (channel.guild.id != message.guild.id) {
await message.react(client.emojis.get(config.emojis.fail));
return message.channel.send("Can't lock a channel outside this guild.");
}
// Determine the period of the mute
var pattern = /\b[0-9]{1,4}[m,s,h,ms,d,w]?\b/g;
var found = pattern.exec(message.content);
var duration = null;
if (found) {
duration = new Duration(found[0]);
}
// Determine the reason
var reason;
if (found) {
if (message.content.length > pattern.lastIndex) {
reason = message.content.substr(pattern.lastIndex + 1);
}
}
else {
args.shift();
if (args.length != 0) {
reason = args.join(' ');
}
}
var toInsert = channel.permissionOverwrites.map(overwrite => {
return {
guild_id: message.guild.id,
channel_id: channel.id,
overwrite_id: overwrite.id,
type: overwrite.type,
allow: overwrite.allow,
deny: overwrite.deny,
timeout_timestamp: duration
}
});
if (!toInsert.some(overwrite => overwrite.overwrite_id == message.guild.id)) {
toInsert.push({
guild_id: message.guild.id,
channel_id: channel.id,
overwrite_id: message.guild.id,
type: 'role',
allow: new Discord.Permissions(0),
deny: new Discord.Permissions(0),
timeout_timestamp: duration
})
}
ChannelOverwrites.bulkCreate(toInsert).then(async () => {
var newOverwrites = channel.permissionOverwrites.map(overwrite => {
var allow = overwrite.allow.toArray();
if (allow.includes("SEND_MESSAGES")) {
allow = allow.filter(function (value, index, arr) {
return value != "SEND_MESSAGES";
});
}
var deny = overwrite.deny.toArray();
if (!deny.includes("SEND_MESSAGES")) {
deny.push("SEND_MESSAGES");
}
return {
id: overwrite.id,
allow,
deny
}
});
if (!newOverwrites.some(overwrite => overwrite.id == message.guild.id)) {
newOverwrites.push({
id: message.guild.id,
deny: ["SEND_MESSAGES"]
})
}
if (reason) {
await channel.send(reason);
}
channel.overwritePermissions({
permissionOverwrites: newOverwrites,
reason: `${message.author.tag} locked a channel.`
});
})
.catch(err => {
console.log(err);
});
},
};
Revise this Paste