How to register slash commands automaically when bot enters server discord.js - discord.js

As of now i have my bot manually deploy slash commands to the server. What should i do so that the bot self registers the slash commands without using a prefix command/registers itself as soon as it enters a server. Here's the code i'm using as of now for deploying slash commands. I'm using discord.js V13 btw
if(content === "!deploy guild") {
if(!message.guild) return;
console.log("Deploying commands in guild...");
await message.guild.commands.set(commands).then(() => console.log(`Commands deployed in guild ${message.guild.name}!`));
await message.reply("Deployed in guild!");

You would be looking at the Events#GuildCreate emitter, once there is a new guild the bot is added to, this event is fired ( emitted ) and you can add your commands / do any specific task you wish to do on joining a new guild
Setting up a listener for it with your client your code would look something like so:
/*
* client is your discord.js Client
* commands obj is your command data you want to add to the guild
*/
client.on('guildCreate', async (guild) => {
guild.commands.set(commands).then(() =>
console.log(`Commands deployed in guild ${guild.name}!`));
})

Related

Why isn't my code working? Discord.js index.js client.on

It's not sending a message, there are no errors I tried modifying the code nothing is working the bot welcome message still works I think that it's not updating with replit.
I tried modifying the code switching some stuff around basically doing stuff if I see any flaws I made.
client.on('message', message => {
// We'll want to check if the message is a command, and if it is, we'll want to handle it
if (!message.content.startsWith(prefix) || message.author.bot) return;
// Split the message into an array of arguments, with the command being the first element
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
// Check if the command is "privatevc"
if (command === 'Privatevc') {
// Check if the member is in a voice channel
if (!message.member.voice.channel) {
// If the member is not in a voice channel, send a message letting them know they need to be in one to use the command
return message.channel.send('You need to be in a voice channel to use this command.');
}
// If the member is in a voice channel, create a private voice channel for them
message.member.voice.createChannel({type: 'Voice'}).then(channel => {
// Send a message to the member letting them know the private voice channel has been created
message.channel.send(`Private voice channel created for you: ${channel}`);
}).catch(console.error); // If there was an error creating the private voice channel, log it to the console
}
});
This code is for Discord.js v12. If using the newest (v14) you need to use the messageCreate event instead of message. So the code would be:
client.on('messageCreate', message => {
You might also enable the MessageContent and GuildMessages intents.
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent
To make it all work you need to enable at least the third option, as you might see in the screenshot.

Discord: Reading messages from one channel in a (non-admin perm) server, and posting it to another (with admin perms)

I am new to Discord API framework.
ServerFROM is a public server that I was invited to (non-admin perms). Hence I cannot add bots there. But I can view the content in ChannelFROM (a text channel in ServerFROM)
I have my own ServerTO (in which I have admin perms and so can do anything). Inside of which, I have the target ChannelTO
I want to deploy a listener on ChannelFROM, such when there is a new message (announcement) in ChannelFROM, I want it to be read and reposted in ChannelTO.
Something similar to what is done in this Stackoverflow issue, except that I cannot have some script run locally 24x7 on my machine. Maybe use Github Actions or something similar?
How can I go about doing it? Any ideas are appreciated. Maybe some form of a server, or just a custom Discord bot
And thanks in advance.
you can use Custom bot for that. I dont know other way
here's how i do it
1st : We Get the ID of the Channel that you wanted to listen
2nd : We make a Output or where the bot copy the message from and send it
3rd : Bot required a permission to view the channel and send message
or in the nutshell ( sorry if im bad at explaining )
client.on("messageCreate", message => {
if(message.author.bot) return;
if(message.channel.id === ID_HERE) // ChannelFROM in ID_HERE
{
let MSG = message.content
let Author = message.member.displayName
let Avatar = message.author.displayAvatarURL({dynamic: true})
const Embed = new MessageEmbed()
.setAuthor(Author , Avatar )
.setDescription(MSG)
.setColor("RANDOM")
client.channels.cache.get(ID_HERE).send({ embeds: [Embed] }) // SendTo in ID_HERE
}
})
you can remove if(message.author.bot) return; if you want it also read other bot message on that specific channel

Discord: Get channel by ID returns undefined / null

I'm setting up a cron job to run a bot command that unlocks/locks a channel at a certain time every day. Trying to get the channel returns either undefined or null, depending on how I go about it.
The bot is added to the discord server and is online
require('dotenv').config();
const Discord = require("discord.js");
const client = new Discord.Client();
client.login(process.env.TOKEN);
const chan = client.channels.cache.get("858211703946084352");
console.log(chan);
const channel = client.channels.fetch("858211703946084352").then(res => {
console.log(res);
});
console.log(channel);
When I run it in the console I get
undefined
Promise { <pending> }
null
I have looked at many many examples and solutions, but none seem to resolve my issue
Edit:
bot has admin permissions.
I did the 'right click on channel and copy ID' technique, which matched the ID I got when I used dev tools to examine the element containing the channel name
There is a MEE6 bot in the server so I know bots can send messages
Edit2:
For fun and profit I deleted the app and remade it, same issue
I tried using a channel the MEE6 bot sends to, same issue
Try running this code in the ready event
client.on('ready', () => {
const chan = client.channels.cache.get("858211703946084352");
console.log(chan);
});

Discord.js Guild Member Add

I'm making a bot to detect when a user joined. The code below does not work when my test account joins. Does it only work when it's the first time a user joins. If so, how can I make it work everytime.
client.on('guildMemberAdd', member => {
// Send the message to a designated channel on a server:
const channel = member.guild.channels.cache.find(ch => ch.name === 'general-chat');
// Do nothing if the channel wasn't found on this server
if (!channel) return;
// Send the message, mentioning the member
channel.send(`Welcome to the server, ${member}`);
});
You might have to enable Presence Intent on your bot settings on the Discord Developer Portal, which makes it able to detect new members.Here's an screenshot.

How to make my discord bot only work in a chat?

I am using discord.js to implement a bot in the discord. When I use a command in any channel in my server, my bot responds to it, but I would like that my bot only worked if someone was sending the commands inside a private chat with the bot, how do I do that?
If you only want it to work between DMs, do
if (!message.channel.type == `dm`) return;
//other commands
You can check if the message was sent in a certain channel by checking the Message.channel.id property.
client.on("message", message => {
if (message.channel.id !== "ChannelID") return false;
// Execute your commands here
});

Resources