I am making a discord bot and one of the commands allows the user to send and embed to any channel with whatever text they want, but I want them to be able to start a new line in the body of the embed too. Simply having them type "\n" in their message in the command does, not work, the bot will output that \n in the embed instead of making a new line. Is there an easy way to do this?
Embed:
const sayEmbed = new Discord.MessageEmbed()
.setColor('#4d4d4d')
.setTitle(header.join(' '))
.setDescription(args.join(' '))
The description field is where this is occurring when there is a "\n" in the args array it will not make a new line it will simply send.
You don't actually need to use \n, you can just create a new line when sending the message, and discord.js will do all the parsing work for you. I tested this out with my bot:
So I made a kind of simple DIY sort of thing. (LOL, actually I made this as I saw the question, took like 5 mins.)
It allows you to disect a message using the operator | (The one below BackSpace).
I tried using this in an eval command, so I'm sure it works.
The codes are:
// Creating array variables so it doesn't return undefined when we try to `.concat()` it.
let sentences = [];
let temp = [];
// Loops every args
for(l=0;l<args.length;l++) {
// Adding the args as an array to the `temp` variable.
temp = temp.concat(argss[l])
// If we meet `|` which is a sentence separator.
if (args[l] === "|") {
// Join the `temp` array, making it a sentence while removing the `|` operator.
sentences = sentences.concat(temp.join(' ').slice(0, -2));
// Resetting `temp` to reset the saved sentence and start a new one.
temp = [];
}
}
Using .join(' ') will not work since it returns a string from an array, and therefore joining \n cannot be used.
The above method may be more efficient. They use a command such as:
// Say prefix is `.` and the command is `embed`
.embed <header> | <content> | <title1> | <sentence1> | <title2> | <sentence2> |
and you will get sentences[0], sentences[1], sentences[2], sentences[3], sentences[4], sentences[5] respectively. You can then add this to your embed.
This will also allow multi string input, instead of a single args. Don't forget the | at the end since without it, it will ignore the whole last sentence.
const sayEmbed = new Discord.MessageEmbed()
.setColor('#4d4d4d')
.setTitle(sentences[0]) // <header>
.setDescription(sentences[1]) // <content>
.addField(sentences[2], sentences[3]) // <title1> <sentence1>
.addField(sentences[4], sentences[5]) // <title2> <sentence2>
// The more you add, the more it'll allow, you'll have to set it yourself.
TL;DR: A simpler answer:
sentences = args.join(" ").split(" | ");
Sorry, I tend to do things the hard way a lot.
Related
so i need to get from a text file alot of data and when i use fs.createReadStream and copy the data to a varible and start changing it it looks like \n and \r and present and the are messing my splits and array checking and i tried to do a function that removes them that runs on the array and checks for ''(it doesnt work for some reason)
if(arr[i]==='\'(this throws the mistake){**strong text**
(removing it and stuff)
}
do you have any idea how to remove it?
You can either use the String prototype replaceAll, or work with split and join:
let data = '\r \n sdfsdf. \r'
data = data.replaceAll(`\r`, '')
data = data.replaceAll(`\n`, '')
OR ------------------------
let data = '\r \n sdfsdf. \r'
data = data.split(`\r`).join('').split(`\n`).join()
Pay attention - replaceAll is a new prototype and exists only on Node v.15+ and the latest versions of the modern browsers.
See MDN documents to check your needs.
I am trying to cut down and make my code simpler since my original method is not very readable and I get a lot of hard to understand errors for which I have to scan all of the code word by word as it is all one long line.
Lets say the text input was for
In my original code, I wrote :
//Example : You want to know if any of your favorite games were licensed by Nintendo
var tsf = (The value of a textInput in string form)
tsf.toLowerCase()
//Lets say the textinput said "Name your favorite games"
if(tsf.contains('mario') || tsf.contains('pokemon') || tsf.contains('mortal kombat')||etc,etc) {
Alert.alert('At least one of these games was licensed by Nintendo')
}
This works but in the real code there are a lot more "games" and it requires each and every item in the list as it is related to a health project I'm working on.
My 2nd idea was to create an array with all the elements and see if the array contains tsf
nintendoGames = ['mario','pokemon','mortal kombat','zelda','tetris',etc]
if(nintendoGames.contains(tsf)){
Alert.alert('This game was licensed by Nintendo')
}
This also works but only if tsf is a single word. Incase
tsf = 'mario, zelda'
the array method would not work since the array only contains 'mario' and 'zelda' and not the string 'mario, zelda'
I want to be able to scan to see if any part of the string contains any one of the elements in the array and so far, only the first solution works for me. If there is a modification in the .contains() function that works or if there is a certain script I have to write, it would be very useful. I wasn't able to find much online about this.
I am working on React.js with expo to host the app.
First, we convert the string to an array using 'split'.
Since we separate the games in the string with ', ' your code should be like:
tsf.split(', '); // we receive: ['mario','zelda'].
Then we use 'some' method to check if some of the elements in the array we created are in the 'nintedoGames' array.
const tsf = 'mario, zelda';
const nintendoGames = ['mario', 'pokemon', 'mortal kombat', 'zelda', 'tetris'];
const result = tsf.split(', ').some(game => nintendoGames.includes(game.toLowerCase()));
console.log(result)
let nintendoGames = ['mario','pokemon','mortal kombat','zelda','tetris'];
let str = "This is a mario games where you can play the way you like.";
if(nintendoGames.some(n => str.toLowerCase().includes(n.toLowerCase())))
alert("This game was licensed by Nintendo");
I'm throwing together a poll command for my discord bot, and need emojis as options. However, when I try to react with default emojis I got from the message, it fails. I won't get into too many specifics, but here is almost all the relevant code:
// This just takes the <prefix> poll off then splits it by newlines
const args = message.content.slice(message.content.indexOf("poll")+"poll ".length).split("\n");
const fields = [];
const reactions = [];
// First arg is title
for(let i = 1; i < args.length; i++){
const splits = args[i].split("-"); // Split each arg by -
fields.push(splits[0] + ": " + splits[1]); // Field title
fields.push(splits[2]); // Field description
reactions.push(splits[0]); // This should be an emoji
}
// This function takes in message, embed title, [field title, field description, ...]
const msg = await embedGenerator.sendNormalEmbed(message, args[0], "", fields);
// React
for(let i = 0; i < reactions.length; i++){
await msg.react(reactions[i])
}
This works perfectly fine with custom emojis, however, it fails with default Unicode emojis:
The second poll gives me this error:
After some looking the put request makes a call that should react, however, the ID of the emoji is the Unicode code for 🚁. Is there any way to get a normal emoji instead of this Unicode code from the text, and if not is there a way to force discord to react with the Unicode code?
I was bit confused in your question, So maybe I am not correct. if it is, so please comment and ping me. You can also ask me for other help.
Reason:-
So discord does not use id for reacting of default emojis which are already in discord. If you want to implement ":helicopter:" as reaction, You need to replace it with this "🚁".
Solution:-
You can still solve it by telling user to insert "\" before ":helicopter:" so that emoji becomes unicode or there's an npm package for that, this.
Also you can refer to this site to copy and paste them or this for help on emojis.
I'm trying to figure out how to set up a reason argument properly for my moderation commands.
I plan to have it be executed like this:
!warn <#UserMention> [reason]
I have it set up so any word that comes after the command name would be sliced into an array of arguments.
let messageArray = message.content.split(/\s+/g);
let cmd = messageArray[0];
let args = messageArray.slice(1);
let command = cmd.slice(prefix.length);
I could just have moderators put the reasons between quotes so the console reads it as one string, but that creates a slight inconvenience. How would I splice any amount of arguments starting at args[1] to make a full string labeled "reason"?
If I understand you correctly, all you're having trouble with is getting a single message instead an array of arguments.
You can just Array#join the arguments:
let msg = messageArray.slice(1).join(' ');
This will join all the words of the message with a single space. See the MDN documentation.
The easiest is to use array.Reduce
someArray = ["I","like","icecream"]
someArray.reduce((total, value) => total + value, "")
//becomes "Ilikeicecream"
Feel free to add spaces at the end of each word.
I have spent hours, close to 8 hours none stop on this, I am trying to use jQuery/JS to create two arrays, one which is dynamic as it is loading a chat script and will be split by whitespace in to an array, for example:
String: Hello my name is Peter
Converted to (message) array: ['hello','my','name','is','peter'];
I have a set array to look out for specific words, in this example let us use:
(find array) ['hello','peter'] however, this array is going to contain up to 20 elements and I need to ensure it searches the message array efficiently, please help.
I can help you with that.
var arrayOfWords = $(".echat-shared-chat-message-body").last().text().split(" ");
That code is actually working! i went to an open chat in this website so I can tested.
So just replace the word REPLACE with your DOM object :)
var arrayOfWords = $("REPLACE").last().text().split(" ");
If I understood well, you're asking to filter an array of string (from an incoming string) given a second array.
In your described case you'll certainly not have to worry about efficiency, really. Unless your incoming message is allowed to be very very big.
Given that, there is a dozen of options, I think this is the most succinct:
const whitelist = [
'hello',
'peter'
]
const message = 'hello my name is Peter'.split(' ')
const found = message.filter(function(word) {
return whitelist.indexOf(word) > -1
}
You can treat invariant case:
const whitelistLower = whitelist.toLowerCase()
const foundInvariantCase = message.filter(function(word) {
return whitelist.indexOf(word.toLowerCase()) > -1
}
Or use ESS Set:
const whitelistSet = new Set(whitelist)
const found = message.filter(function(word) {
return whitelistSet.has(word)
}