Error when running discord bot piece of code - discord.js

So when running te following piece of code:
case 'greeting':
if (member.hasPermission('ADMINISTRATOR')) {
if(!args[1]) return message.reply('Foloseste "//greeting')
else
canal = args[1]
message.channel.send("Done")
} else {
message.channel.send("Nu esti administrator")
}
break;
I get the following error
if (member.hasPermission('ADMINISTRATOR')) {
^
ReferenceError: member is not defined
So how do I define member? Also tell me if there is something wrong with the code. I am trying to make it so that users with the administrator permission to be able to change the value of "canal". Thanks for your patience reading this!

Like jakye said, you are looking for message.member, if you want to store it inside a variable/define it just use the normal variable syntax:
const member = message.member;

Related

Kick Script discord.js v.13

I wanted to copy my Kick Command from Discord v.12, but I got the problem that I can't kick any members. You can see my error code down below.
My Code:
case 'kick':
case 'Kick':
if (!message.member.permissions.has(Discord.Permissions.FLAGS.KICK_MEMBERS)) return;
let kicked = message.mentions.members.first();
if (!kicked) return
message.guild.members(kicked).kick();
const kickemb = new Discord.MessageEmbed()
.setColor('RED')
.addField(`The User`, `${message.mentions.members.first()} was kicked successfully!`)
.setFooter(`The Kick was from ${message.author.username}#${message.author.discriminator}!`)
message.channel.send({embeds: [kickemb]});
My error code:
C:\User\Bot v13\index.js:185
message.guild.members(kicked).kick();
^
TypeError: message.guild.members is not a function
Guild#members() (Guild#member()) is deprecated. Your kicked object is already a member, call .kick() on that.
let kicked = message.mentions.members.first();
if (!kicked) return;
kicked.kick();
// Your code...
let kicked = message.mentions.members.first();
if (!kicked) return
kicked.kick();
or
let kicked = message.mentions.members.first();
if (!kicked) return
message.guild.members.cache.get(kicked).kick();
you can use both, good coding
That is not the correct way to get a member. kicked is already a GuildMember which means you can run .kick() on it:
kicked.kick()

discord.js Discord bot returns "TypeError: cmd.run is not a function"

The code is:
module.exports = (client, message) => {
if (message.author.bot) return;
if (message.content.indexOf(client.config.prefix) !== 0) return;
const args = message.content.slice(client.config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
const cmd = client.commands.get(command);
if (!cmd) return;
cmd.run(client, message, args);
};
and the Error message is:
cmd.run(client, message, args);
^
TypeError: cmd.run is not a function
What can I use instead of run, or how I can fix the problem?
I don't know what you are trying to do, because client.commands doesn't exist in any version of discord.js that I used, so you may look at this part of your code.
(Maybe we can help you a bit more if you give us a bigger part of the code, which may give us informations about variables, or maybe give more informations about what you're trying to do.)
Thanks ;)

Discord bot: Respond "Unknown command" when using an incorrectly spelled command

I want to have my discord.js bot respond
Unknown command, use c!help for available commands" when doing something like, c!hep (misspelled), or a different type of command not implemented, like, c!youtube, or just flat out random letters like c!rgoiw.
Basically just a response if their message doesn't match any commands available.
I don't have any specific code, I'm just using the const PREFIX = 'c!'; with let args = message.content.substring(PREFIX.length).split(" ") and setting all the commands in a switch(args[0]){ block.
I don't know really anything about coding, all that i've done so far is pretty self explanatory once wrote out, but I don't know what to go for when writing it from scratch.
Haven't seen any threads online about an unknown command response so I'm assuming it might be impossible to do.
Thanks
const PREFIX = 'c!';
bot.on('message', message=>{
let args = message.content.substring(PREFIX.length).split(" ")
switch(args[0]){
case 'example':
break;
//Code to respond to the prefix with no matching case
}
})
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
A default clause; if provided, this clause is executed if the value of expression doesn't match any of the case clauses.
Example:
switch (expr) {
case 'Oranges':
console.log('Oranges are $0.59 a pound.');
break;
case 'Mangoes':
case 'Papayas':
console.log('Mangoes and papayas are $2.79 a pound.');
// expected output: "Mangoes and papayas are $2.79 a pound."
break;
default:
console.log('Sorry, we are out of ' + expr + '.');
}
As shown above, if nothing matches (to translate to your use case - if no command is recognized) then execute commands found under default
In other words, your code should be
switch(args[0]){
case 'example':
break;
default: console.log(`Unknown command, use c!help for available commands`);
}
})

Using a let variable in message.content.send in discord.js

I'm basically making a counting bot where one command shows a let in a message, one increments it and sends a message, and, once I get passed this little issue, one that will let me set the let in case someone goes overboard. It's for a D&D campaign using discord and is basically just a meme bot I'm making for fun. The issue I'm having is that I can't figure out how to use the let variable in the message itself. This is what I'm working with based on youtube and my previous experience in Python, of which there is little.
const { prefix, token } = require('./Config.json');
const client = new Discord.Client();
let insight = 4;
client.once('ready', () => {
console.log("Ready!")
})
client.on('message', message => {
console.log(message.content);
if(message.content.startsWith(`${prefix}insight`)) {
return message.channel.send("Ghyger had distrusted someone ", insight, " times!")
}
if(message.content.startsWith(`${prefix}distrust`)) {
insight++;
return message.channel.send("Not again Ghyger! That's ", insight, " times now!")
}
})
client.login(token);
When this is ran and I execute either command, it throws up this, followed by a bunch of paths
options.reply = reply;
^
TypeError: Cannot create property 'reply' on number '5'
It's because reply is a function, not a variable, you should be calling it options.reply(reply);.
Fix is let option.reply = reply

Detecting Guild ID

I want to make certain commands so they can only be used in a specific guild.
I've tried an if statement that checks if message.guild.id = the guild id, but the command still will execute in any guild, perhaps I'm doing something wrong.
client.on("message", msg => {
var bcid = "585676550544949248"
if (msg.guild.id = bcid) {
if (msg.content.toLowerCase().startsWith(prefix + "ping")) {
msg.channel.send("Pong! :ping_pong:");
}
}
});
One = symbol is used for assigning a value to a variable, two == or three = symbols are used for checking if a value equals another value. In if statements you use the two or thee equals symbol operator.
Thus to fix your problem, change the if to:
if (msg.guild.id === bcid) {
// Your code here
}
Check out this link to learn about the difference between == and ===.

Resources