npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@elara-services/twitter

v2.0.4

Published

Package to help you get twitter posts in your Discord servers easier!

Downloads

15

Readme

What is this package?

This announces / tells you when someone posts.

Support Server

https://my.elara.services/support

Getting Started

Note: For this package you need 1 string from the Twitter developer page. bearer_token is required for this package to work properly.

Creating the Twitter client

const { Twitter } = require("@elara-services/twitter");
const twitter = new Twitter({
    BearerToken: "Your bearer_token from the Twitter developer page.",
    defaultAnnouncements: true, // For the default announcement embeds (if set to false then it won't announce anything, you'll have to listen to the 'stream:post' event to create and announce with your own custom content / embeds)
    updateRulesOnStart: true, // For the package to automatically update the Twitter Stream-V2 rules 
})

Adding Users


twitter.addUser({ 
    name: "The Twitter user's username", // [REQUIRED]: The Twitter user's full username (NOT DISPLAY NAME)
    color: 0xFF0000, // [NOT_REQUIRED]: The embed's color
    webhooks: [ // [REQUIRED]: The webhook urls to announce the tweets in for the creator
        "https://discord.com/api/webhooks/..."
    ]
})
// OR For multiple users

twitter.addUsers([
    { 
        name: "The Twitter user's username", // [REQUIRED]: The Twitter user's full username (NOT DISPLAY NAME)
        color: 0xFF0000, // [NOT_REQUIRED]: The embed's color
        webhooks: [ // [REQUIRED]: The webhook urls to announce the tweets in for the creator
            "https://discord.com/api/webhooks/..."
        ]
    },
    { ... }
])

Starting the Twitter Stream

await twitter.start()
// This will start the twitter stream for the provided users.
// NOTE: You have to restart the twitter stream if you want to add more users! (THIS IS A TWITTER STREAM API LIMITATION)

Listening to Events


// Stream Start
twitter.on("stream:start", ({ start }) => console.log(`The twitter stream started!`, start));

// Stream End
twitter.on("stream:disconnect", (response) => console.log(`The twitter stream disconnect!`, response));

// Stream Error
twitter.on("stream:error", (error) => console.warn(`The twitter stream had an error!`, error));

// Stream Post
twitter.on("stream:post", (body, user) => console.log(`New tweet!`, body, user));
// User should always return { id: string, color: string | number, webhooks: [] }

// Webhook Errors
twitter.on("webhook:error", (error) => console.log(`We've hit an error while sending to the webhook!`, error));

Formatting the Twitter Post Data

const body = twitter.fetchData(postData, userData);
console.log(body);
// Note: This can return 'null' and 'userData' isn't required for this function

Fetching a user

const user = await twitter.fetchUser("SUPERCHIEFYT").catch(() => {});
console.log(user);
// Returns the fetched user data 

Customizing the Twitter announcements

twitter.on("stream:post", async (post) => {
    const user = post.includes.users.find(c => c.id === post.data.author_id);
    if (!user) return;
    let data = twitter.fetchData(post, twitter.data.find(c => c.name === user.name));
    if (!data) return;
    return twitter.send({
        webhook: data.webhooks,
        username: "Whatever you want the webhook's name to be called",
        avatar_url: "The image url you want the webhook to have",
        content: "Message Content, if you want it",
        embeds: [
            {
                title: "Customize the embed(s)"
            }
        ],
        components: [] // Add components (ONLY WORKS FOR APPLICATION/BOT OWNED WEBHOOKS, DISCORD LIMITATION)
    })

    // NOTE: 'content', 'embeds' or 'components' You need one of those for anything to be sent!
})