How to create arguments from a message












0















I am trying to make a music bot for my discord server, I have it set to play the music, but I can't figure out how to make it play a link that the user inputs



client.on("message", message => {
if (message.content.startsWith("^play")) {
let channel = client.channels.get('496722898858278912');
const ytdl = require('ytdl-core');
const streamOptions = {
seek: 0,
volume: 1
};
const broadcast = client.createVoiceBroadcast();

channel.join()
.then(connection => {
const stream = ytdl(('https://www.youtube.com/watch?v=XAWgeLF9EVQ'), {filter: 'audioonly'});
broadcast.playStream(stream);
const dispatcher = connection.playBroadcast(broadcast);
});
}
});


The link in the code would be replaced with the user submitted link.










share|improve this question

























  • const stream = ytdl((message.content.replace("^play", ""), { filter : 'audioonly' }); This will remove ^play, so there should be only the link. But you must check if it's a link, if it's a youtube video link and if the video exists.

    – TrueTiem
    Nov 20 '18 at 17:21


















0















I am trying to make a music bot for my discord server, I have it set to play the music, but I can't figure out how to make it play a link that the user inputs



client.on("message", message => {
if (message.content.startsWith("^play")) {
let channel = client.channels.get('496722898858278912');
const ytdl = require('ytdl-core');
const streamOptions = {
seek: 0,
volume: 1
};
const broadcast = client.createVoiceBroadcast();

channel.join()
.then(connection => {
const stream = ytdl(('https://www.youtube.com/watch?v=XAWgeLF9EVQ'), {filter: 'audioonly'});
broadcast.playStream(stream);
const dispatcher = connection.playBroadcast(broadcast);
});
}
});


The link in the code would be replaced with the user submitted link.










share|improve this question

























  • const stream = ytdl((message.content.replace("^play", ""), { filter : 'audioonly' }); This will remove ^play, so there should be only the link. But you must check if it's a link, if it's a youtube video link and if the video exists.

    – TrueTiem
    Nov 20 '18 at 17:21
















0












0








0








I am trying to make a music bot for my discord server, I have it set to play the music, but I can't figure out how to make it play a link that the user inputs



client.on("message", message => {
if (message.content.startsWith("^play")) {
let channel = client.channels.get('496722898858278912');
const ytdl = require('ytdl-core');
const streamOptions = {
seek: 0,
volume: 1
};
const broadcast = client.createVoiceBroadcast();

channel.join()
.then(connection => {
const stream = ytdl(('https://www.youtube.com/watch?v=XAWgeLF9EVQ'), {filter: 'audioonly'});
broadcast.playStream(stream);
const dispatcher = connection.playBroadcast(broadcast);
});
}
});


The link in the code would be replaced with the user submitted link.










share|improve this question
















I am trying to make a music bot for my discord server, I have it set to play the music, but I can't figure out how to make it play a link that the user inputs



client.on("message", message => {
if (message.content.startsWith("^play")) {
let channel = client.channels.get('496722898858278912');
const ytdl = require('ytdl-core');
const streamOptions = {
seek: 0,
volume: 1
};
const broadcast = client.createVoiceBroadcast();

channel.join()
.then(connection => {
const stream = ytdl(('https://www.youtube.com/watch?v=XAWgeLF9EVQ'), {filter: 'audioonly'});
broadcast.playStream(stream);
const dispatcher = connection.playBroadcast(broadcast);
});
}
});


The link in the code would be replaced with the user submitted link.







javascript discord.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 18:56









Federico Grandi

3,11321229




3,11321229










asked Nov 20 '18 at 16:41









GarbearGarbear

32




32













  • const stream = ytdl((message.content.replace("^play", ""), { filter : 'audioonly' }); This will remove ^play, so there should be only the link. But you must check if it's a link, if it's a youtube video link and if the video exists.

    – TrueTiem
    Nov 20 '18 at 17:21





















  • const stream = ytdl((message.content.replace("^play", ""), { filter : 'audioonly' }); This will remove ^play, so there should be only the link. But you must check if it's a link, if it's a youtube video link and if the video exists.

    – TrueTiem
    Nov 20 '18 at 17:21



















const stream = ytdl((message.content.replace("^play", ""), { filter : 'audioonly' }); This will remove ^play, so there should be only the link. But you must check if it's a link, if it's a youtube video link and if the video exists.

– TrueTiem
Nov 20 '18 at 17:21







const stream = ytdl((message.content.replace("^play", ""), { filter : 'audioonly' }); This will remove ^play, so there should be only the link. But you must check if it's a link, if it's a youtube video link and if the video exists.

– TrueTiem
Nov 20 '18 at 17:21














1 Answer
1






active

oldest

votes


















0














To create arguments, you can split the content of the message using ' ' as separator, the arguments are the elements of that array except for the first (that's the command):



// message.content: "^play https://youtube.com/blablabla other arguments"
let args = message.content.split(' '); // ["^play", "https://youtube.com/blablabla", "other", "arguments"]: the string got splitted into different parts
args.shift(); // remove the first element (the command)
// you can do all your stuff, when you need the link you find it in args[0]
const stream = ytdl((args[0]), { filter : 'audioonly' });


Please note that the first argument is not granted to be a valid youtube link, so check it or prepare your code to handle an invalid argument.






share|improve this answer
























  • Thank you! I have tried many different ways to do this, but yours is the only one that completely worked!

    – Garbear
    Nov 21 '18 at 2:55











  • Happy to hear that :)

    – Federico Grandi
    Nov 21 '18 at 14:55













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397613%2fhow-to-create-arguments-from-a-message%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














To create arguments, you can split the content of the message using ' ' as separator, the arguments are the elements of that array except for the first (that's the command):



// message.content: "^play https://youtube.com/blablabla other arguments"
let args = message.content.split(' '); // ["^play", "https://youtube.com/blablabla", "other", "arguments"]: the string got splitted into different parts
args.shift(); // remove the first element (the command)
// you can do all your stuff, when you need the link you find it in args[0]
const stream = ytdl((args[0]), { filter : 'audioonly' });


Please note that the first argument is not granted to be a valid youtube link, so check it or prepare your code to handle an invalid argument.






share|improve this answer
























  • Thank you! I have tried many different ways to do this, but yours is the only one that completely worked!

    – Garbear
    Nov 21 '18 at 2:55











  • Happy to hear that :)

    – Federico Grandi
    Nov 21 '18 at 14:55


















0














To create arguments, you can split the content of the message using ' ' as separator, the arguments are the elements of that array except for the first (that's the command):



// message.content: "^play https://youtube.com/blablabla other arguments"
let args = message.content.split(' '); // ["^play", "https://youtube.com/blablabla", "other", "arguments"]: the string got splitted into different parts
args.shift(); // remove the first element (the command)
// you can do all your stuff, when you need the link you find it in args[0]
const stream = ytdl((args[0]), { filter : 'audioonly' });


Please note that the first argument is not granted to be a valid youtube link, so check it or prepare your code to handle an invalid argument.






share|improve this answer
























  • Thank you! I have tried many different ways to do this, but yours is the only one that completely worked!

    – Garbear
    Nov 21 '18 at 2:55











  • Happy to hear that :)

    – Federico Grandi
    Nov 21 '18 at 14:55
















0












0








0







To create arguments, you can split the content of the message using ' ' as separator, the arguments are the elements of that array except for the first (that's the command):



// message.content: "^play https://youtube.com/blablabla other arguments"
let args = message.content.split(' '); // ["^play", "https://youtube.com/blablabla", "other", "arguments"]: the string got splitted into different parts
args.shift(); // remove the first element (the command)
// you can do all your stuff, when you need the link you find it in args[0]
const stream = ytdl((args[0]), { filter : 'audioonly' });


Please note that the first argument is not granted to be a valid youtube link, so check it or prepare your code to handle an invalid argument.






share|improve this answer













To create arguments, you can split the content of the message using ' ' as separator, the arguments are the elements of that array except for the first (that's the command):



// message.content: "^play https://youtube.com/blablabla other arguments"
let args = message.content.split(' '); // ["^play", "https://youtube.com/blablabla", "other", "arguments"]: the string got splitted into different parts
args.shift(); // remove the first element (the command)
// you can do all your stuff, when you need the link you find it in args[0]
const stream = ytdl((args[0]), { filter : 'audioonly' });


Please note that the first argument is not granted to be a valid youtube link, so check it or prepare your code to handle an invalid argument.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 18:54









Federico GrandiFederico Grandi

3,11321229




3,11321229













  • Thank you! I have tried many different ways to do this, but yours is the only one that completely worked!

    – Garbear
    Nov 21 '18 at 2:55











  • Happy to hear that :)

    – Federico Grandi
    Nov 21 '18 at 14:55





















  • Thank you! I have tried many different ways to do this, but yours is the only one that completely worked!

    – Garbear
    Nov 21 '18 at 2:55











  • Happy to hear that :)

    – Federico Grandi
    Nov 21 '18 at 14:55



















Thank you! I have tried many different ways to do this, but yours is the only one that completely worked!

– Garbear
Nov 21 '18 at 2:55





Thank you! I have tried many different ways to do this, but yours is the only one that completely worked!

– Garbear
Nov 21 '18 at 2:55













Happy to hear that :)

– Federico Grandi
Nov 21 '18 at 14:55







Happy to hear that :)

– Federico Grandi
Nov 21 '18 at 14:55






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397613%2fhow-to-create-arguments-from-a-message%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents