How to run the code in the server its command is used - discord.js

So, I have made this code
const { Guild } = require("discord.js");
Guild.roles.create({
name: 'Kırmızı',
color: '#ff0000',
reason: 'Before giving color roles, you need to add the roles to server'
})
I know that I should be writing the guild name but I want it to run this code in the server its command is used. How can I do that?

You're pretty close. You get the guild from the message:
message.guild.roles.create({
name: 'Super Cool Blue People',
color: 'BLUE',
reason: 'we needed a role for Super Cool People',
});
It does depend on how you are triggering this interaction, e.g. message, interaction, reaction, etc.
If you are doing it from a reaction, for example:
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.emoji.name === '👍') {
reaction.message.guild.roles.create({
name: 'Super Cool Red People',
color: 'RED',
reason: 'we needed a role for Super Cool People',
});
}
});
Roles
Reactions

Related

How can I DM the user an embed before he is kicked (discord.js)

I'm trying to make a blacklist command, and how do I make it so it sends an embed instead of normal message.
(Also, I'm new to discord.js)
Here's my script:
module.exports = {
data: new SlashCommandBuilder()
.setName('blacklist')
.setDescription('Blacklist someone from the server')
.addUserOption((option) => option.setName('member')
.setDescription('Who are you blacklisting?')),
async execute(interaction) {
await interaction.deferReply();
const member = interaction.options.getMember('member');
if (!interaction.member.roles.cache.some(r => r.name === "Blacklist perms")) {
return interaction.editReply({ content: 'You do not have permission to use this command!' });
}
member.send("You have been blacklisted!")
member.kick().catch(err => {
interaction.editReply({ content: `I do not have enough permissions to do that.`})
})
await interaction.editReply({ content: `${member} has been succesfully blacklisted!` });
},
};```
Since your function is already async, simply await sending the message before you call kick.
await member.send("You have been blacklisted!")
member.kick().catch(err => {
interaction.editReply({ content: 'I do not have enough permissions to do that.'})
})
You might also wanna catch() on the send method because users can have DMs from server members disabled or blocked your bot.
You also might consider banning the user instead, because this system would fail if your bot ever happens to go offline when a blacklisted user tries to join. Additionally, depending on how fast your bot reacts, the user may still have time to spam in channels or do something harmful before your bot is able to kick them.
To send an embed either use an EmbedBuilder or a plain JavaScript object with the correct embed structure.
Example:
member.send({ embeds:[{
title: "Title",
description: "Hello World!"
}] })

Add a Role for myself discord.js

I want to do a setup command so I can create a role and give it to myself via a command. The role is created but I cannot give myself the role. I always get an error code after the role was created.
My Code:
case 'setup':
if(!message.author.id == '416940852291045376') return message.channel.send('Youre not me')
message.guild.roles.create({
name: 'Bot dev',
color: '#0080FF',
permissions: 'ADMINISTRATOR'
})
.catch(console.error);
let role = message.guild.roles.cache.find(r => r.name === "Bot dev")
let member = message.member
member.roles.add(role)
The error code i'm getting:
C:\Discord Bots\Bot v13\node_modules\discord.js\src\managers\GuildMemberRoleManager.js:110
throw new TypeError('INVALID_TYPE', 'roles', 'Role, Snowflake or Array or Collection of Roles or Snowflakes');
^
TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes.
RoleManager#create() returns a promise with the newly created role, you must await it or resolve it before searching for the role.
message.guild.roles.create({
name: 'Bot dev',
color: '#0080FF',
permissions: 'ADMINISTRATOR'
})
.then(role => {
const { member } = message;
member.roles.add(role);
})
.catch(console.error);

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;

How can I clear my Discord bot's status after it has been set?

I have some code that gives my bot a status and I want it to clear with the use of a command. I am able to change the status, but I can't clear it.
Here's the code that I use to give the bot it's status:
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
targetGuild = client.guilds.cache.get('729676078599110776')
client.user.setPresence({
status: 'online',
activity: {
name: `${targetGuild.memberCount} members | !c help`,
type: "WATCHING"
}
});
});
I've tried clearing it by setting the presence without adding the status itself, but that seems to just leave it the same.
client.user.setPresence({
status: 'online'
});
Yes, the client.user.setPresence({ activity: null }) will clear the status, and if you want to log out of the bot so it's offline, you can do client.destroy()

Discord js creating discord category

I'm trying to make a category on discord server using Discord Bot but I couldn't find the method or something on internet. Also I looked the "discord.js.org". Then I thought that isn't there any possibility to do that. So is there any way to make a category on discord servers?
discordjs v13 needs GUILD_CATEGORY instead of just "category"
message.guild.channels.create("Name", { type: "GUILD_CATEGORY" });
You need to use the .createChannel method and then enter „category“ as type of the channel
<guild>.createChannel("NAME OF THE CHANNEL", "category")
I would advice the usage of a promise as it adds a lot of functionality and safety to your code
guild.createChannel('new-category', {
type: 'category',
permissionsOverwrites: [{
id: guild.id,
deny: ['MANAGE_MESSAGES'],
allow: ['SEND_MESSAGES']
}]
})
.then(console.log)
.catch(console.error);
This allows you to create the channel with permissions and actually handle any errors like the channel already existing or your bot not being able to create said channel cause of its permissions assigned.
This is the proper way to do this.
Example to create channel
guild.createChannel('new-general', { type: 'text' })
.then(console.log)
.catch(console.error);
v12:
message.guild.channels.create('Category', { type: 'category' });
I have made a command code for you to use. Modify it and use it.
if(message.content === `${prefix}create-channel`) {
message.guild.createChannel('name', {
//Channel type (text || voice || category)
type: 'text',
permissionsOverwrites: [{
id: guild.id,
deny: [],
allow: ['SEND_MESSAGES']
}]
})
.catch(console.error);
}
discordjs v14 needs ChannelType.GuildCategory and the name in the options
message.guild.channels.create({ name: "Name", type: ChannelType.GuildCategory });

Resources