Im making a say command for my bot but how do I make it so it doesnt say #everyone, #here or # mention
ex:
!say #everyone
the bot will say #everyone which pings how do I make it so it doesnt ping?
the code:
var arg= message.content.split(" ").slice(1).join(" ")
if(!arg){
message.channel.send('What do you want me to say?')
} else
message.channel.send (arg)
Try this:
const newMessage = arg.replace(/<#[!&]?\d+>/, '[mention]');
Replace var arg= message.content.split(" ").slice(1).join(" ") with var arg= message.cleanContent.split(" ").slice(1).join(" ")
Found a simpler way:
const Util = require('./node_modules/discord.js/src/util/Util.js');
const msg = Util.removeMentions(arg);
message.channel.send(msg);
Refer to: here.
Related
As in the question, I have a problem with a bot that won't add ranks, I don't know why, I think I'm doing everything right.
bot.on('guildMemberAdd', guildMember => {
WelcomeUser(guildMember)});
The bot only sends an embed to the welcome chat, but does not rank the user
function WelcomeUser(usr){
usr.roles.add(userRoleID);
let welcomeChan = bot.channels.cache.find(channel => channel.id === welcomeChanID);
let userAvatar = usr.user.avatarURL();
const embed = new Discord.MessageEmbed()
.setTitle(usr.user.tag + " welcome in our server!")
.setColor("#ffff00")
.setThumbnail(userAvatar)
.setFooter('RollerBot by flanktus', "https://i.imgur.com/M2NNoC9.png")
.setDescription("We hope you will have a nice time");
welcomeChan.send(embed);
welcomeChan.send(`${usr.user}`)
}
So, what I want to do is something like this:
User: sends a command like this: mybot!announce #mention (message) (Attachs image)
Bot: gets the mention, message & attachment from the message, and deletes it. Then resends the message in an embed (see picture).
I managed to do everything else but I can't get the attachment & re-send it in the embed. Please, help :p
Here is the code I actually have, in case you need it:
if (command === 'announce')
{
var mention = args[0];
if (!args[0]) return message.channel.send('You need to mention someone!');
var actualMention = null;
if (mention === '#everyone')
{
actualMention = message.mentions.members.every();
}
else
{
actualMention = message.mentions.members.first();
}
var messageAnnouncement = args.slice(1).join(" ");
if (msgAnnouncement === null) return message.channel.send('You need to put the announcement itself!');
var imageAttachments = message.attachments;
const anunouncementEmbed = new Discord.MessageEmbed()
.setAuthor('YPBot - Announcement')
.setTitle(message.author.tag + ' made an announcement')
.setDescription('Mentioning: ' + mention + '\n\n' +
msgAnnouncement)
.setThumbnail(imageAttachments[0]);
message.channel.send(anunouncementEmbed);
}
Hope I explained myself well. In case I didn't, feel free to ask anything.
Message#attachments actually returns a Collection, so if you want the first element you need Collection#first(). From there you can access the MessageAttachment#url property.
.setThumbnail(imageAttachments.first().url);
I want to make a image embed via arguments, but I can't set the Title of the embed.
But when I try to run the comand, it errors out and the embed won't appear
if(message.content.startsWith(prefix + "foto-annuncio"))
{
if(!message.member.hasPermission(["MANAGE_MESSAGES"]))
{
/* error message pops out */
}
let argsresult;
let mChannel = message.mentions.channels.first()
message.delete()
argsresult = args.slice(1).join(" ")
let url = args.slice(1).join(" ");
let title = args.slice(2);
message.delete();
const embed = new Discord.RichEmbed ()
.setImage(url)
.setColor('#008000')
.setAuthor('Delta Logistics', 'https://cdn.discordapp.com/attachments/604963851561336832/665504827044003860/zoom_delta_6_discord.png')
.setTitle(title)
mChannel.send(embed).catch(err => console.log(err));
message.channel.send("Done!");
}
As Giuuliopime said you can't set an array as a title, you prob meant to join the arguments:
const title = args.slice(2).join(" ");
I have this command on my bot where you can mute someone using the command g!mute where the 'user' is mentioned using #. However, if you don't mention e.g GeoGeo instead of #GeoGeo, it causes the bot to crash. I know you need to put .catch(console.error); somewhere, but I'm not sure where. Thanks in advance. The Error is
let person = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[1]))
^
TypeError: message.guild.members.get is not a function
Code:
const Discord = require('discord.js');
const ms = require('ms');
module.exports = {
name: 'mute',
description: "this is mute command",
execute(message, args){
if(!message.member.roles.cache.find(r => r.name ==="Staff", "Head Staff", "Owner", "Co-Owner")) return message.channel.send(`YOU DO NOT HAVE PERMISSION TO DO THAT`)
let members = args[0];
if(!members) return message.reply("g!mute <user> <time>")
let person = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[1]))
if(!person) return message.reply("That person is not in the server!");
let mainrole = message.guild.roles.cache.find(role => role.name === "Fans");
let muterole = message.guild.roles.cache.find(role => role.name === "muted");
if(!muterole) return message.reply("That role does not exist");
let time = args[1];
if(!time){
return message.reply("g!mute <user> <time>");
}
person.roles.remove(mainrole.id);
person.roles.add(muterole.id);
const embed = new Discord.MessageEmbed()
.setTitle ("Muted:")
.setDescription (`${person.user.tag} has now been muted for ${ms(ms(time))}`)
.setColor(0x01B8FF)
message.channel.send(embed);
setTimeout(function(){
person.roles.add(mainrole.id)
person.roles.remove(muterole.id)
const embed = new Discord.MessageEmbed()
.setTitle ("Muted:")
.setDescription (`${person.user.tag} has been unmuted`)
.setColor(0x01B8FF)
message.channel.send(embed);
}, ms(time));
}
}
When faced with an error like: TypeError: message.guild.members.get is not a function
The logical thing to do is check the docs to see that message.guild.members really has a function named get. Here's the docs: https://discord.js.org/#docs/main/stable/class/GuildMemberManager
No get. But there is a cache like you use elsewhere in the code. Just by checking over the docs you can tell that your existing code is wrong (it's outdated) and you need to use cache like you do elsewhere in your code:
message.guild.members.cache.get(args[1])
I'm trying to create a discord.js announcement bot ex. When I write ?Ann Hello Guys the bot delete my message and send Hello Guys but now if I write ?Ann Hello Guys the bot delete my message and send ?Ann Hello Guys the code is this
const client = new Discord.Client();
client.login('--------------');
const PREFIX = '?';
client.once('ready', () => {
console.log('Questo bot e online!');
});
client.on('message', message=>{
let args = message.content.slice(PREFIX.length).split(' ');
switch(args[0]){
case 'ann':
const embed = new Discord.MessageEmbed()
.addField('test', message.content );
message.channel.send(embed);
break;
}
})
Made a pen for this:
https://codepen.io/herbievine/pen/YzwNewQ
client.on('message', message=>{
let args
if (message.startsWith('?'))
args = message.content.replace('?', '').split(' ')
if (args[0].toLowerCase() === 'ann') {
const embed = new Discord.MessageEmbed()
.addField('test', message.contentreplace('?ann', ''));
message.channel.send(embed);
}
}
})
Hope it helps
message.content represents the whole message that you are sending, so in this case will be ?Ann Hello Guys.
DiscordJS doesn't understand what commands are, and it doesn't split this stuff out for you.
As your message is formatted as ?Mention <Command arguments here> you can split it via the space within the string.
let messageContent = message.content.split(" ")
This takes your string of "?Ann Hello Guys" and turns it into "?Ann", "Hello", "Guys"
.split(" ") separates your string via the spaces, and returns an array.
You can then do messageContent.pop() to remove the first element in the array, which is "?Ann".
You can then .join(" ") these back together, forming the string "Hello Guys" and send that. Fully formed code:
let splitMessage = message.content.split(" ")
splitMessage.pop();
const transformedMessage = splitMessage.join(" ")