Merge pull request #4 from Vylpes/feature/command-user-only
Limit commands to specific users
This commit is contained in:
commit
e2974e1a0d
6 changed files with 44 additions and 21 deletions
|
@ -67,6 +67,7 @@ class test extends command {
|
|||
super.requiredConfigs = "link";
|
||||
super.roles = "Moderator";
|
||||
super.roles = "Admin";
|
||||
super.users = "<ID-HERE>";
|
||||
}
|
||||
|
||||
test(context) {
|
||||
|
@ -84,11 +85,15 @@ module.exports = test;
|
|||
|
||||
> **Note:** You can set more than one role to be required by setting `super.roles` again.
|
||||
|
||||
5. If you want the command to require a variable in the config, set the name in `super.requiredConfigs`
|
||||
5. If you want the command to only be ran by specific users, set their ID in `super.users`
|
||||
|
||||
> **Note:** You can set more than one user to be required by settubg `super.users` again.
|
||||
|
||||
6. If you want the command to require a variable in the config, set the name in `super.requiredConfigs`
|
||||
|
||||
> **Note:** You can set more than one role to be required by setting `super.requiredConfigs` again.
|
||||
|
||||
6. Create a method using the name you set in `super(run)`, with the `context` as its parameter.
|
||||
7. Create a method using the name you set in `super(run)`, with the `context` as its parameter.
|
||||
|
||||
The `context` parameter will be a JSON object of:
|
||||
|
||||
|
|
2
package-lock.json
generated
2
package-lock.json
generated
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "vylbot-core",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.4",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "vylbot-core",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.4",
|
||||
"description": "A discord client based upon discord.js",
|
||||
"main": "./src/index",
|
||||
"scripts": {
|
||||
|
|
|
@ -34,10 +34,7 @@ class client extends Client {
|
|||
if (typeof config.prefix != "string") throw "Prefix is not a string";
|
||||
|
||||
if (typeof config.commands != "object") throw "Commands is not a string";
|
||||
if (!existsSync(`${process.cwd()}/${config.commands}`)) throw "Commands folder doesn't exist";
|
||||
|
||||
if (typeof config.events != "object") throw "Events is not a string";
|
||||
if (!existsSync(`${process.cwd()}/${config.events}`)) throw "Events folder doesn't exist";
|
||||
|
||||
this._config = config;
|
||||
}
|
||||
|
|
|
@ -9,14 +9,14 @@ class util {
|
|||
loadCommand(name, args, message) {
|
||||
for (let c = 0; c < this._client.config.commands.length; c++) {
|
||||
let folder = this._client.config.commands[c];
|
||||
|
||||
stat(`${process.cwd()}/${this._client.config.commands}/${name}.js`, (err, stat) => {
|
||||
|
||||
stat(`${process.cwd()}/${folder}/${name}.js`, err => {
|
||||
if (err == null) {
|
||||
let commandFile = require(`${process.cwd()}/${this._client.config.commands}/${name}.js`);
|
||||
let commandFile = require(`${process.cwd()}/${folder}/${name}.js`);
|
||||
let command = new commandFile();
|
||||
|
||||
|
||||
let requiredConfigs = command.requiredConfigs;
|
||||
|
||||
|
||||
for (let i = 0; i < requiredConfigs.length; i++) {
|
||||
if (!this._client.config[name]) throw `${commandFile.name} requires ${requiredConfigs[i]} in it's configuration`;
|
||||
if (!this._client.config[name][requiredConfigs[i]]) throw `${commandFile.name} requires ${requiredConfigs[i]} in it's configuration`;
|
||||
|
@ -30,7 +30,14 @@ class util {
|
|||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let users = command.users;
|
||||
|
||||
if (!users.includes(message.member.id)) {
|
||||
message.reply(`You do not have permission to run this command`);
|
||||
return;
|
||||
}
|
||||
|
||||
command[command.run]({
|
||||
"command": name,
|
||||
"arguments": args,
|
||||
|
@ -46,15 +53,19 @@ class util {
|
|||
}
|
||||
|
||||
loadEvents() {
|
||||
let eventFiles = readdirSync(`${process.cwd()}/${this._client.config.events}/`);
|
||||
for (let e = 0; e < this._client.config.events.length; e++) {
|
||||
let folder = this._client.config.events[e];
|
||||
|
||||
for (let i = 0; i < eventFiles.length; i++) {
|
||||
let eventName = eventFiles[i].split('.')[0];
|
||||
let file = require(`${process.cwd()}/${this._client.config.events}/${eventName}.js`);
|
||||
|
||||
let event = new file;
|
||||
|
||||
this._client.on(eventName, event[event.run]);
|
||||
let eventFiles = readdirSync(`${process.cwd()}/${folder}/`);
|
||||
|
||||
for (let i = 0; i < eventFiles.length; i++) {
|
||||
let eventName = eventFiles[i].split('.')[0];
|
||||
let file = require(`${process.cwd()}/${folder}/${eventName}.js`);
|
||||
|
||||
let event = new file;
|
||||
|
||||
this._client.on(eventName, event[event.run]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ class command {
|
|||
|
||||
this._roles = [];
|
||||
this._requiredConfigs = [];
|
||||
this._users = [];
|
||||
}
|
||||
|
||||
// Description
|
||||
|
@ -50,6 +51,15 @@ class command {
|
|||
set requiredConfigs(conf) {
|
||||
this._requiredConfigs.push(conf);
|
||||
}
|
||||
|
||||
// Users
|
||||
get users() {
|
||||
return this._users;
|
||||
}
|
||||
|
||||
set users(userid) {
|
||||
this._users.push(userid);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = command;
|
||||
|
|
Reference in a new issue