Git Product home page Git Product logo

revolt-uploader's Introduction

Revolt Uploader

Revolt.js doesn't offer the ability to upload attachments, so here is a utility package to allow easy file uploads.

Installation

npm install revolt-uploader

It's as easy as that! :)

Usage

First, you have to import and initialize an uploader object.

// import the uploader library
const Uploader = require("revolt-uploader");

// you have to initialize a revolt.js client object as well.
// Then initialize the uploader and provide it with the client
const uploader = new Uploader(client);

Now you've got your uploader. All you have to do is to login your bot client using client.login("token")

After that, you can upload files to revolt's servers using the uploadFile method.

// you need to attach this to a message, meaning you need to have a message object
// you can get this by listening for the `message` event on the client object but this is up to you
client.on("message", (message) => {
  // the upload method will return an attachment id that you can add to the message
  Promise.allSettled([
    uploader.uploadFile("path/to/file", "file"),
    uploader.uploadFile("path/to/another/file", "another-file"),
  ]).then(attachments => { // we're using Promise.allSettled to asynchronously upload all of them
    attachments = attachments.map(attachment => attachment.value); // extracting the value from the promises
    // send the attachment to the channel
    message.channel.sendMessage({
        content: "Here is your file!",
        attachments: attachments // Note that attachments always has to be an array, even if you're only uploading one file
    });
    // All done!
  });

  // async/await approach:
  message.channel.sendMessage({
    content: "Here is your file!",
    attachments: [await uploader.uploadFile("/path/to/another/file", "file-name")]
  });
});

Uploading URLs

You can upload files from urls by using the uploadUrl method. It will stream the file from the url to autumn without saving the image on your machine. It works just like a normal file upload:

const id = await uploader.uploadUrl("url", "fileName");

Advanced usage

If you need to upload anything else than existing files, use the .upload(fileData, fileName) method. You can use any kind of data object for the fileData but have to specify the file name.

For example using a stream:

const https = require("https");

client.on("message", (message) => {
  https.get("<url>", (response) => {
    message.channel.sendMessage({
      content: "Downloaded file: ",
      attachments: [await uploader.upload(response, "file.filetype")]
    });
  });
});

Furthermore, it is possible to upload your content under different tags. The default tag is attachments. These tags are available:

  • attachments
  • avatars
  • backgrounds
  • icons
  • banners
  • emojis

All of these have different configurations and limits. See this file for the exact specifications.

revolt-uploader's People

Contributors

austinhuang0131 avatar shadowlp174 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

revolt-uploader's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.