Discordjs bot embed gifs - javascript

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)

Related

How to make an discord js bot to send an random message(i make an list and he sends it) at some time in an exact text channel from discord

This is how an comand works
And this is my main.js
an little help please?i really need it and i would apreciatte much if you would help me
First of all you need to find the channel ID. Best to go into the Discord app and right click the channel and select "Copy ID". It should look something like this: 845346073543326453
Now to send something to that specific channel you have to do this:
const channel = client.channels.cache.get(845346073543326453);
channel.send("hello!")
For the random messages you just create an array and randomly pick one:
const random = (min, max) => {
return Math.floor(Math.random() * (max - min + 1) + min);
}
let randomMsg = [`Howdy`, `Howdily doodily`, `Zoinks`]
channel.send(quotes[random(0, quotes.length - 1)])
To send it at a specific time there's many methods. I recommend using the cron package and reference this post: How can I send a message every day at a specific hour?
But if you just want a quick and really low effort way you could just use setInterval() and set the delay to an hour. So we end up with something like this:
const channel = client.channels.cache.get(845346073543326453);
const randomMsg = [`Howdy`, `Howdily doodily`, `Zoinks`]
const random = (min, max) => {
return Math.floor(Math.random() * (max - min + 1) + min);
}
const sendRandomMsg = () => {
var d = new Date();
var n = d.getHours();
if (n === 12) {
channel.send(randomMsg[random(0, quotes.length - 1)])
}
}
setInterval(function(){ sendRandomMsg() }, 3600000);
You can add more functions into the if, in case you have more functions to run at specific times.

Discord Bot: How to check if the client message is the correct answer?

I'm trying to build a simple math game and it works fine, but I can't check if the answer someone gives to the math problem is correct or not.
I made an if statement to see if the answer matches the message content:
if (msg.content == answer) {
msg.reply('correct');
}
The problem is that msg.content only accepts a string, not an integer. Can anyone help me fix that issue?
Here is the full code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', (msg) => {
var minimum = 1;
var maximum = 100;
var int1 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
var int2 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
if (msg.author.bot) return;
//step 1
if (msg.content === 'mathplus') {
msg.reply(`hi what is ${int1} + ${int2}`);
var answer = int1 + int2;
console.log(answer);
}
//check if answer is correct -- where the problem is
if (msg.content == answer) {
msg.reply('correct');
}
});
client.login(process.env.TOKEN);
The problem is NOT that the msg.content is not an integer. You're correctly using double equals here (and 5 == '5'). The problem is that answer is no longer the sum of int1 and int2, it's undefined. When you use the mathplus command, you define the answer but if you send a new message with the answer, it's no longer available.
Check out the example below:
function test(command) {
if (command === 'mathplus') {
var answer = 5
console.log(`"mathplus" command. The answer is ${answer}`)
}
if (command == answer) {
console.log('correct')
}
console.log({
answer,
command
})
}
test('mathplus')
test('5')
As Radnerus mentioned in their comment, you can use message collectors to wait for an answer from the user. I've added an example below how you could use it with lots of comments:
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '!';
// helper function to get a number between min and max
function randomInt(min, max) {
if (min > max) [min, max] = [max, min];
return Math.floor(Math.random() * (max - min + 1) + min);
}
client.on('message', async (message) => {
if (message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'mathplus') {
const int1 = randomInt(0, 100);
const int2 = randomInt(0, 100);
const answer = int1 + int2;
// we only wait for 30s for an answer
const maxWait = 30000; // in ms
const embed = new Discord.MessageEmbed()
.setColor('#f8cf4d')
.setTitle(`Hey ${message.author.username}! What is ${int1} + ${int2}? 🙈`);
await message.channel.send(embed);
// filter checks if the response is from the same author who typed the command
const filter = (response) => response.author.id === message.author.id;
const collector = message.channel.createMessageCollector(filter, {
// set up the max wait time the collector runs
time: maxWait,
});
// fires when a response is collected
collector.on('collect', (response) => {
if (parseInt(response.content, 10) === answer) {
message.channel.send(
`🎉🎉🎉 Woohoo, ${response.author}! 🎉🎉🎉\n\nYou're a maths genius, the correct answer was \`${answer}\`.`,
);
// the answer is correct, so stop this collector and emit the "end" event
collector.stop();
} else {
// give the user another chance if the response is incorrect
message.channel.send(
`Oh, ${response.author}, \`${response.content}\` is not correct... 🙊\nDo you want to try again?`,
);
}
});
// fires when the collector is finished collecting
collector.on('end', (collected, reason) => {
// only send a message when the "end" event fires because of timeout
if (reason !== 'time') return;
// if there are incorrect answers
if (collected.size > 0) {
return message.channel.send(
`Ah, ${message.author}. Out of ${collected.size} guess${
collected.size > 1 ? 'es' : ''
} you couldn't find the number \`${answer}\`. I'm not saying you're slow, but no more answers are accepted.`,
);
}
// if the user haven't submitted any answer, let's get a bit more aggressive
return message.channel.send(
`Okay, ${message.author}, I'm bored and I can't wait any longer. No more answers are accepted. At least you could have tried...`,
);
});
}
});
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.login(process.env.TOKEN);
The result:
You might try this approach
if(!parseInt(message.content)){ // If the message wasn't actually a number
return message.reply("You can only answer numbers");
}
if(parseInt(message.content) === answer){ // Correct answer
return message.reply("Correct answer");
}else{ // Wrong answer
return message.reply("Oops");
}
parseInt() converts string to integers. So parseInt("10.103") would return 10. If you need to work with floating numbers try parseFloat()
Use these resources to learn more about these functions
Resources
parseInt()
parseFloat()

Discord bot send messages specific channel error

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"]})

Discord Random Image Bot spamming

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.

How can I avoid sending two commands at once?

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)){ }

Categories

Resources