time_amount says it is undefined even tho there are 2 arguments - discord.js

when i execute the command with 2 agrumments it then sends in the channel "member gas been timed out for undefined seconds"
example command: !timeout #user 10
if (command === 'timeout'){
let { member, time_amount} = args
if (!args.length) {
return message.channel.send(`You didn't provide a amount of time or a member, ${message.author}!`);
} else {
if(args.length = 0){
message.channel.send(`there are only ${args.length} argument please add the right arguments`)
}
else{
if(args.length = 1){
console.log(time_amount)
if(isNaN(time_amount)){
if (message.member.roles.cache.find(r => r.name === 'owner')){
const member = message.mentions.members.first() || message.guild.members.cache.get(argument[0]) || message.guild.members.cache.find( x => x.user.username.toLocaleLowerCase() === argument.slice(0).join(" " || x.user.username === argument[0]));
member.timeout()
message.channel.send(`member has been timed out for `+ time_amount + ` seconds`)
setTimeout(function(){message.channel.send('the user <#' + member.id + '> is not in timeout anymore.')}, time_amount)
}
else{
message.channel.send('you dont have the right promitions <#' + message.author.id + '>.')
}
}
else{
message.channel.send(`time amount is not a number, <#${message.author.id}>`)
}
}
else{
message.channel.send(`there are to many agruments please add the right arguments`)
}
}
}
}
i want that time_amount stores the second argument so could somebody help me

Related

Discord.js | How to read a number in a message?

I'm trying to make a game for my discord bot: bot chooses a random number and then, user tries to find number by guidance of the bot. I tried this thing but when I send a message (I send a number) it turns NaN. Why this happens and how can I fix it?
let randomNumber = (Math.floor( Math.random() * (100)))
let usersAnswer = parseInt(message.content)
if (!message.author.bot && message.content === "!pick number"){
message.reply("I kept! :check: ").then(j => {
message.channel.awaitMessages(( message, user) => (user.id === message.author.id && randomNumber < usersAnswer || randomNumber > usersAnswer),
{max: 1, time: 10000}).then(j => {
if (usersAnswer < randomNumber) {
message.reply("Increase! :arrow_up:" + usersAnswer) //I add usersAnswer to the bot's message because I want to see what it reads my message like in Discord
}
else {
message.reply("Decrease! :arrow_down:" + usersAnswer)
}
})
})
}
If you use parseInt() you need to set a radix before using this method. I took 10 as an example.
let usersAnswer = parseInt(message.content, 10)
if (!message.author.bot && message.content === "!pick number"){
message.reply("I kept! :check: ").then(j => {
message.channel.awaitMessages(( message, user) => (user.id === message.author.id && randomNumber < usersAnswer || randomNumber > usersAnswer),
{max: 1, time: 10000}).then(j => {
if (usersAnswer < randomNumber) {
message.reply("Increase! :arrow_up:" + usersAnswer) //I add usersAnswer to the bot's message because I want to see what it reads my message like in Discord
}
else {
message.reply("Decrease! :arrow_down:" + usersAnswer)
}
})
})
}
Or you could also simply use Number():
let usersAnswer = Number(message.content)
if (!message.author.bot && message.content === "!pick number"){
message.reply("I kept! :check: ").then(j => {
message.channel.awaitMessages(( message, user) => (user.id === message.author.id && randomNumber < usersAnswer || randomNumber > usersAnswer),
{max: 1, time: 10000}).then(j => {
if (usersAnswer < randomNumber) {
message.reply("Increase! :arrow_up:" + usersAnswer) //I add usersAnswer to the bot's message because I want to see what it reads my message like in Discord
}
else {
message.reply("Decrease! :arrow_down:" + usersAnswer)
}
})
})
}
If I were you I would add a check if the usersAnswer is even a Number otherwise the output could also be NaN.)
You could do this by simply adding the following line:
if (isNaN(usersAnswer)) return message.reply('This is not a number');

