Long discord.js arguments - javascript

I'm starting with discord.js and I have a question. Is it possible to make an infinite argument currently I have to use message.channel.send(${args}) and for example when I type $test 1 2 only 1 will be sent and 2 will not please help I wanted to make a reason for the command $kick but only one argument the first is often seen only the first sentence instead of the whole reason please help!
I don't think you know what I mean, I mean that the argument should not be a single word, for example command:$embed hello word :) I want the bot to send: hello word :) but it sends: hello without word and :) it doesn't have to be hello word :) it has to be the words given by the user I hope I wrote it clearly for any help thank you!
const Discord = require('discord.js');
const client = new Discord.Client();
const config = require('./config.json');
const fs = require('fs');
const prefix = "$";
const RED = "#EC0F0F";
const BLUE = "#1725E7";
const GREEN = "#17E729";
const FIOLET = "#880FEC";
client.once('ready', () => {
console.log('Ready!');
});
client.on('message', async message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
console.log(message.content);
if (message.content === `$embed`) {
const embesmess = new Discord.MessageEmbed()
.setTitle(`${args[0]}`)
.setColor(`${GREEN}`)
.setdescription(`HERE I WANT THIS ARGUMENT`)
message.channel.send(embesmess);
}
});
client.login(config.token);

Use something like
if (message.content.startsWith(`$embed`)) {
const embesmess = new Discord.MessageEmbed()
.setTitle(`${args.slice(1).join(" ")}`) //join all arguments besides the first
.setColor(`${GREEN}`)
.setdescription(`HERE I WANT THIS ARGUMENT`)
message.channel.send(embesmess);
}
This will combine all of your arguments excluding the first.

Related

Trying to make it so my command only works in one guild Discord.js

in discord.js i am trying to make it so that my code only executes if the user is in a specific guild, how would i go about doing this?
module.exports = client => {
const channelId = '798241375572328479'
const infoId = '<#798241375136776215>'
const serverId = '798241375098896414'
client.on('guildMemberAdd', (member) => {
console.log(member)
if (client.guild.id(serverId)) {
const message = `Welcome to **awbtawtnawtb** ${member.user}!\n Please take some time to read through ${infoId}. \nI have also Direct Messaged you some quick information`
const welcomeEmbed = new Discord.MessageEmbed()
.setDescription(message);
client.channels.cache.get(channelId).send(welcomeEmbed)
const welcomeDMEmbed = new Discord.MessageEmbed()
.setColor('#9000D2')
.setTitle(`Welcome To The awbeyvatw Discord ${member.user.tag}!`)
.setDescription(`\n\nBy joining this server you are abide by the #info-read rules. Breaking one of these rules will result in punishments accordingly.\nIf you require help create a ticket in #support\nIf you have any further concerns you may DM me #JohnDoe#5032 as a last resort!\n\nI Hope you enjoy your time!\n\n-JohnDoe`)
.setAuthor(``);
const welcomedmipEmbed = new Discord.MessageEmbed()
.setColor('#00F0FF')
.setTitle(`Server Address: wvawbattbvga`)
} else {
} console.log(`Didn't send message due to not being in the same guild!`)
You would use the comparison operator.
if (member.guild.id === serverId) {
// Your code
}

Google search with discord bot

I am working on a bot who searches to google with given arguments.
So i just want it to send the link of the top result found on google kinda like this
so here is the code till now
const Discord = require(`discord.js`);
const bot = new Discord.Client();
const config = require('./botconfig.json');
const prefix = '!g'
bot.on('ready', () => {
console.log('Google-Bot is ready!');
})
bot.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const searchTopic = args.join(' ')
//here it searches on google and provides the link
message.channel.send(`Here is the result! \n ${result}`)
})
bot.login(config.token)
thank you for reading!
You can simply add a link to the requested argument by using https://google.com/search?q=${args.join('+')}, then send it.
Example:
message.channel.send(`https://google.com/search?q=${args.join('+')}`)
You also need to join with a + because google encodes any spaces with a plus symbol.
I also have a suggestion that could lead to a potential problem.
First, make sure you're using the slice attribute after your args.join('+') to exclude the prefix from the argument. So you could use args.join('+').slice(1).
Here is your improved code:
const Discord = require(`discord.js`);
const bot = new Discord.Client();
const config = require('./botconfig.json');
const prefix = '!g'
bot.on('ready', () => {
console.log('Google-Bot is ready!');
})
bot.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const searchTopic = args.join('+').slice(1)
let googleResult = `https://google.com/search?q=${searchTopic}`
let searchEmbed = new Discord.MessageEmbed()
.setColor("#00ff00")
.setDescription(`Here's what Google came up with for ${searchTopic}!\n${googleResult}`)
message.channel.send(searchEmbed)
})
bot.login(config.token)

How to make commands for discord bot non-case sensitive

