Reputation: 19
There was a post for this but it didn't seem to fix my problem. I'm writing a simple discord.js program and when I created a new command called forceverify, it gave the error "Cannot read property 'run' of undefined"
main.js:
const Discord = require('discord.js');
const client = new Discord.Client();
let fetch = require('node-fetch');
const prefix = '!'
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Loaded');
})
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'help') {
client.commands.get('help').execute(message, args, Discord);
} else if (command === 'verify') {
client.commands.get('verify').execute(message, args, Discord);
} else if (command === 'forceverify') {
client.commands.get('forceverify').run(message, args, Discord);
}
})
client.on('guildMemberAdd', member => {
const welcomeChannel = client.channels.cache.find(ch => ch.name === 'member-log');
if (!welcomeChannel) {
console.log('No channel was found');
}
welcomeChannel.send(`Welcome!`);
client.login('The token is there');
forceverify.js
const { DiscordAPIError } = require("discord.js");
const fetch = require("node-fetch");
const packageData = require("../package.json");
module.exports = {
name: 'verify',
description: "Verify yourself to get access to the server",
execute(message, args, Discord) {
if (message.member.roles.cache.has('805210755800367104')) {
const clientUser = client.users.fetch(args[1]);
let myRole = message.guild.roles.cache.get("805531649131151390");
fetch(`https://api.sk1er.club/player/${args[0]}`)
.then(res => res.json())
.then(({ player }) => {
clientUser.member.setNickname(player.displayname).catch(console.error);
})
.catch(err => console.log(err));
} else {
message.channel.reply("You don't have sufficient permissions.");
}
}
}
Every other command works properly but for some reason, this one simply doesn't work. I tried what the other question said to do (which is add .execute to module.exports or change .execute to .run) neither worked probably because it simply can't find the file (everything was spelled correctly)
Upvotes: 0
Views: 301
Reputation: 11915
The export from forceverify.js
has the name verify
but it should be forceverify
. So, the forceverify
command isn't registered and client.commands.get('forceverify')
returns undefined
.
// commands/forceverify.js
module.exports = {
name: 'forceverify',
description: 'Force verify yourself to get access to the server',
execute(message, args, Discord) {
// do stuff
}
}
And you should call the execute
function when invoking the command.
client.commands.get('forceverify').execute(message, args, Discord)
You can refactor your code to simplify the command invocation code.
client.on('message', (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) {
return
}
const args = message.content.slice(prefix.length).split(/ +/)
const command = args.shift().toLowerCase()
if (!client.commands.has(command)) {
return
}
client.commands.get(command).execute(message, args, Discord)
})
Upvotes: 2