Merge pull request #4 from Vylpes/feature/command-user-only

Limit commands to specific users
This commit is contained in:
Vylpes 2020-10-28 18:08:01 +00:00 committed by GitHub
commit e2974e1a0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 21 deletions

View file

@ -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
View file

@ -1,6 +1,6 @@
{
"name": "vylbot-core",
"version": "1.0.0",
"version": "1.0.4",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View file

@ -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": {

View file

@ -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;
}

View file

@ -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]);
}
}
}
}

View file

@ -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;