so I created small bot for Discord in Java but I want to use it on multiple Discord Servers but thats not possible because of Role ID so how could I bypass role ID? Thank you
const Role = Role ID
})
bot.on('message', message=>{
if(message.author.bot) return;
if(message.content === '$register'){
message.member.roles.add(Registred);
message.channel.send('Congrats.You are now registered!');
}
});
Related
How would I check what servers my discord bot is in, code wise in discordjs.
And able to join those servers.
You can view the guilds (servers) your bot is in with <Client>#guilds. Example:
const { Client, GatewayIntentBits } = require('discord.js');
const bot = new Client({ intents: [GatewayIntentBits.Guilds] });
console.log(bot.guilds.cache.map(guild => guild.id));
bot.login('YOUR_TOKEN');
This will return an array of the ids of the guilds your bot is a member of.
I'm using the Get Current User Guild Member method of Discord API.
I successfully get back information about user such as their role in a specific guild
roles: ['705175621399216228']
How can I go from the role ID to the actual name of the role?
https://discord.com/developers/docs/resources/guild#get-guild-roles
You can get all the role objects of the guild and filter the roles with the ID of the role you require.
You can use node-fetch, etc to get the roles with something like this
const fetch = require("node-fetch")
const data = await fetch("{base.url}/guilds/{guild.id}/roles")
const roleid = "xxxxxx"
const role = data.find(rl => rl.id === roleid)
I'm developing an unban command for a ban command.
When the ban is lifted, I want the embed message unbanEmbeduserside to be sent to the banned user via private message. This embed message will let the banned user know about it.
The codes for my unban command are as follows. Thanks in advance to my friends who will help me with how to write a code so that I can perform the operation I mentioned in the above article.
async run (bot, message, args) {
message.delete(message.author);
if (!message.member.hasPermission("BAN_MEMBERS"))
return message.author.send("Buna yetkin yok.")
let unbanEmbednotfoundmessage = new discord.MessageEmbed()
.setColor(0xdb2727)
.setDescription(`Kullanıcı bulunamadı veya yasaklı değil.`);
let userID = args[0]
message.guild.fetchBans().then(bans => {
if(bans.size == 0) return message.channel.send(unbanEmbednotfoundmessage);
let bUser = bans.find(b => b.user.id == userID)
if(!bUser) return message.channel.send(unbanEmbednotfoundmessage)
let unbanEmbeduserside = new discord.MessageEmbed()
.setAuthor('YASAKLAMA KALDIRILDI', 'https://i.hizliresim.com/midfo22.jpg')
.setDescription(`
*Yasaklamanız kaldırıldı, sunucuya tekrar katılabilirsiniz!*
Yasaklamayı Kaldıran Yetkili: **${message.author.username}**
`)
.setColor(0xc8b280)
.setTimestamp()
.setFooter('gtaplus Multiplayer Community');
let unbanEmbedserverside = new discord.MessageEmbed()
.setAuthor('YASAKLAMA KALDIRMA', 'https://i.hizliresim.com/midfo22.jpg')
.setDescription(`
Yasağı Kaldırılan Kullanıcı: **${userID}**
Yasaklamayı Kaldıran Yetkili: **${message.author.username}**
`)
.setColor(0xc8b280)
.setTimestamp()
.setFooter('gtaplus Multiplayer Community');
message.guild.members.unban(bUser.user).then(() => message.channel.send(`**${userID}** yasaklaması kaldırıldı.`));
if (unbanEmbedserverside){
const log = message.guild.channels.cache.find(channel => channel.name === 'server-log')
log.send(unbanEmbedserverside);
}
})
}}
In Discord, any user, including a bot, will need to be in a server with a user to send them direct messages. This, unfortunately, means that unless the bot shares a server with the banned user, you will not be able to send the message. If the bot does share another server, the answer that Skularun Mrusal provided in the comments of your post should work.
I am already using this "guildMemberAdd" for the members recently added to join "Visitors" role.
client.on("guildMemberAdd", member =>{
member.roles.add(member.guild.roles.cache.find(role => role.name == "Visitors"), "auto added.");
})
My bot can't be online 24h. So when I connect it, there are some users who joined meanwhile the bot was offline, obviously with no role (I guess they are in #everyone role).
My intention is to move discord users with no role to an existing role when I connect the Bot.
In your ready event, just fetch every member in the guild with GuildMemberManager#fetch(), and then filter through members with only one role (#everyone).
client.on('ready', async () => {
const members = await client.guilds.cache.get('Guild ID').members.fetch(); // fetch every member
members
.filter((m) => m.roles.cache.size === 1) // iterate through every member with only one role
.each((m) => m.roles.add('Role ID')); // add the Visitors role
});
I'm trying to make a discord.js bot for my server I made for people in my school. I'm trying to make a #classes channel and if you react to certain messages it gives you a role (which gives you access to the text channel for that class).
client.on('messageReactionAdd', (reaction, user) => {
console.log("client.on completed.")
if (message.channel.name === 'classes') {
console.log("if(message) completed.")
if (reaction.emoji.name === "reminder_ribbon") {
console.log("emoji test completed.")
const guildMember = reaction.message.guild.members.get(user.id);
const role = reaction.message.guild.roles.find(role => role.name === "FACS");
guildMember.addRole(role);
}
}
});
This is what I have tried so far, however, it does not give me/the other people reacted to it the role nor does it return an error message.
P.S. Also, how would I be able to make it so when they unreact it removes the role?
Edit: It seems it only gets reactions from cached messages/messages sent after bot startup. Also, message is not defined on the first if(message.channel.id) message.
Try to use the following code:
client.on('messageReactionAdd', (reaction, user) => {
if (reaction.message.channel.id === '552708152232116224') {
if (reaction.emoji.name === "reminder_ribbon") {
const guildMember = reaction.message.guild.members.get(user.id);
const role = reaction.message.guild.roles.get('552709290427940875');
guildMember.addRole(role);
}
}
});
First of all, reaction.users is an Object with all the users that reacted on the message, so you first have to define to which user you want to assign the role. I fixed this fetching the guildMember with user.id.
The second mistake was that you tried to assign a role ID to a guildMember although you first have to fetch the role and then assign the role Object to the guildMember.