I'm trying to "moderate" a channel, in this one should only be able to send certain commands, and when they send a command or message different from the one allowed, then I would send a message warning about it. The logic of moderation I have with the conditions, but my problem is that I can not get the messages that are sent on that channel (of anyone who writes on that specific channel)
When executing the code it does not show me anything in the console, which means that it is not recognizing the messages that they send in that channel :(
Code:
client.on("message", (message) => {
if (message.author.tag === "NAME#1234") {
if (message.content.startsWith(prefix + "on")) {
console.log(message.channel.id)
if (message.channel.id == "361344824500289538") {
//If the channel where they send the message has the id that I have set, then show me the messages that are sent
console.log(message.content)
} else {
console.log(message.content)
}
}
}
});
Let’s say you want to have a command only to be used in one channel (this could be /meme for a channel called “memes”). If the command is used elsewhere the bot will say “Command Not Allowed Here!”
Here Is The Code:
client.on("message", message => {
if (message.content.startsWith('/meme') {
if (message.channels.find(c => c.name ===! 'memes') {
message.reply("Command Not Allowed Here!")
} else {rest of meme command script}
}
}
Related
I made a bot that deletes text messages in an image channel, but when I use it it's logs this error.
client.on("message", async message => {
if (message.author.bot) return;
const users = ['853248522563485717'];
if (message.channel.id === '841260660960395287') {
if (users.includes(message.author.id)) return;
if (message.attachments.size !== 0) return;
message.member.send("You can't send text in 'images' channel")
await message.delete();
} else return;
});
(this is different from the other questions with the same topic)
how do I fix it?
The most likely problem is that you are running multiple instances of the Discord bot. Check if you have multiple command lines open, if so, check if they are running the bot. Apart from that, if users spam, Discord API tends to get confused about which messages to delete, making it try to delete the same message more than 1 time. You can try adding an if statement checking if the message is undefined.
client.on("message", async (message) => {
if (!message) return; // ADDED THIS LINE OF CODE
if (message.author.bot) return;
const users = ["853248522563485717"];
if (message.channel.id === "841260660960395287") {
if (users.includes(message.author.id)) return;
if (message.attachments.size !== 0) return;
message.member.send("You can't send text in 'images' channel");
await message.delete();
} else {
return;
}
});
From your error above, you are seemingly using a message event listener for every command or function you want your bot to perform. The maximum for this is 11 (of the same type). This is not the correct way to do this.
What is an event listener?
//the 'client.on' function is called every time a 'message' is sent
client.on("message", async (message) => {
//do code
});
How to fix this:
Instead of using 1 event listener per command, use 1 for the whole client. You could solve this a multitude of ways, with command handlers or such, however I will keep it simple.
This is the basic setup you should see in your index.js or main.js file:
// require the discord.js module
const Discord = require('discord.js');
// create a new Discord client
const client = new Discord.Client();
// on message, run this
client.on('message', (message) => {
if (!message) return;
console.log(`message sent in ${message.channel.name}. message content: ${message.content}`);
//this event will fire every time a message is sent, for example.
});
// login to Discord with your app's token
client.login('your-token-goes-here');
So what do you do if you want to do multiple functions with your bot? (for example deleting text in an images only channel and logging messages to console)
Command handling. It is the only way. I linked an official Discord.js guide above, read through it and see what you can do. I would highly recommend trying this with an entirely different bot and clean index file, as it would be easier than trying to fit in code you dont understand between your already problematic code.
I am trying to get my discord bot to send this message only if the user starts typing in the defined channel, and not other text channels. I don't get any errors, the bot just doesn't send a message. What am I doing wrong here? Can typingStart be defined to a specific channel?
const Join2_channel = "972135774921247676"
bot.on("typingStart", (message , channel) => {
if (channel.id === Join2_channel) {
message.send('Type !join');
}
});
The typingStart event takes two parameters; channel (the channel the user started typing in) and user (that started typing), in this order.
In your current code you're checking if the user.id is 972135774921247676 and as that's a channel's snowflake, it won't match. You need to update your callback function:
bot.on("typingStart", (channel, user) => {
if (channel.id === Join2_channel) {
channel.send('Type !join');
}
});
I want to make my discord bot send different messages when anyone types a command in a normal channel or NSFW channels.
I followed the documentation, which I didn't quite get it. I wrote the testing commands below:
client.on('message', message => {
if (command === 'testnsfw') {
if (this.nsfw = Boolean(true.nsfw)) {
return message.channel.send('yes NSFW');
} else return message.channel.send('no NSFW');
}
})
I think it is not working. The bot only responds "no NSFW" on both channels.
You cannot refer to the TextChannel using this in the anonymous function. (Also, this is always undefined in an arrow function) You can access the TextChannel using the Message class, stored in the message variable.
client.on("message", message => {
// Making the sure the author of the message is not a bot.
// Without this line, the code will create an infinite loop of messages, because even if a message is sent by a bot account, the client will emit the message event.
if (message.author.bot) return false;
// message.content is a String, containing the entire content of the message.
// Before checking if it equals to "testnsfw", I would suggest to transform it to lowercase first.
// So "testNSFW", "testnsfw", "TeStNsFw", etc.. will pass the if statement.
if (message.content.toLowerCase() == "testnsfw") {
// You can get the Channel class (which contains the nsfw property) using the Message class.
if (message.channel.nsfw) {
message.channel.send("This channel is NSFW.");
} else {
message.channel.send("This channel is SFW.");
}
}
});
What I encourage you to read:
Message
TextChannel
now you can just do this instead
if(!message.channel.nsfw) return message.channel.send('a message to send to channel')
this is using
discord.js verson 13.6.0
So I am attempting to make a Discord bot post a message in a specific channel on a server, because it only posts to #everyone. I have looked at many other posts and can;t seem to get it. I am a complete moron at javascript and only know the very basic basics. The goal is when a user says "test" in a channel then the bot will output "Test" into a specific channel meant for logging certain responses. (This will become a offense log for when a user says something they aren't supposed to). This is what my code looks like so far:
client.on('message', msg => {
if (msg.content === 'test') {
client.channels.get("546117125702680596");
channel.send('Test');
}
});
Am I doing something wrong?
Have you tried this?
client.channels.get('546117125702680596').send('Test');
I'm not sure what channel is a reference to in your code.
If you want to post to the same channel as the origin message:
client.on('message', msg => {
if (msg.content === 'test') {
message.channel.send('Test');
}
});
If you want to send into a specific channel:
client.on('message', msg => {
if (msg.content === 'test') {
const yourchannel = msg.guild.channels.find(channel => channel.id === '546117125702680596')
yourchannel.send('Test');
}
});
I have a suggestion channel, and i made it so users are only allowed to post links in it, with the ability for the bot to react on what they post.
I have done the auto react on links, but i could not make the bot deletes what is not a link.
So i want everything else that is not a link to be deleted.
bot.on('message', message => {
// whenever a message is sent
if (bot.id === '514484773171757061') {
return;
}
if (message.channel.id === "508606903740268545" ){
if (message.content.includes('playrust.io/')) {
message.react('✅').then( () =>{
message.react('❌')});
} else if (message.delete()) {
message.channel.sendMessage('Message has been Deleted ' + message.author)
}
}
});
It is kind working but not very well.
It deletes the message that is not a link million times and sends million notifications :/
I guess the problem is with else if part
I think the problem is that you are creating a sort of infinite loop. In your code you first check if the message send is in a specific channel, after which you check if it contains a link or not. If it doesn't you send a message to that same channel saying 'I didn't find a link'.
When you send this, your bot gets triggered again because a new message has been send. It checks whether it's in a specific channel and if it contains a link, which it doesn't, and thus the cycle repeats.
It can be fixed with one simple statement, which also happends to be very good practice when creating bots. To fix it, you need to include some code which checks if a message has been send by a bot or not. Look at the example below:
bot.on('message', message => {
// Ignore messages from all bots
if (message.author.bot) return;
/* All your other code here */
client.id is not a thing so it needs to be
if (message.author.bot) return;
Then instead of
else if (message.delete()) {
message.channel.send('Message has been Deleted ' + message.author)
}
Use
else {
message.delete(5000)
message.channel.send('Message has been Deleted ' + message.author)
}
Wich result in:
bot.on('message', message => {
// whenever a message is sent
if (message.author.bot) return;
if (message.channel.id === "508606903740268545" ){
if (message.content.includes('playrust.io/')) {
message.react('✅').then( () =>{
message.react('❌')});
} else {
message.delete(5000)
message.channel.send('Message has been Deleted ' + message.author)
}
}
});