Server join log displaying "${guild.name}" instead of the guild name - discord.js

I am trying to log when my bot is added to servers, but the code I have just displays "New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!" in chat instead of the guild name, guild id, and guild members. Does anyone know how to fix it?
client.on("guildCreate", guild => {
client.channels.cache.get('774529558044344333').send('New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!');
})```

You are using ordinary strings (with normal quotation signs) instead of literal templates (uses ` sign). Replace double-quotes like that:
client.on("guildCreate", guild => {
client.channels.cache.get('774529558044344333').send(`New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`);
})

Related

Discord API - Get Discord role name from role ID

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)

How to define guild in discord.js?

I am trying to make a command were it creates a role but gives me the error saying that guild is not defined:
guild.roles.create({
^
ReferenceError: guild is not defined
while this is my code:
guild.roles.create({
data: {
name: 'Boots',
color: 'BLUE',
},
reason: 'very cool',
})
.then(console.log)
.catch(console.error);
let role = message.guild.roles.find(r => r.name === "Boots");
member.roles.add(role)
message.channel.bulkDelete(1);
message.channel.send('Mhm, very cool');
You have to use
message.guild.roles [...]
Because if you type
guild.roles [...]
the program wants to access the roles property of the variable guild
Did you define guild? If you want to use guild, just write something like const guild = message.guild;

Getting nicknames of users of my Discord bot

I can see the names, ids and user numbers of the servers where my bot is located, but how can I get the list of users (nicknames)?
You can make use of guild.members.fetch() in order to get all members and then use the nickname property to receive their nicknames. Finally I removed all bots with a simple filter.
const members = (await message.guild.members.fetch())
.filter((m) => !m.user.bot)
.map((m) => m.displayName);
console.log(members);
Working example as a command:
client.on("message", async (message) => {
if (message.author.bot) return;
if (message.content === "!list") {
const members = (await message.guild.members.fetch())
.filter((m) => !m.user.bot)
.map((m) => m.displayName);
console.log(members);
}
});
client.login("your-token");
Thanks to #MrMythical who suggested using displayName only. That property automatically returns the normal username when no nickname has been set for a user.
Users do not have nicknames, only guild members, if you are trying to fetch a list of server member nicknames.
You can use the code snippet from below:
const map = message.guild.members.cache.filter(c=> !c.member.user.bot).map(c=>c.displayName).join('\n');
console.log(map)
The
message.guild.members.cache.filter(c=> !c.member.user.bot)
Filters bots from the list, the
.map(c=>c.displayName).join('\n');
maps the data and only the user nicknames and joins them by paragraph breaks.
If there are any issues, please comment!

How to get the user which added the bot?

i'm currently working on a Discord.js bot and i want that i get the id or name of the user which added the bot to the guild.
I want that the person which added the bot gets a DM.
Thanks for the answers!
client.on("guildCreate", guild => { // This event fires when a guild is created or when the bot is added to a guild.
guild.fetchAuditLogs({type: "BOT_ADD", limit: 1}).then(log => { // Fetching 1 entry from the AuditLogs for BOT_ADD.
log.entries.first().executor.send(`Thank you for adding me to ${guild.name}!`).catch(e => console.error(e)); // Sending the message to the executor.
});
});

How to learn guild's id at add reaction (messageReactionAdd event)

I want to create a command as Zira Bot. If you don't know Zira, the command is reaction role. So, at reaction add, if reaction emoji is 'bla bla', add role 'bla bla'. But I need to learn the guild's id.
I have tried reaction.guild.id and user.guild.id, but it hasn't worked.
My code is:
Bot.on ('messageReactionAdd', (reaction, user) => {
const fs = require ('fs')
const reactionRole = JSON.parse(fs.readFileSync('./Util/Reaction Role.json'))
const reactionRoleEmoji = reactionRole[reaction.guild.id].reaction
const reactionRoleRole = reactionRole[reaction.guild.id].role
if (reaction.emoji.id === reactionRoleEmoji) user.addRole(reactionRoleRole)
})
It errors "the property "id" of undefined".
reaction has a message property, and message has guild.
reaction.message.guild.id

Resources