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

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
});

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 bot that sends an message to a channel whenever a message is deleted. Discord.js

I’m trying to make my bot send a message to a channel whenever a user deletes his/her message, sorta like the bot Dyno, but I do not know how to do this. I think the method is .deleted() but I can’t seem to make it work. Can anyone tell me how? Sorry for lack of detail, there’s nothing else to add. Thank you in advance.
The Client (or Bot) has an event called messageDelete which is fired everytime a message is deleted. The given parameter with this event is the message that has been deleted. Take a look at the sample code below for an example.
// Create an event listener for deleted messages
client.on('messageDelete', message => {
// Fetch the designated channel on a server
const channel = message.guild.channels.cache.find(ch => ch.name === 'deleted-messages-log');
// Do nothing if the channel wasn't found on this server
if (!channel) return;
// Send a message in the log channel
channel.send(`A message has been deleted. The message was: ${message.content}`);
});

How to make bot change channel permissions?

So, the Discord bot that I'm making is sort of a server manager kind of thing and I'm making a channel lock command which basically makes it so people can't send messages in the channel.
How would I make it so that when someone types the command it turns the 'Send Messages' permission FALSE?
Any help is appreciated! Thanks.
const Channel = client.channels.cache.get("ChannelID");
if (!Channel) return console.log("Invalid channel ID.");
Channel.updateOverwrite(message.guild.roles.everyone, {SEND_MESSAGES: false});

How to create a discord bot that only allows a specific word in a certain text channel

I am trying to create a discord bot that only allows the word "upgrade" in a certain text channel.
As I am very new to this I would like to learn about how this is done.
Its easy. First create a bot. I guess you know about node.js if not, find tutorials for creating a project with discord.js.
First create a client:
const Discord = require("discord.js");
const client = new Discord.Client();
client.login("SuperSecretBotTokenHere");
(make sure you have a bot token) discordapp.com/developers
Then you create a message event:
client.on("message", (message) => {
if(message.content != "upgrade") return message.delete()
});
Put your bot in the server, give it permissions to delete messages in the channel aaand done!
Sorry for my bad English.

Resources