const Discord = require("discord.js");
//ignore 1 up and 1 down
const client = new Discord.Client();
//prefix
const prefix = `+`;
//log for me
client.once(`ready`, () => {
console.log("IM POWERED UP")
});
//commands
client.on(`message`, message =>{
const args = message.content.slice(prefix.lengh).split(/ +/);
//Username..(embed)
if (message.content === `${prefix}Username`){
let messageArray = message.content.split(" ");
let args = message.content.split(` `);
let command = messageArray[1].toLowerCase();
const embed = new Discord.MessageEmbed()
.setTitle(`user information`)
.addField(`Your Username is`, message.author.username)
.addField(`Bugs`, `To report bugs please go to TxPsycho#1080 and you may earn a reward!`)
.setThumbnail(message.author.displayAvatarURL())
.setFooter(`Wanna get Valorant cheats? Join here`)
.setColor(`RANDOM`)
message.channel.send(embed);
}
//help command
if (message.content === `${prefix}Help`){
const embed = new Discord.MessageEmbed()
.setTitle(`Help`)
.addField(`A list of commands are below!`)
.setThumbnail(message.author.displayAvatarURL())
.setDescription(`AMOTHERFUCKINGTESTYOUMOTHERFUCKAYOUTHINKITSFUNNYCALLINGAT2AM`)
.setColor(`RANDOM`)
message.channel.send(embed);
}
});
//ignore this and leave it at bottom
client.login(`Nz`)
I am not sure how to make the commands caps insensitive, this is the error it’s been giving me. I have been searching but found no information. Down below is the link to an error that I have been receiving when I try to debug the code.
Click here to see the error picture
Like rogue said the issue is calling toLowerCase on undefined here:
let command = messageArray[1].toLowerCase();
The real command would be at 0, since arrays are 0 indexed
You might want to shift it instead so you can get the args easier:
Message: !help 12 24 36
const args = message.content.split(" ");
const command = args.shift().toLowerCase();
console.log(args);
// => [12, 24, 36]
console.log(args[0]);
// => 12
After that you just compare command instead of message.content:
if(command === "!help") {
}
You can sanitise messages with toLowerCase
console.log('ALPHABET'.toLowerCase()); // 'alphabet'
console.log('aLPhABeT'.toLowerCase()); // 'alphabet'
console.log('alphabet'.toLowerCase()); // 'alphabet'

Javascript Discord Bot giving code reference errors when ran

I've been working on a discord bot lately, this is my first time coding in general, and I thought Javascript would be easier than other options I might have had. Right now, I'm struggling through reading error after error.
Anyways, lets get to the question at hand. Currently, the code is as follows:
const Discord = require("discord.js");
const client = new Discord.Client();
const commando = require('discord.js-commando');
const bot = new commando.Client();
const prefix="^";
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
let short = msg.content.toLowerCase()
let GeneralChannel = server.channels.find("General", "Bot")
if (msg.content.startsWith( prefix + "suggest")) {
var args = msg.content.substring(8)
msg.guild.channels.get(GeneralChannel).send("http\n SUGGESTION:" + msg.author.username + " suggested the following: " + args + "")
msg.delete();
msg.channel.send("Thank you for your submission!")
}
});
When I ran said code, it returned an error that (I think) basically told me that "server" in let GeneralChannel = server.channels.find("General", "Bot") was undefined. My problem is, I dont actually know how to define server. I'm assuming that when I define server to it, it will also tell me I need to define channel and find, though I'm not sure.
Thanks in advance :)
First of all, why are you using both let and var? Anyways, as the error says, server is not defined. The client doesn't know what server you're referring to. That's where your msg object comes in, it has a property guild which is the server.
msg.guild;
Secondly, what are you trying to achieve with let GeneralChannel = server.channels.find("General", "Bot")? the find method for arrays takes a function. Are you trying to look for the channel with the name "General" or something? If so, better to use the id of the channel that way, you can use a channel from any server the bot is in (in case you're trying to send all suggestions to a specific channel on a different server).
let generalChannel = client.channels.find(chan => {
return chan.id === "channel_id"
})
//generalChannel will be undefined if there is no channel with the id
If you're trying to send
Going by that assumption, your code can then be re-written to:
const Discord = require("discord.js");
const client = new Discord.Client();
const commando = require('discord.js-commando');
const bot = new commando.Client();
const prefix="^";
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
let short = msg.content.toLowerCase();
if (msg.content.startsWith( prefix + "suggest")) {
let generalChannel = client.channels.find(chan => {
return chan.id === 'channel_id';
});
let args = msg.content.substring(8);
generalChannel.send("http\n SUGGESTION: " + msg.author.username + " suggested the following: " + args + "");
msg.delete();
msg.channel.send("Thank you for your submission!")
}
});
Not that scope is a concern in this instance but it's worth noting that 'let 'defines a local variable while 'var' defines a global variable. There is a difference.

Can't send a message when the first word of a sentence is specified

I am trying to make a discord bot. I have a done a ping function when the bot answers pong. This a properly working. But now i am trying to make a music bot so the command is ~play url
I'm taking the first word of the command to be sure it's ~play. But my way to analyze the sentence is not correct apparently.
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log('I am ready!');
});
client.on('message', message => {
if (message.content === '~ping') {
message.reply('pong');
}
var msg = message.content;
var play = msg.split(" ", 1);
if (play === '~play') {
console.log('jioejfaoi');
}
});
client.login('mytoken');
The split function returns an array, even if it has only one value.
var play = msg.split(" ", 1)[0]

Categories