Image not refreshing (discord.js) - javascript

So basically I'm trying to create a Discord bot that when you type "!cat" the Discord bot sends an image of a random cat inside of an embed but every time I use the command the image stays the same.... I tried to put the embed variable inside of the function so it refreshes every time someone says "!cat" but it didn't work..... this is the code that I'm using:
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Ready!');
});
client.on('message', message => {
console.log(message.content);
if (message.content === "!cat") {
const catEmbed = new Discord.MessageEmbed()
.setTitle("MEOW!")
.setImage("http://theoldreader.com/kittens/600/400")
.setThumbnail("http://theoldreader.com/kittens/600/400")
message.channel.send(catEmbed)
}
});
client.login('NOT GONNA TELL MY TOKEN');

It looks like a caching issue. Try to append some random character as a query string to your URL. Something like this should work:
client.on('message', (message) => {
if (message.content === '!cat') {
// generate a random string
const rand = Math.random().toString(36).slice(2);
const catEmbed = new Discord.MessageEmbed()
.setTitle('MEOW!')
// append it as a query string
.setImage(`http://theoldreader.com/kittens/600/400?${rand}`)
.setThumbnail(`http://theoldreader.com/kittens/600/400?${rand}`);
message.channel.send(catEmbed);
}
});

Related

Send a message to my private channel on join & leave

Heyo,
I want my bot to send a embed message to my private discord server when it joins & leaves a server. But the problem is that it does not send anything anywhere. My code looks like this:
exports.run = async (client, guild) => {
if(!guild.available) return
if(!guild.owner && guild.ownerID) await guild.members.fetch(guild.ownerID);
if(!channel) return;
const embed = new MessageEmbed()
.setTitle(`Bot joined a server`)
.setDescription(`${guild.name}`)
.setColor(0x9590EE)
.setThumbnail(guild.iconURL())
.addField(`Owner", "${guild.owner.user.tag}`)
.addField(`Member Count", "${guild.memberCount}`)
.setFooter(`${guild.id}`)
client.channels.cache.get('ID').send(embed)
}
Your code doesn't activate upon joining the server. For that you have a nice event (that has a misleading name) guildCreate - it is emitted whenever the client joins a guild.
So, your code should look something like this
client.on('guildCreate', async guild => {
let YourChannel = await client.channels.fetch('channelid');
const embed = new Discord.MessageEmbed()
.setTitle(`Bot joined a server`)
.setDescription(`${guild.name}`)
.setColor(0x9590EE)
.setThumbnail(guild.iconURL())
.addField(`Owner`, `${guild.owner.user.tag}`)
.addField(`Member Count`, `${guild.memberCount}`)
.setFooter(`${guild.id}`)
YourChannel.send(embed);
});
Same works for leaving the guild, use guildDelete event.

Sorting through html with javascript

I am making a discord bot in javascript. My intent is to visit https://api.chess.com/pub/player/edisonst/stats and somehow parse only the chess_daily last rating, chess_rapid last rating, chess_bullet last rating, chess_blitz last rating, etc. I do not know how to choose only those elements.
Here is my existing code.
const discord = require('discord.js')
const fetch = require('node-fetch');
const client = new discord.Client();
const prefix = '!';
client.once('ready', () =>{
console.log('Console is online');
});
client.on('message', async message => {
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length);
let url = "https://api.chess.com/pub/player/" + args + "stats";
})
In my last line of code I arrive at the site that I wish to get the information from, but I don't know how to get just those bits. Thank you for any and all help that I may receive.
You can use jsdom to "render" page that you get and manipulate it as you would in browser. This way you can select elements and get their contents.

generalChannel.send is not a function

I'm creating a discord bot, and I am trying to make it so that it would greet everyone when it turns on. I have been using bot.channels.get to find the channel and so far that part of the code works fine. It stops working when it tries to send a message.
I have tried all combinations of bot.channels.get and bot.channels.find, together with generalChannel.send and generalChannel.sendMessage, but to still no avail.
const Discord = require('discord.js');
const fs = require('fs');
const bot = new Discord.Client();
let rawVariables = fs.readFileSync('variables.json');
let variables = JSON.parse(rawVariables);
var generalChannel;
bot.on('ready', () => {
generalChannel = bot.channels.get(variables.Channels.general);
generalChannel.send("Helllo!");
});
bot.login(variables.BotToken);
I just need it to message the channel when it starts up.
variables.Channels.general should be a string of the id of your channel
you can get the id by rightclicking on a channel -> copy id
it should look something like this:
'408632672669273'

"Rich is not defined" when trying to build an embed

I made so code to build an embed for my Discord bot, but it doesn't seem to work:
client.on('message', message => {
// Ignore messages that aren't from a guild
if (!message.guild) return;
if (message.content.startsWith('$Commands')) {
let Embed = new Rich.Embed;
let Message = message.guild;
let botEmbed = new Discord.RichEmbed()
.setTitle("Commands From TTB")
.setDescription("**Commands :**")
.setColor("#4dff077")
.setAuthorName("Galak$y#3038")
.setAvatar("https://cdn.discordapp.com/avatars/563449221701959700/8386d5fe48d71898c40244e7a5a66d58.png")
.addField("1. *Ban* = *Bans User Mentioned After Command* ``$ban <Mention User>``")
.addField("2. Kick = Kicks User Mentioned After Command ``$kick <Mention User>``")
.addField("3. what is myavatar = ``$what is my avatar``")
.addFooter("New Commands Coming Soon...")
return;
message.channel.send(botEmbed)
}
});
Here is the error:
Embeds are created using the RichEmbed class constructor like so:
let embed = new Discord.RichEmbed()
When you declare Embed you're using Rich.Embed, and as it says, Rich is undefined.

DeprecationWarning: Collection#find: pass a function instead

I'm quite a newbie to node.js and I'm currently using discord.js to make a Discord bot. As soon as any bot command gets used the console prints a DeprecationWarning.
for example:
(node:15656) DeprecationWarning: Collection#find: pass a function instead
(node:15656) sometimes is another number, nearly always changing.
This is what my code looks like (only one command, I've got multiple, I get this error with all of them though):
const botconfig = require("./botconfig.json")
const Discord = require("discord.js");
const bot = new Discord.Client();
bot.on("ready", () => {
console.log(`Launched ${bot.user.username}...`);
bot.user.setActivity("Games", { type: "PLAYING" });
});
bot.on("message", async message => {
if (message.author.bot) return;
let prefix = botconfig.prefix;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = messageArray.slice(1);
let botico = bot.user.displayAvatarURL;
if (cmd == `${prefix}help`) {
let helpEmbed = new Discord.RichEmbed()
.addField(".kick", "kick a user", true)
.addField(".ban", "ban a user", true)
.addField(".unban", "unbans a user", true)
.addField(".mute", "mutes a user over a period of time", true)
.setColor("#005b5f")
.setThumbnail(botico);
message.channel.send(helpEmbed);
console.log(`command used: help`);
};
});
bot.login(botconfig.token)
It is in one of your other commands. You more than likely are using something like #Collection.find('name', 'keyname') in one of the other commands.
This has been updated to #Collection.find(x => x.name === "name").
Like it says in the error. #Collection.find() requires a function instead. So use one and the error goes away.

Categories

Resources