How would I make my bot react with random emoji? - bots

I use Discord.js, here's my code so far;
if (message.content === '..react') {
message.react('😀')
message.react('😂')
message.react('🤣')
message.react('😎')
message.react('🙂')
message.react('😣')
message.react('🙄')
message.react('😥')
message.react('😪')
message.react('🤐')
message.react('😛')
const embed = new RichEmbed()
.setDescription('I just reacted with diffrent emojies! React to your favorite!')
.setColor(0x1ED5E7)
message.channel.send(embed);
}
});
I want it to react with any emojis that's available, without going over 11 reactions. Help would be greatly appreciated.

If you want to react random emojies you can get all client emoji.id , shuffle it, then slice and react in array map. I do not know methods to get standard unicode emoji lists in Discord, so maybe you need to great all unnciode emoji array and then concat its with emojilist array.
React with 11 random available client emoji you can somethink like this
if (message.content === '..react') {
let emojiList = bot.emojis.map(emoji => emoji.id)
let randTenEmojies = shuffle(emojiList).splice(0,11)
randTenEmojies.map(emoji => message.react(emoji))
const embed = new RichEmbed()
.setDescription('I just reacted with diffrent emojies! React to your favorite!')
.setColor(0x1ED5E7)
message.channel.send(embed);
}
});
const shuffle = (arr) => {
var j, temp;
for(var i = arr.length - 1; i > 0; i--){
j = Math.floor(Math.random()*(i + 1));
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
return arr;
}

Related

Undefined username in leaderboard command [ quick.db / discord.js ]

I've tried making a leaderboard command with quick.db for my Discord.js bot but it isnt working.
It replies with undefined users and stuff like that!
var money = await db.all(`money_${message.guild.id}`, { sort: ".data" });
let resp = "";
for (let i = 0; i < money.length; i++) {
let user = client.users.cache.get(money[i].ID.split("_")[1]);
resp += `${i + 1}. ${user} - ${money[i].data}$\n`;
}
return message.channel.send(
new Discord.MessageEmbed().setAuthor(`Leaderboard`).setDescription(resp)
);
Here is the code ^^^^
Here is the output for the command
It means that money[i].ID.split("_")[1] is not a valid ID. Have you thought about logging these values in the loop to check the validity of the data you store?

.permission.has() discord.js v12 - Cannot read property 'permissions' of undefined

I'm having trouble coding my bot for people who have MANAGE_MESSAGES to be able to say 'badword'. Here is my code:
const Discord = require('discord.js')
const bot = new Discord.Client()
const token = process.env.DISCORD_TOKEN
let set = new Set(['badword'])
bot.on('message', msg => {
if (msg.author.bot) {
return
}
let wordArray = msg.content.split(' ')
if (!msg.member.hasPermission('MANAGE_MESSAGES')) {
for(var i = 0; i < wordArray.length; i++) {
if(set.has(wordArray[i].toLowerCase())) {
msg.delete()
msg.channel.send(`${msg.author.username}, you said a bad word.`)
break
}
console.log('message filtered')
}
}
})
bot.login(token)
The .permissions.has() is not working as according to this website https://discordjs.guide/additional-info/changes-in-v12.html#permissions-haspermission-s, it says that .hasPermission() / .hasPermissions() is now completely removed from v11 and added permissions.has(). Yes, my bot is upgraded to v12 so please help.
The correct way to use it is .hasPermission('MANAGE_MESSAGES')
if (!msg.member.hasPermission('MANAGE_MESSAGES'))
You're using the GuildMember class which uses the hasPermission() method. You were looking at the permission class.
https://discord.js.org/#/docs/main/stable/class/GuildMember

is there a way to make my code non case sensitive

I'm trying to make a bot able to read an input automatically, no need for a command and then check the input against a list of defined words. I have managed to get this working but i'm having trouble in such a way that if anything varies from the word on the list, the bot ignores it. Code is as follows, all help is appreciated:
bot.on('message', message => {
for (i = 0; i < blacklist.length; i++)
{
//var check = blacklist[i].trim();
if (blacklist[i].trim().length > 0){
var lookup = '\\b'+blacklist[i]+'\\b';
var re = new RegExp(lookup);
var foundpos = message.content.search(re)
if(foundpos >=0) {
message.delete();
break;
}}}
for(i in blacklist) {
console.log(blacklist[i]);
}
})

Nodejs Webdav read multiple files (Problem with Array)

I am starting using NodeJs and webdav, I am using websockets to receive a list with the names of the images that I need to send to my app and then I have a for loop to send them to my app, but for some reason some of the images are undefined.
I am starting using NodeJs and webdav so I have no idea what's wrong.
for(let i = 0; i < ImagemList.length; i++){
wfs.readFile(ImagemList[i], "binary", function(err, data) {
Imagens[i] = data;
if(Imagens.length == ImagemList.length){
socket.emit("ImagemPost", Imagens);
}
});
}
For some reason I can't acess the variable "i" and for some reason the data was going to a random place leaving empty spots.
I've updated the code and it still goes to random places (don't know why) but doesn't leave any empty spot.
My array is still random if someone can help me.
Imagem = Imagem.replace("[", "");
Imagem = Imagem.replace("]", "");
let ImagemList = Imagem.split(", ");
let Imagens = [];
let contador = 0;
for(let o = 0; o < ImagemList.length; o++){
wfs.readFile(ImagemList[o], "binary", function(err, data) {
Imagens[contador] = (data);
contador++;
if(Imagens.length == ImagemList.length){
socket.emit("ImagemPost", Imagens);
}
});
}

How to make a invites command (Discord.js)

Hello, I am currently wondering how to make a invites command for my discord bot.
How it would work will be I use n!invites and it comes with
You have (num) invites.
I would like it to be the amount of invites the user has gained.
If I could get some help here that would be appreciated. Thanks.
Note I have looked at the the discord.js website and there is a place for it but I never understood.
Here is a code I found on another question
client.on('message', message => {
if(message.content === "n!invites"){
var user = message.author;
message.guild.fetchInvites()
.then
(invites =>
{
const userInvites = invites.array().filter(o => o.inviter.id === user.id);
var userInviteCount = 0;
for(var i=0; i < userInvites.length; i++)
{
var invite = userInvites[i];
userInviteCount += invite['uses'];
}
message.reply(`You have ${userInviteCount} invites.`);
}
)
}
});
It gets every invite links from the guild using
message.guild.fetchInvites()
and finds an invite link that was created by the user which is the author, in this case, using a filter
const userInvites = invites.array().filter(o => o.inviter.id === user.id);
Using a for loop, it adds the amount of invites the user has until the invites the user has matches it.
var userInviteCount = 0;
for(var i=0; i < userInvites.length; i++)
{
var invite = userInvites[i];
userInviteCount += invite['uses'];
}
Then it adds the amount of uses to the userInviteCount statement which will be 0 if there is no invite found made by the user or the user has not invited anybody to the server.
Finally is sends a reply with the amount of invites the user has
message.reply(`You have ${userInviteCount} invites.`);
It is in a Template String so you don't have to put + but instead put ${expression} which is userInviteCount, so the amount of invites the user has.
I believe the command you're looking for would look something like this:
if (message.content === 'n!invites') {
var userId = message.author.id;
var userInvites = message.guild.fetchInvites().then(invites => invites.find(invite => invite.inviter.id === userId));
var useAmount = userInvites.uses;
if (useAmount === undefined) {
message.channel.send(`${message.author.username} has 0 invites`);
}
else {
message.channel.send(`${message.author.username} has ${useAmount} invites`);
}
}

Resources