Eric Wang
Eric Wang

Reputation: 436

Chat command for discordjs bot

I'm making a bot on discord which I would add more commands in the future, I just don't understand the use of arguments and how would it work if there is a space like ";say general(the channel) hi guys I'm a text (the text to send)". It will only send "hi" to general and nothing else which is a downside to the chat command.

const bot = new Discord.Client({disableEveryone: true});


bot.on("ready", async () => {
	console.log(`Bot is ready! ${bot.user.username}`);


	try {
		let link = await bot.generateInvite("[ADMINISTRATOR]");
		console.log(link);
	} catch(e) {
			console.log(e.stack);
	}
	
	bot.user.setGame('nope nope nope');

});

bot.on("message", async (message) => {
	if (message.author.bot) return;
	if (!message.content.startsWith(prefix)) return;
	
	let command = message.content.split(" ")[0];
	command = command.slice(prefix.length);
	
	const args = message.content.slice(prefix.length).trim().split(/ +/g);
	
	if (command === "ping") {
		message.channel.send(`Pong! Time took: ${Date.now() - message.createdTimestamp} ms`);
	} else

	if (command === "say") {
		message.delete()
		let thetext = args[2];
		let thechannel = args[1];
		
		bot.channels.find("name",`${thechannel}`).sendMessage(`${thetext}`)
	}

});

Upvotes: 0

Views: 2180

Answers (2)

Ren Hiyama
Ren Hiyama

Reputation: 479

U should try this if I think u want to repeat the message from a user. My bot prefix is R! My command is echo So my message is

if(command==='echo'){
let ec = message.content;
let co = ec.replace("R!echo","");
message.channel.send(co);
}

Hope this helps you... If u need help join my server https://discord.gg/Rf3xPMm

Upvotes: 0

Ethan Mitchell
Ethan Mitchell

Reputation: 602

So, if I understand you correctly, everything works except the message only includes the first word of said message. This occurs because you're grabbing args[2] which only grabs the second argument (or second word). In order to get the entire message, you would do this:let theText = args.slice(1).join(' ');.

Explanation of code snippet slice(1) what this does is remove the first argument which is the text channel. join(' ') This joins all the arguments that are after the first arg.

In conclusion, if the person ran this command: say general My message is awesome. This would be the variables: theChannel = 'general' theMessage = 'My message is awesome'

Upvotes: 1

Related Questions