DMing a Specific Person with Pinging Him

so today I was trying to make a bot DM a specific person by pinging him, for example: !dm #potato hello
By Using This:
if (message.content.startsWith(prefix + 'dm')) {
let args = message.content.split(" ")[2];
let user = message.mentions.users.first()
if (!user) return message.channel.send("**Please mention a user**")
if (!message.member.roles.cache.has('814454313438806016')) {
return message.channel.send('You do not have the permissions to use this command.')
}
if (!args[0])
return message.reply("Nothing to say?").then(m => m.delete(5000));
if (args[0].toLowerCase() === "embed") {
const embed = new MessageEmbed()
.setDescription(args.slice(1).join(" "))
.setTimestamp()
.setColor('WHITE');
message.channel.send(embed);
} else {
message.channel.send('**Done**')
user.send(args.join(" "));
}
}
And SMH, it's not working, I hope you guys could help me
Console Link Screenshot: https://prnt.sc/10kzxie
Tbh, there's not a lot of errors in your code, just a lil bit of problems. Use the following, if there are still problems, please comment!
if (message.content.startsWith(prefix + 'dm')) {
let args = message.content.slice(prefix.length).split(/ +/g);
let member = message.mentions.members.first()
if (!member) return message.channel.send("**Please mention a member**")
if (!message.member.roles.cache.has('814454313438806016')) {
return message.channel.send('You do not have the permissions to use this command.')
}
if (!args[0]){
return message.reply("Nothing to say?").then(m => m.delete(5000));
}
if (args[0].toLowerCase() === "embed") {
const embed = new MessageEmbed()
.setDescription(args.slice(2).join(" "))
.setTimestamp()
.setColor('WHITE');
message.channel.send('**Done**')
member.send(embed);
} else {
message.channel.send('**Done**')
member.send(args.splice(1).join(" "));
}
}

I'm trying to make a clear command with role permissions for my Discord bot. (Discord.js)

I have some code already, but how would I implement some other code to make the command only accessible by users with the MANAGE_MESSAGES permission?
My attempt at doing it myself:
else if (message.content.startsWith(`${prefix}clear`)) {
const amount = parseInt(args[0]);;
if (isNaN(amount)) {
return message.reply('that doesn\'t seem to be a valid number.');
} else if (amount <= 0 || amount > 100) {
return message.reply('you need to input a number between 1 and 100.');
}
message.channel.bulkDelete(amount, true).catch(err => {
console.error(err);
message.channel.send('Uh oh! Something went wrong!');
}).catch(() => {
if (!message.member.hasPermission(['MANAGE_MESSAGES'])) {
message.reply("you do not have permission to use this command!");
}
});
}
Without the extra bit at the end:
else if (message.content.startsWith(`${prefix}clear`)) {
const amount = parseInt(args[0]);;
if (isNaN(amount)) {
return message.reply('that doesn\'t seem to be a valid number.');
} else if (amount <= 0 || amount > 100) {
return message.reply('you need to input a number between 1 and 100.');
}
message.channel.bulkDelete(amount, true).catch(err => {
console.error(err);
message.channel.send('Uh oh! Something went wrong!');
});
}
Try this:
else if (message.content.startsWith(`${prefix}clear`)) {
// put this at the very top
if (!message.member.hasPermission("MANAGE_MESSAGES")) {
return message.reply("you do not have permission to use this command!");
const amount = parseInt(args[0]);
if (isNaN(amount))
return message.reply("that doesn't seem to be a valid number.");
if (amount <= 0 || amount > 100)
return message.reply("you need to input a number between 1 and 100.");
message.channel
.bulkDelete(amount, true)
.catch((err) => {
console.error(err);
message.channel.send("Uh oh! Something went wrong!");
})
.catch((err) => console.log(err));
}
}
I think the problem is that you were not returning if the member did not have the required permissions, so your code was just continuing on normally.

