I have declared a variable beforehand called:
var mathsLesson = 'https://www.link1.com';
The value of this variable will change
if (message.content === '!change') { //this checks if the user messages !change
mathsLesson = 'https://www.link2.com'
message.channel.send(mathsLesson); //This outputs www.link2.com
}
However when I output mathsLesson outside the if statement, it shows 'www.link1.com' instead of 'www.link2.com' which I wanted. How do I make it output 'www.link2.com'? Thanks in advance!
You shouldnt be using a VAR or CONST if the value of the variable tends the change. Dont use VAR because its pretty old and has many shortcomings change the var to let if you wanat the variable globally you can always export a function to return its value andmany other options
let mathsLesson = 'https://www.link1.com';
if (message.content === '!change') { //this checks if the user messages !change
let mathsLesson = 'https://www.link2.com'
message.channel.send(mathsLesson); //This outputs www.link2.com
}
Others have suggested that you should change var to let, but it won't make any difference. I'm not sure where you're checking the value, but as you can see below, it works as expected, if the statement is true:
var mathsLesson = 'https://www.link1.com';
var message = {
content: '!change'
};
console.log('outside if, before updating:', mathsLesson);
if (message.content === '!change') {
mathsLesson = 'https://www.link2.com';
console.log('inside if:', mathsLesson);
}
console.log('outside if, after updated:', mathsLesson);
And won't work if the statement is false:
var mathsLesson = 'https://www.link1.com';
var message = {
content: 'no command for you today'
};
console.log('outside if, before updating:', mathsLesson);
if (message.content === '!change') {
mathsLesson = 'https://www.link2.com';
console.log('inside if:', mathsLesson);
}
console.log('outside if, after updated:', mathsLesson);
let mathsLesson = 'https://www.link1.com';
//The value of this variable will change
if (message.content === '!change') { //this checks if the user messages
mathsLesson = 'https://www.link2.com'
message.channel.send(mathsLesson); //This outputs www.link2.com
}
Reason
"var" can not be renew
Related
How can I toggle between false/true a variable's value with a command
my code
var tagdetect = true;
const command = args.shift().toLowerCase();
if (command === 'tagdetect'){
if (args[1] === 'true'){
}
}
if (tagdetect){
if (message.mentions.users.has('id')){
client.TagİDAlgılayıcı.get('nick').execute(message, args);
}
i am using a command handler, i want to do something like {prefix}tagdetect true/false
Assuming tagdetect is the variable you want to switch, you can use two if statements to switch the boolean's state based on user input.
if (args[1] === 'true'){
tagdetect = true;
} else if (args[1] === 'false'){
tagdetect = false;
} else {
// User has given an invalid setting, let them know by sending an error message.
}
The comment is just there to let you know what you should add.
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);
Is there any way to make an event toggleable with a command?
I'm trying to make a welcome/farewell event but I don't want it to be active on default.
This is how my event looks right now:
client.on("guildMemberAdd", (member) => {
const guild = member.guild;
let memberTag = member.user.tag;
guild.channels.sort(function(chan1, chan2) {
if (chan1.type !== `text`) return 1;
if (!chan1.permissionsFor(guild.me).has(`SEND_MESSAGES`)) return -1;
return chan1.position < chan2.position ? -1 : 1;
}).first().send(memberTag + " just joined <:floshed:533687801741443082>");
});
As requested here is an example of my comment:
One way to do it is to store a variable for a guild in some database which has a value of either true or false. Then you'd grab that variable and check if said guild has the option turned on or off
client.on("guildMemberAdd", (member) => {
const guild = member.guild;
let memberTag = member.user.tag;
// Code here to get the guild from database, this is just a non-working example
let dbGuild = database.get('Guild', guild.id);
// Check if the guild has the welcome command disabled
if (dbGuild.enableWelcomeCmd === false) {
// Breaks the function, no further message will be send
return;
}
guild.channels.sort(function(chan1,chan2){
if(chan1.type!==`text`) return 1;
if(!chan1.permissionsFor(guild.me).has(`SEND_MESSAGES`)) return -1;
return chan1.position < chan2.position ? -1 : 1;
}).first().send(memberTag + " just joined <:floshed:533687801741443082>");
});
client.on("message", async message => {
// Check if the msg has been send by a bot
if(message.author.bot) return;
// Check if message has correct prefix
if(message.content.indexOf(config.prefix) !== 0) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
// Code for actually changing the guild variable
if (command === 'toggleWelcome') {
// Code here to get the guild from database, this is just a non-working example
let dbGuild = database.get('Guild', message.guild.id);
dbGuild.enableWelcomeCmd = !dbGuild.enableWelcomeCmd;
// Save the new variable for the guild (also a non-working example)
database.save('Guild', message.guild.id, dbGuild);
}
});
You'll have to look into databases and such yourself, there is a wide variety of (free) options which all have a different syntax. That part is something for you to figure out but I hope this can give you a general idea.
I'm trying to make an input form that will check if the data exists in firebase database, below is the code that I used to check the data :
if(element.validation.codeunique) {
function checkCode(inputcode) {
firebaseUsers.orderByChild('code')
.equalTo(inputcode).once('value')
.then( snapshot => {
let thecode = null;
if(snapshot.val()){
thecode = false;
} else {
thecode = true;
}
console.log(thecode)
})
}
let checkcode = null;
checkcode = checkCode(element.value);
console.log(checkcode)
const valid = checkcode;
const message = `${!valid ? 'Code Exists':''}`;
error = !valid ? [valid,message] : error
}
in database i have a data :
users
-L3ZeHOI7XOmP9xhPkwX
-code:"DEM"
when I entered data DEM at the form :
console.log(thecode) result is false (which is the result that i want)
and when i entered another data ASD at the form :
console.log(thecode) result is true(which is the result that i want)
so the firebaseUsers actually is giving me the feedback/data that I want, but when I tried to get the data at valid with true/false,
this is the code that I originally used:
if(element.validation.codeunique) {
function checkCode(inputcode) {
firebaseUsers.orderByChild('code')
.equalTo(inputcode).once('value')
.then( snapshot => {
if(snapshot.val()){
return false;
} else {
return true;
}
})
}
const valid = checkCode(element.value);
const message = `${!valid ? 'Code Exists':''}`;
error = !valid ? [valid,message] : error
}
the valid shows = undefined,
my goal is to make the valid shows true/false,
could someone help me, and point out, what did i do wrong?
*I make the first code just to point out that the firebase is actually working
*the second code is the one that I originally used
There are a few issues that are standing out to me.
First, the code that you use originally was using return inside of the function's if / else conditional. I believe this is correct, you'll want to go back to doing that.
Second, the firebase.orderByChild() function that you are calling is a Promise as it has a .then() statement appended to it. What this means is that the function does not synchronously finish executing and return a value. The code below that promise is being run while the firebase function is still processing.
Give this a shot and see if it works, and if you have any further errors beyond the promise.
if(element.validation.codeunique) {
function checkCode(inputcode) {
firebaseUsers.orderByChild('code')
.equalTo(inputcode).once('value')
.then( snapshot => {
if(snapshot.val()){
return false;
} else {
return true;
}
console.log(thecode)
})
}
let message = checkCode(element.value) ? 'Code Exists':'Code Does not Exist';
}
I've done it outside of the function above and by checking it when the user submit the form, and if the code exists, i direct it to form again :
firebaseUsers.orderByChild('code')
.equalTo(mycode)
.once('value')
.then (snapshot => {
if(snapshot.val()) {
alert("Code Exists, Please Choose another");
this.props.history.push('/myscreen')
} else {
//submit the data
}
hopefully this will help someone
// Check voted
var votes = res.data.votes;
if(votes.length == 0){$scope.like = true;}
votes.forEach(function(vote){
if(vote.userId === auth.profile.user_id) {
$scope.liked = true;
} else {
$scope.like = true;
}
});
I have written a code to check if the user has voted or not, but I'm having a small issue with the else statement:
using the following code, the $scope.liked works correctly, but the else statement only goes for the first.
How can I edit this, so he goes through all the votes, and if nothing is found, he displays the $scope.like
What about just do it outside the loop?
votes.forEach(function(vote){
if(vote.userId === auth.profile.user_id) {
$scope.liked = true;
}
});
$scope.like = !$scope.liked;