Discord.js v12 make Rainbow Role - discord

I want to make a rainbow role(changes all 60seconds the color).
Is this possible?
I have a general understanding of discord.js.
Thanks allot dor trying to help.

I created a tutorial in german on YouTube. If you understand german and want to exectly understand what the code does, check it out here!
Here is a basic code for a rainbow role, you need to adjust some things to work for you.
client.on('ready' , async() =>{
const guild = client.guilds.cache.get('///Your guild ID');
colors = ['///Enter a list of hex codes here'];
var role = guild.roles.cache.get('///Your Rainbow Role ID here!')
setInterval(() => {
const roleCount = guild.roles.cache.get(role).members.size;
if(roleCount >= 1){
var random = Math.floor(Math.random() * colors.length);
role.edit({
color: colors[random]
})
console.log('Rainbow Color changed, it is now: ' + colors[random])
}
else{
console.log('No user with rainbow role')
}
}, 600*1000)
}

Related

Discord.js autorole from username

I want to make my bot autorole if username of anyone member contains a specific tag and the code i made isn't working, bot starts fine but didn't assign role if user have tag in username than also so i thought to use stackoverflow to get some help
Here is the code:
client.on("userUpdate", async (oldUser, newUser) => {
if (oldUser.username !== newUser.username) {
let tag = "⚡";
let server_id = "793143091350470708";
let role = "888436049620119562";
if (newUser.username.includes(tag) && !client.guilds.get(server_id).members.get(newUser.id).roles.has(role)) {
client.guilds.get(server_id).members.get(newUser.id).addRole(role)
} if (!newUser.username.includes(tag) && client.guilds.get(server_id).members.get(newUser.id).roles.has(role)) {
client.guilds.get(server_id).members.get(newUser.id).removeRole(role)
}
}
})
You would require to define USER_UPDATE, GUILD_MEMBER_UPDATE, and
PRESENCE_UPDATE intents if you are on discord.js version 13 and further enable the members and presence intent too, they can be located in the Discord Developer Portal further I would like to suggest using the Client#guildMemberUpdate listener instead since your code has nothing to do with presence you would not need the "extra" intents.
Additional Information
Directed by this comment It has come to my notice that you are using version 12 of discord.js so you would want to make adequate changes to your code for that ( your code as it currently is, is clearly written for discord.js v11) the following changes would be made in your code:
client.on("guildMemberUpdate", async (oldUser, newUser) => {
if (oldUser.username !== newUser.username) {
let tag = "⚡";
let server_id = "793143091350470708";
let role = "888436049620119562";
if (newUser.username.includes(tag) && !client.guilds.cache.get(server_id).members.cache.get(newUser.id).roles.has(role)) {
client.guilds.cache.get(server_id).members.cache.get(newUser.id).roles.add(role)
} if (!newUser.username.includes(tag) && client.guilds.cache.get(server_id).members.cache.get(newUser.id).roles.has(role)) {
client.guilds.cache.get(server_id).members.cache.get(newUser.id).roles.remove(role)
}
}
})

avatarURL has a strange issue with Users

So when trying to make my Discord.js bot display an User Avatar, it works with other bots but not with actual users, I have this problem on all commands that use user.avatarURL
This is the code I am using
const user = message.mentions.users.first();
if(message.content.toLowerCase().startsWith(`${prefix}av`)){
let member = message.mentions.members.first();
if(member){
const emb=new Discord.MessageEmbed()
emb.setAuthor(`${user.username}'s avatar`, user.avatarURL())
emb.setImage(user.avatarURL());
message.channel.send(emb)
}
else{
message.channel.send("Sorry none found with that name")
}
You should use displayAvatarURL instead, with the optional dynamic option.
emb.setAuthor(`Text`, user.displayAvatarURL({ dynamic: true }));
https://discord.js.org/#/docs/main/stable/class/User?scrollTo=displayAvatarURL

How do I display local image with discord.js message embed?

I'm working on semi-gacha bot command where you can pull characters and so on. I want to display the characters image after the user pulls and here is where I get stuck, it just doesn't display anything and I found what looked like the answer on here, but it didn't help, as I would get the same result and I don't get any errors, so I don't know what exactly is wrong. I have tried printing out the result of MessageAttachment first:
const attachment = new Discord.MessageAttachment('./chars/1.Jakku.png', '1.Jakku.png');
console.log(attachment);
and I get: undefined, anybody has any ideas of what am I doing wrong? And yes, I have discord.js library imported.
Relevant part of the code:
collector.on('collect', reaction => {
//new embd
const newEmbd = new Discord.MessageEmbed();
// gacha logic
if (reaction.emoji.name === '✅') {
const values = Object.values(chars);
const randomChar = values[parseInt(Math.floor(Math.random()*values.length))];
const attachment = new Discord.MessageAttachment('./chars/1.Jakku.png', '1.Jakku.png');
const charData = randomChar.split('.');
newEmbd
.setTitle(`You pulled: ${charData[1]}!`)
.setColor('YELLOW')
.attachFiles(attachment)
.setImage(`attachment://1.Jakku.png`);
embdReact.edit(newEmbd);
pulled = true;
} else if (reaction.emoji.name === '❌') {
newEmbd
.setTitle('Time for gacha!')
.setColor('YELLOW')
.addFields(
{ name: 'Result:', value: 'You decided against pulling' }
);
embdReact.edit(newEmbd);
};
});
You need to use the correct syntax for attaching files as per this guide
const exampleEmbed = new Discord.MessageEmbed()
.setTitle('Some title')
.attachFiles(['./chars/1.Jakku.png'])
message.channel.send(exampleEmbed);

how to make your bot send more than one direct embedded messages on discord

I'm new to coding and to be honest sometimes i don't know what i am doing, i try looking for videos online but i cant find the right answer or i don't get what they are trying to say.I hope someone can tell me what to do or what i am doing wrong in this coding.
const Discord = require("discord.js");
const colors = require("./colors.json");
const bot = new Discord.Client({disableEveryone: true});
const token = 'my token';
const PREFIX = '!';
bot.on("ready", async () => {
console.log(`${bot.user.username} is online!`)
bot.user.setActivity("Live chat", {type: "HELPING"});
})
bot.on("message", async message => {
let args = message.content.substring(PREFIX.length).split(" ");
if (message.author.bot) return;
if (message.content.startsWith ("!Help") || (message.content.startsWith ("!help"))){
let uEmbed = new Discord.MessageEmbed()
.setColor(colors.blue)
.setTitle("**Hello there Human how can i help you today?**")
.addField('**1. If you need help or have an issue with a purchase please type**', '**Purchase**')
.addField('**2. If you need help or have an issue with Impulse mod please type**', '**Impulse**')
.addField('**3. If you need help or have an issue with GTA V Game key please type**', '**GTA V**')
.addField('**4. If you need help or have an issue with Spotify Premium Key type**', '**Spotify**')
.setTimestamp()
.setFooter(`Live chat bot | At your service`, bot.user.defaultAvatarURL)
message.author.send({embed: uEmbed})
}
if (message.content.startsWith ("Purchase") || (message.content.startsWith ("purchase"))){
let uEmbed = new Discord.MessageEmbed()
.setColor(colors.aqua)
.setTitle("**Our bot is not ready for this command**")
.addfiled ("please creat a ticket on the **#『🎫』support-ticket chennel** in the Knight-Shop Server and staff will be in touch with you shortly.")
.setTimestamp()
.setFooter(`Live chat bot | At your service`, bot.user.defaultAvatarURL)
message.author.send({embed: uEmbed});
}
})
bot.login(token);
(node:3864) UnhandledPromiseRejectionWarning: TypeError: (intermediate value).setColor(...).setTitle(...).addfiled is not a function
In general you should learn the base language first before using a framework / API, in this case you made a simple typo, addfiled instead of addField like you did above

How could I send a message to a user that had note for my bot?

I had set up the webhooks correctly for dblapi.js and I need help with sending a message to a user.
My code:
dbl.webhook.on("vote", vote => {
let { message } = require("discord.js");
let voted = vote.user.id;
voted.send("thanks for voting")
});
Will this work? If not, can you show me the proper way to do it? Thanks a lot!
Taken from the DBL Api Docs:
So we first have to get the User Object and then send the dm. vote.user.id will not work as vote.user already is the ID.
Here is how it can and should be done.
dbl.webhook.on("vote", vote => {
console.log('User with ID ' + vote.user + ' voted!');
const user = client.users.get(vote.user); // This will get the User Object from the Client#users Collection
if(user) { // This checks if the Bot knows who the User is.
user.send('Thank you for voting!'); // DM the User "Thank you for voting!"
}
});
Something like that should work, got if from here.
dbl.webhook.on('vote', vote => {vote.user.send("thanks for voting")});

Resources