I want the message to be sent to a specific channel when I give the command. I want the user to encounter an error if the command was not run on a particular channel. I need your help.
if (msg.author.bot) return;
if (msg.content.toLowerCase() === prefix + 'xgif' ) {
number = 100;
imageNumber = Math.floor (Math.random() * (number -1 + 1)) + 1;
client.channels.get(`channelID`).send( {files: ["./images/" + imageNumber + ".gif"]})
}
Error: TypeError: client.channels.get is not a function
Since discord.js v12 you need to use the cache property to access channels collection, so you need to replace
client.channels.get(`channelID`).send( {files: ["./images/" + imageNumber + ".gif"]})
with
client.channels.cache.get(`channelID`).send( {files: ["./images/" + imageNumber + ".gif"]})
Related
My bot are sends random gifs but not embed. I wanna sends random gifs with embed. How can I this ?
Codes:
if (msg.author.bot) return;
if (msg.content.toLowerCase() === prefix + 'xgif' ) {
number = 100;
imageNumber = Math.floor (Math.random() * (number -1 + 1)) + 1;
client.channels.cache.get(`channelID`).send( {files: ["./images/" + imageNumber + ".gif"]})
}
You need to first do
const { RichEmbed } = require('discord.js')
That'll get RichEmbed for you. Then, do
const embed = new RichEmbed()
There's a lot of things you can do to change the embed. There's
.setColor(hexidecimal)
.setThumbnail(normally user.displayAvatarURL)
.setFooter(whatever you want)
.setTimestamp()
.setDescription(whatever you want)
.setAuthor(whatever you want)
.addField(stuff)
I prepared an sending random images discord bot. bot works smoothly but instead of just sending an image, it randomly sends all images in the folder. I need your help.(12 images available, prefix: !! ) codes;
const Discord = require('discord.js');
const client = new Discord.Client();
const settings = require('./settings.json');
var prefix = settings.prefix;
client.on('ready', () => {
console.log(`${client.user.tag} ready!`);
});
client.on('message', msg => {
if (msg.content.toLowerCase() === prefix + 'xgif' )
number = 12;
imageNumber = Math.floor (Math.random() * (number -1 + 1)) + 1;
msg.channel.send ( {files: ["./images/" + imageNumber + ".gif"]})
});
client.login(TOKEN HERE)
You have numerous problems leading to this. First, your if statement isn't scoped properly. Your code is the functional equivalent of:
if (msg.content.toLowerCase() === prefix + 'xgif' ) {
number = 12;
}
imageNumber = Math.floor (Math.random() * (number -1 + 1)) + 1;
msg.channel.send ( {files: ["./images/" + imageNumber + ".gif"]})
So what is happening is number is not always set to 12, and the last two lines execute with every single message, including messages that come from the bot itself. You need to:
1. scope your if statement correctly.
2. ignore all bot message.
client.on('message', msg => {
if(msg.author.bot) return; // Ignore bots!
if (msg.content.toLowerCase() === prefix + 'xgif' ) {
number = 12;
imageNumber = Math.floor (Math.random() * (number -1 + 1)) + 1;
msg.channel.send ( {files: ["./images/" + imageNumber + ".gif"]})
} // End of if scope
}
Unlike Python, for one example, JavaScript and all other C-style syntax languages do not use white space to indicate scope. An if statement with no brackets only includes the next single statement in it's scope.
I'm working in a discord bot, specifically in a rate command, which is supposed to return:
"I would rate (thing to be rated) (random number)/10."
But it returns:
"I would rate (thing to be rated) [object Undefined]/10."
client.on('message', message => {
if(message.content.startsWith (prefix + "rate")) {
if(message.content.slice(prefix.length + 4) === ""){
message.channel.send("Give me something to rate");
}
else
message.channel.send("I would rate" + "**" +
message.content.slice(prefix.length + 4) + "**" + " " +
toString(Math.floor(Math.pow(Math.random, 10))) + "/10");
}
What can possibly be wrong?
You need to incorporate the following code (considering you want round integers returned):
Math.floor(Math.random()*11)
Ditch the Math.pow() method.
edit made from suggestion by #Geert-Jan
I'm creating a discord bot and part of its commands is to roll dice with different values (d6, d10, d20, d100). I've set it up like this:
const Discord = require('discord.js');
const client = new Discord.Client();
function commandIs (str, msg){
return msg.content.toLowerCase().startsWith("!" + str)
} //Function that shortens all response commands.
client.on ('message', message => {
if(commandIs("rolld6", message)){
var result = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
message.channel.sendMessage (result);
} //rolls a d6
if(commandIs("rolld10", message)){
var result = Math.floor(Math.random() * ((10 - 1) + 1) + 1);
message.channel.sendMessage (result);
} //rolls a d10
if(commandIs("rolld20", message)){
var result = Math.floor(Math.random() * ((20 - 1) + 1) + 1);
message.channel.sendMessage (result);
} //rolls a d20
if(commandIs("rolld100", message)){
var result = Math.floor(Math.random() * ((100 - 1) + 1) + 1);
message.channel.sendMessage (result);
} //rolls a d100
})
client.login('<Discord bot token>'); //Bot token so it can login to Discord
The issue I have is when I send '!rolld100', the bot answers as if I sent '!rolld10' and '!rolld100' at the same time since 10 is in 100.
Any possible fixes? I'm also new to javascript so if you could explain what your solution does it would help me a lot.
The code seems to be a little complicated with duplicated code. You can simplify this down to a few lines of code by using a regular expression
const Discord = require('discord.js');
const client = new Discord.Client();
client.on ('message', message => {
const rollMatch = message.match(/roll(\d+)\s/)
if (rollMatch) {
const sides = Number(rollMatch)
var result = Math.floor(Math.random() * sides + 1);
message.channel.sendMessage (result);
}
})
client.login('<Discord bot token>'); //Bot token so it can login to Discord
Now if you want to do it your way. You basically would need to do
if(commandIs("rolld100", message)){ }
else if(commandIs("rolld10", message)){ }
else if(commandIs("rolld20", message)){ }
else if(commandIs("rolld6", message)){ }
I google for a solution that allows me to generate a random number between 1 and 6. i found this: http://www.w3schools.com/jsref/jsref_random.asp
Now i tryed to use it in my code:
$scope.getUserImage = function (user) {
if (user.avatar) {
return user.avatar;
} else {
return '/images/icons/user-' + user.gender((Math.random() * 6) + 1) + '.svg';
}
};
but with this i get a error, it means that the user.gender isnt any function. I know this is a realy very basic question. I started new with JavaScript. I am a Designer normaly.
Since you want to add the strings together (called 'concatenate' since we're talking about strings), separate each with a + symbol. But the 1-6 logic is also wrong, you'll need to round those numbers down.
$scope.getUserImage = function (user) {
if (user.avatar) {
return user.avatar;
} else {
var randomNumberOneToSix = Math.floor(Math.random() * 6 + 1);
return '/images/icons/user-' + user.gender + randomNumberOneToSix + '.svg';
}
};
Bonus tip: w3schools is not a great resource. Add 'mdn' to every search and use Mozilla's excellent documentation for learning about web technologies.
You miss a "+" to concat gender change this line
return '/images/icons/user-' + user.gender((Math.random() * 6) + 1) + '.svg';
to
return '/images/icons/user-' + user.gender + ((Math.random() * 6) + 1) + '.svg';