(Discord.js) Bot does not respond

I've made a bot, and I have this purge function, it worked before i added the if that checked for the user's role. It gives me no errors and doesnt reply at all, no matter if i have the roles or not.
Code:
client.on("message", message => {
if (message.content.startsWith(prefix("purge"))) {
if (!message.guild.member.roles.cache.get('703727486009213048') || !message.guild.member.roles.cache.get('702847833241550859') || !message.guild.member.roles.cache.get('703727579328151562')) {
console.log('ssadd')
return message.reply('you can\'t use that command!')
};
const args = message.content.slice(prefix.length).split(" ");
const amount = args[1];
if (!amount) {
return message.reply("please specify the number of messages to purge!");
}
if (isNaN(amount * 1)) {
return message.reply(
"you'll need to specify a number, not whatever \"" +
`${amount}` +
'" is.'
);
}
message.delete();
message.channel.bulkDelete(amount * 1 + 1);
};
});
client.login(process.env.token);```
If it never replied to anything that either means the bot didn't log in or it never passed the first if condition. To check if the bot logged in, just do client.on("ready", () => console.log("ready"))
But I think it's more likely it just failed the first condition, is prefix a function?
prefix("purge") should be prefix + "purge".
There are some other flaws in your code too. Heres just the redo, if you need me to explain anything just lmk.
client.on("message", msg => {
if (msg.author.bot || !msg.content.startsWith(prefix)) return;
const args = msg.content.slice(1).split(" ");
//later on you should move to modules but for now this is fine ig
if (args[0] === "purge") {
//other flags here https://discord.js.org/#/docs/main/stable/class/Permissions?scrollTo=s-FLAGS
if (!msg.member.hasPermission("ADMINISTRATOR")) {
return msg.reply("you can't use that command!")
}
const amount = args[1] && parseInt(args[1]);
if (!amount) {
return msg.reply("please specify an integer of messages to purge!");
}
msg.delete();
msg.channel.bulkDelete(amount);
};
});
client.login(process.env.token);

Issue with sending periodic messages using discord.js

I get this issue
setInterval(function(){
var randommessage = Math.floor(Math.random() * 5) + 1;
var randomsentence = Math.floor(Math.random() * 10) + 1;
if(randommessage === 1){
console.log("Silence...")
} else {
if(randomsentence === 1){
client.channels.get('532065669718736906').send("Do you ever wonder why we were born... whats our purpose?")
} else if(randomsentence === 2){
client.channels.get('532065669718736906').send("So... you come here often?")
} else if(randomsentence === 3){
client.channels.get('532065669718736906').send("What is the point of this server?")
} else if(randomsentence === 4){
client.channels.get('532065669718736906').send("Why do i exist?")
} else if(randomsentence === 5){
client.channels.get('532065669718736906').send("Why are we still here... just to suffer..")
} else if(randomsentence === 6){
client.channels.get('532065669718736906').send("Begels are life!")
} else if(randomsentence === 7){
client.channels.get('532065669718736906').send("Banana Bros!!")
} else if(randomsentence === 8){
client.channels.get('532065669718736906').send("Is the number 3 a myth?")
} else if(randomsentence === 9){
client.channels.get('532065669718736906').send("*Shuffles through papers*")
} else if(randomsentence === 10){
client.channels.get('532065669718736906').send("In the end.. it doesnt even matter!")
} else {
client.channels.get('532065669718736906').send("randomness is gud")
}
}
}, 3000);
that is suppose to send periodic messages except I get the error
TypeError: Cannot read property 'send' of undefined
at Timeout._onTimeout (C:\Users\User\Documents\Discord Intro Bot\index.js:52:46)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7)
I've tried everything under the sun to try and fix this even finding the channel beforehand then sending to it, but it still thinks the channel does not exist.
I don't want to use the on message since my server is pretty inactive, and I'm using this to add a bit of emotion to the bot

Resources