0
votes

I´m an Owner of a German Discord Server & Minecraft Server, programming my own Discord bot to solve the problem of typing the same Thing or whatever hundreds of times (News, Givaways, etc.) and need help with the following Problem:

I want to make a command (usually p/[command] with this bot) to send a Givaway News in a specific channel. I thought "No Problem" but I´m stuck at the slicing thing on Discord.js. I want the command to work like i just have to write the Attributes in a row.

p/giveaway [Date of Ending] [Time of Ending] [number of Winners] [Price to Win] [optional conditions to participate] [optional Text as desciption] 

Example:

Example Input:

p/giveaway 01.01.2020 00:00 20 something nothing some text

Example output should be:

Runtime till 01.01.2020 at 00:00 o clock
Noumber of winners: 20
What you can win: something
Conditions: nothing
some text

The Layout is no problem, but i don´t know how to split the messages so the message doesn´t gets messed up. I want the bot to follow a strict template.

If anything is unclear, please feel free to ask.

This is the code i got so far. It works, exept that the output is scuffed because i dont know how to slice the back part of a messaage so it just caches a specific part of the Message.

The code i got so far:

bot.on("message", function(message) {

//     command

        if(message.content.startsWith("p/giveaway")){

//     vars, etc

            let destination = bot.channels.cache.get("[Discord-channel ID here]")
            var timetorun = message.content.split(" ").slice(1).join(" ");
            var TIMEtorun = message.content.split(" ").slice(2).join(" ");
            var winner = message.content.split(" ").slice(3).join(" ");
            var price = message.content.split(" ").slice(4).join(" ");
            var conditions = message.content.split(" ").slice(5).join(" ");
            var text = message.content.split(" ").slice(6).join(" ");

//     Embed

            const GiveawayEmbed = new Discod.MessageEmbed()
            .setColor("#0099ff")
            .setTitle("")
            .setAuthor("Server Giveaway", "https://i.imgur.com/MMiVcKH.png")
            .setThumbnail("https://i.imgur.com/MMiVcKH.png")
            .addFields({ name:"->", value: ("Runtime till " + (timetorun) + " at " + (TIMEtorun) + " o clock" + "\nNoumber of winners: " + (winner) + "\nWhat you can win: " + (price) + "\nConditions: " + (conditions) + "\n" + (text))
            })       
            .setFooter("The general rules of ... apply  -  ... .net")

//     Code

            if(!timetorun || !TIMEtorun || !winner || !price) {
                message.channel.send("\nuse the folling format to use this command: [Date of Ending] [Time of Ending] [number of Winners] [Price to Win] [optional conditions to participate] [optional Text as desciption]");
                return;
            };

//     permissions

            if(!message.member.hasPermission("ADMINISTRATOR")){
                message.channel.send("``` \nYou are not allowed to do that! \n ```");
                return;
            };

//     finaly send

            destination.send(GiveawayEmbed);
        };
};

This is how the Bot actually replyes

Thank you for your help!

1

1 Answers

2
votes

First, we want to split the message into an array of words and store it as a variable:

const args = message.content.split(' ')

With your example input, its value would look like this:

[
  "p/giveaway",
  "01.01.2020",
  "00:00',
  "20',
  "something",
  "nothing",
  "some",
  "text"
]

Now, we can take some time to understand what the .slice() function does. With the fact that, in JavaScript, arrays are indexed from 0, we run args.slice(5). It should return the following:

[
  "nothing",
  "some",
  "text"
]

Notice we now have the string with index 5, and every other string that comes after it. This is why you're getting the output you linked in your post. What you should do instead, is only get the index you're interested in, like so:

args[1] // 01.01.2020
args[4] // something

If you want to better understand how .slice() works, I suggest you give this Mozilla Developer Network page a read (here it is in deutsch too).