Issue with sending periodic messages using discord.js - 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

Related

time_amount says it is undefined even tho there are 2 arguments

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

ReactJS - Validate Nested-IF/ELSE Statement

In my react app I'm using IF statement inside another IF statement to validate a condition that I want to get data filtered only from a specific column not from all.
Basically I'm using AntD table in which I'm displaying students data. In a table I do have few columns namely Age, Marks and Attendance. On each column(Age, Marks and Attendance) I'm applying a range filter with aim to fetch student(s) when applied search on a specific column i.e. either Age, Marks or on Attendance.
The problem I'm getting that my Nested-IF condition is not validating to true. Rest of code is working fine. I also tried to run code without Nested-IF(I mean with the simple IF/ELSE statement). That is also working fine.
I don't know what did I wrong or what I'm missing/skipping in my attempt.
Help me to validate my Nested-IF/ELSE statement(s).
What I'm trying is:
const handleSearch = (firstInput, secondInput, dataIndex) => {
setSearchStu(
student.filter((obj) => {
if (dataIndex === "age") {
if (obj.age >= firstInput && obj.age <= secondInput) {
return true;
} else {
return false;
}
} else {
if (dataIndex === "marks") {
if (obj.marks >= firstInput && obj.marks <= secondInput) {
return true;
} else {
return false;
}
} else {
if (dataIndex === "attendance") {
if (obj.attendance >= firstInput && obj.attendance <= secondInput) {
return true;
} else {
return false;
}
} else {
return false;
}
}
}
})
);
};
Where
firstInput is the 1st input field in a range filter
secondInput is the 2nd input field in a range filter
dataIndex is like a key to validate my 1st IF statement in Nested-IF case
Values "age", "marks" and "attendance" are the defined values which are uniquely correspond to the each columns(Age, Marks and Attendance) in the table
and so on..
Thank you in advance for your assistance.
to simplfy your code, you can try this :
const handleSearch = (firstInput, secondInput, dataIndex) => {
setSearchStu(
student.filter((obj) => {
if (dataIndex === "age") {
return (obj.age >= firstInput && obj.age <= secondInput)
} else {
if (dataIndex === "marks") {
return (obj.marks >= firstInput && obj.marks <= secondInput)
} else {
if (dataIndex === "attendance") {
return (obj.attendance >= firstInput && obj.attendance <= secondInput)
} else {
return false;
}
}
}
})
);
};
or like this :
switch(dataIndex){
case "age" :
return (obj.age >= firstInput && obj.age <= secondInput)
break;
case "marks" :
return (obj.marks >= firstInput && obj.marks <= secondInput)
break;
case "attendance" :
return (obj.attendance >= firstInput && obj.attendance <= secondInput)
break;
default :
return false
}
The function does work given the right inputs and data - I made a sandbox on replit to verify. Possibly there is an issue with the student data (consider renaming to students) or with the inputs to the function. Also, the function can be simplified to the following (see replit sandbox):
const handleSearch = (firstInput, secondInput, dataIndex) => {
return (
student.filter((obj) => {
if (["age", "marks", "attendance"].includes(dataIndex)) {
return obj[dataIndex] >= firstInput && obj[dataIndex] <= secondInput
}
return false
})
);
};

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');

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.

php for loops how to start again after else

I want to do something like this. Do you have ideas for this?
=== a ===
if( Something ){
=== b ===
{
else
{
go back to === a ===
}
Thanks.
It is usual while loop.
$something = false;
while (!$something) {
//here are ====a==== actions
}
//here are b actions
You want something like this:
while (true) {
if( Something ){
=== b ===
}
else
{
=== a ===
}
if (we're done) {
break;
}
}
Alternatively, you could use a proper condition in your while loop to exit.

Resources