My bot sends the message if someone boosts/unboosts the server.
You can see my code here:
client.on("guildMemberUpdate", (oldMember, newMember) => {
const oldStatus = oldMember.premiumSince;
const newStatus = newMember.premiumSince;
if (!oldStatus && newStatus) {
client.channels.cache
.get("channel id")
.send(`Thank you ${newMember.user.tag} (:`);
}
if (oldStatus && !newStatus) {
client.channels.cache
.get("channel id")
.send(`woah ${newMember.user.tag}, unboost this server`);
}
});
The code works perfectly, there is no error, but the bot is not tagging people, just mentioning the tag name like this:
I want the bot to mention people like this instead:
I think the problem is ${newMember.user.tag}. Usually, I use <#${member.id}>, but I don't know how to fix this code if using {user.tag}.
You can either use:
.send(`woah <#${newMember.id}>, unboost this server`)
or simply:
.send(`woah ${newMember}, unboost this server`)
Related
CODING LANGUAGE = DISCORD.JS
| COMMAND = R!MASSBAN
client.on('message', async(message) => {
if (message.content === 'r!massban') {
message.guild.members.cache.forEach (member => {
if (member.hasPermission("ADMINISTRATOR")) return;
member.ban();
});
}
})```
It only bans me. I get no errors in console. It will only ban me and no one else even though it is above all other roles. This is my first coding project using discord.js and js. Any help will be appreciated.
Looks mostly fine to me.
If you are the only person being banned, maybe console.log the cache to see who is in it. The cache only has people who were recently active, so if it's your test server, if you're the only active person, you may be the only person in the cache.
//edit:
Found out what it might be. replace cache with .fetch(), fetch also gets offline members.
client.on("message", (msg) => {
if(msg.content.trim().startsWith("r!")){
const [prefix, command] = msg.content.split("!");
switch(command){
case "massban":
msg.guild.members.cache.forEach(member => {
if(member.hasPermission("ADMINISTRATOR")) return;
member.ban();
});
break;
default:
msg.reply("That command doesn't exist");
}
}
})
I want to have a simple command like "!hello" to output "Hello #everyone" and ping everyone. The output text is correct, but it doesn't actually ping. The command just shows the text #everyone without doing the mention.
const Discord = require("discord.js")
module.exports.run = async (bot, message, args) => {
message.channel.send("#everyone Hello!");
}
module.exports.help = {
name: "hello"
}
I would expect it to output this:
Actual Result:
So, turns out I had:
const bot = new Discord.Client({disableEveryone: True});
Once I changed it to:
const bot = new Discord.Client({disableEveryone: False});
everything worked.
Thank you for your help everyone.
Citing discord.js issue #2285:
You mention everyone or here with the literal strings #everyone or #here, not a > regular role mention.
This is not a bug, but a discord thing.
Try this
message.channel.send("<#everyone>" + "Hello!");
There is a guild.defaultRole
You can mention it like this:
client.on('message', (msg) => {
msg.channel.send(msg.guild.defaultRole.toString());
});
You can check if your bot can mention everyone this way:
client.on('message', (msg) => {
let everyone = msg.guild.defaultRole;
if (msg.guild.me.hasPermission(everyone.permissions)) {
msg.channel.send(everyone.toString());
} else {
console.log("I can't mention everyone");
}
});
You can fetch the everyone role from guild.roles.
const everyone = await guild.roles.fetch("#everyone");
channel.send(`Hello ${everyone}`);
I'm not sure why, but I remember having to put the role in its own string literal in the send call:
message.channel.send("#everyone" + " Hello!");
Unable to test for myself atm, see if that works.
I have been wondering how can I send messages when someone invites my bot to their server.
Please help me in this area I can't figure it out it's there in Python but I have not used to it.
Thank you for your help
All the above answers assume you know something about the server and in most cases you do not!
What you need to do is loop through the channels in the guild and find one that you have permission to text in.
You can get the channel cache from the guild.
Take the below example:
bot.on("guildCreate", guild => {
let defaultChannel = "";
guild.channels.cache.forEach((channel) => {
if(channel.type == "text" && defaultChannel == "") {
if(channel.permissionsFor(guild.me).has("SEND_MESSAGES")) {
defaultChannel = channel;
}
}
})
//defaultChannel will be the channel object that the bot first finds permissions for
defaultChannel.send('Hello, Im a Bot!')
});
You can of course do further checks to ensure you can text before texting but this will get you on the right path
Update discord.js 12+
Discord.JS now uses cache - here is the same answer for 12+
bot.on("guildCreate", guild => {
let found = 0;
guild.channels.cache.map((channel) => {
if (found === 0) {
if (channel.type === "text") {
if (channel.permissionsFor(bot.user).has("VIEW_CHANNEL") === true) {
if (channel.permissionsFor(bot.user).has("SEND_MESSAGES") === true) {
channel.send(`Hello - I'm a Bot!`);
found = 1;
}
}
}
}
});
})
You can simply send the owner for example a message using guild.author.send("Thanks for inviting my bot");
Or you can also send a message to a specific user:
client.users.get("USER ID").send("My bot has been invited to a new server!");
I'm not sure if you're using a command handler or not since you seem new to JS I'm going to assume you're not. The following is a code snippet for what you're trying to do:
client.on('guildCreate', (guild) => {
guild.channels.find(t => t.name == 'general').send('Hey their Im here now!'); // Change where it says 'general' if you wan't to look for a different channel name.
});
So I have been utterly frustrated these past few days because I have not been able to find a single resource online which properly documents how to find emojis when writing a discord bot in javascript. I have been referring to this guide whose documentation about emojis seems to be either wrong, or outdated:
https://anidiots.guide/coding-guides/using-emojis
What I need is simple; to just be able to reference an emoji using the .find() function and store it in a variable. Here is my current code:
const Discord = require("discord.js");
const config = require("./config.json");
const fs = require("fs");
const client = new Discord.Client();
const guild = new Discord.Guild();
const bean = client.emojis.find("name", "bean");
client.on("message", (message) => {
if (bean) {
if (!message.content.startsWith("#")){
if (message.channel.name == "bean" || message.channel.id == "478206289961418756") {
if (message.content.startsWith("<:bean:" + bean.id + ">")) {
message.react(bean.id);
}
}
}
}
else {
console.error("Error: Unable to find bean emoji");
}
});
p.s. the whole bean thing is just a test
But every time I run this code it just returns this error and dies:
(node:3084) DeprecationWarning: Collection#find: pass a function instead
Is there anything I missed? I am so stumped...
I never used discord.js so I may be completely wrong
from the warning I'd say you need to do something like
client.emojis.find(emoji => emoji.name === "bean")
Plus after looking at the Discord.js Doc it seems to be the way to go. BUT the docs never say anything about client.emojis.find("name", "bean") being wrong
I've made changes to your code.
I hope it'll help you!
const Discord = require("discord.js");
const client = new Discord.Client();
client.on('ready', () => {
console.log('ready');
});
client.on('message', message => {
var bean = message.guild.emojis.find(emoji => emoji.name == 'bean');
// By guild id
if(message.guild.id == 'your guild id') {
if(bean) {
if(message.content.startsWith("<:bean:" + bean.id + ">")) {
message.react(bean.id);
}
}
}
});
Please check out the switching to v12 discord.js guide
v12 introduces the concept of managers, you will no longer be able to directly use collection methods such as Collection#get on data structures like Client#users. You will now have to directly ask for cache on a manager before trying to use collection methods. Any method that is called directly on a manager will call the API, such as GuildMemberManager#fetch and MessageManager#delete.
In this specific situation, you need to add the cache object to your expression:
var bean = message.guild.emojis.cache?.find(emoji => emoji.name == 'bean');
In case anyone like me finds this while looking for an answer, in v12 you will have to add cache in, making it look like this:
var bean = message.guild.emojis.cache.find(emoji => emoji.name == 'bean');
rather than:
var bean = message.guild.emojis.find(emoji => emoji.name == 'bean');
I have recently created a bot for my discord server. Now I want him to filter bad words.
For example:
User (without bot): You are an asshole
User (with bot): You are an [I'm stupid because I swear]
Is this even possible in Discord? I have given my bot all permissions! (including removing messages, it can't edit message with the program self tho)
If that is not possible^ Can we do the following?
The ability to directly delete the message and write the following:
Bot: #username Do not swear!
Now I have the following code (I dont know if useful):
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log('Hello, the bot is online!')
});
client.on('message', message => {
if(message.content === '--Trump'){
message.reply('He is the president of the United States of
America!');
}
if(message.content === '--Putin'){
message.reply('He is the president of Russia!');
}
});
client.login('MzAwMzM5NzAyMD*1NDUxNzc4.C8rH5w.M44LW*nrfbCR_zHzd**vtMqkr6nI');
Docs. Currently in the Discord API there is no possible way to edit a message from another user. You could completely delete the message or you could resend it but edited. If you want to resend it then you could use:
let censor = "[Sorry, I Swear]"; /* Replace this with what you want */
client.on('message', message => {
let edit = message.content.replace(/asshole/gi, censor);
message.delete();
message.channel.send(`${message.author.username}: ${edit}`);
}
Input >>> Hello asshole
Output <<< AkiraMiura: Hello [Sorry, I Swear]
Take note that if the user sends a 2000 byte (Charater) long message you won't be able to send a fixed version and it would just get deleted.
Use Regex to help you detect from blacklisted words you wanted to.
For example, If you want to blacklist the word asshol*, use the regex to detect the word:
if ((/asshole/gm).test(message.content))
message.delete().then(() => {
message.reply('Do not swear!'); // Sends: "#user1234 Do not swear!"
});
}
If you wanted to blacklist/filter MULTIPLE words, like fu*k and sh*t
Use separators in Regex: /(word1|word2|word3)/gm
So... use:
if ((/(fuck|shit)/gm).test(message.content)) {
message.delete().then(() => {
message.reply('Do not swear!');
});
}
It's fully working!
Re-write in FULL code,
client.on('message', (message) => {
if ((/asshole/gm).test(message.content)) {
message.delete().then(() => {
message.reply('Do not swear!'); // Sends: "#user1234 Do not swear!"
});
}
});
Tell me if it works
Good Luck,
Jedi
try:
client.on('message', message => {
message.edit(message.content.replace(/asshole/gi, "[I'm stupid because I swear]"))
.then(msg => console.log(`Updated the content of a message from ${msg.author}`))
.catch(console.error);
});
credit to #André Dion for bringing up the right method from the API