Embed in Discord.js send also the command - discord

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(" ")

Related

Why doesn't my bot add rank to users? [DISCORD.JS

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

How to make my bot not say #everyone, #here or #mention

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.

DiscordJS Argument Slice

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(" ");

How to make this bot listen to argument after prefix and answer?

so i'm trying this 8ball bot, and everything is working fine, but i can't get how can i leave in the condition that only when the bot get "!verda arg1 arg2" it answers one of the replies in the array.
meanwhile my condition is if the user type the prefix "!verda" only, it replies , i want to include the argument too in the condition
const Discord = require("discord.js");
const client = new Discord.Client();
const cfg = require("./config.json");
const prefix = cfg.prefix;
client.on("message", msg => {
if (!msg.content.startsWith(prefix) || msg.author.bot) return;
const args = msg.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase;
if (msg.content === prefix){
let replies = [
"Yes.",
"No.",
"I don't know.",
"Maybe."
];
let result = Math.floor((Math.random() * replies.length));
msg.channel.send(replies[result]);
}
else if (msg.content === "!help"){
msg.channel.send("I have only 1 command [!verda]");
}
})
client.login(cfg.token);
const command = args.shift().toLowerCase;
toLowerCase is a function and therefore should be
const command = args.shift().toLowerCase();
By doing msg.content === prefix, you are checking if the whole content of the message is equal to that of cfg.prefix
if(msg.content.startsWith(`${prefix}8ball`) {
}
The answer was simple as i figured it out, i simply had to join the spaces
if (msg.content === `${prefix} ${args.join(" ")}`)

How to change the commas to space?

So I created a poll command, but when I type in the command, there are commas between each word.
exports.run = async (bot, message, args) => {
let text = message.content.slice('__poll'.length);
if (!args) return message.reply("You must have something to vote for!")
message.channel.send(`:ballot_box: ${message.author.username} started a poll! React to my next message to vote on it. :ballot_box: `);
const pollTopic = await message.channel.send(`${args}`);
pollTopic.react(`✅`);
pollTopic.react(`⛔`);
};
when I want the question to be What is up, this happens
what,is,up!
By default, when translating an array into a string, JavaScript will separate each element by a comma. To change the delimiter, you can use Array.join().
Example:
const arr = ['How', 'are', 'you?'];
console.log(`Original: ${arr}`);
const str = arr.join(' ');
console.log(`Joined: ${str}`);

Resources