Coder Social home page Coder Social logo

inkohx / discord.js-reaction-controller Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 2.0 169 KB

Discord.js Reaction Controller

Home Page: https://www.npmjs.com/package/discord.js-reaction-controller

License: MIT License

TypeScript 100.00%
discordjs discord-bot typescript

discord.js-reaction-controller's Introduction

discord.js-reaction-controller

Scrapbox

Discord.js Reaction Controller

Install

Requirements

  • Discord.js v12.2.0 or later

NPM

npm install discord.js-reaction-controller

Yarn

yarn add discord.js-reaction-controller

Example of usage

const Discord = require('discord.js')
const { ReactionController } = require('discord.js-reaction-controller')

const client = new Discord.Client()

client.on('message', message => {
  if (message.content.startsWith('>pagination')) {
    const controller = new ReactionController(client)

    controller
      .addReactionHandler('๐Ÿค”', (reaction) => {
        reaction.message.channel.send('thinking')
          .catch(console.error)
      })

    controller.addPages([
      new Discord.MessageEmbed().setImage('https://github.com/yyx990803.png'),
      new Discord.MessageEmbed().setImage('https://github.com/egoist.png'),
      new Discord.MessageEmbed().setImage('https://github.com/vercel.png'),
      new Discord.MessageEmbed().setImage('https://github.com/Google.png'),
      new Discord.MessageEmbed().setImage('https://github.com/Microsoft.png')
    ])

    controller.sendTo(message.channel, message.author)
      .catch(console.error)
  }
})

client.login()
  .catch(console.error)

Using Promise (Lazy Loading)

It is recommended to create a function that returns MessageEmbed with Promise and put it in the argument of addPages and addPage when there are many processes to access externally through the network.

That way, "discord.js-reaction-controller" will resolve the promises as needed and cache and display the MessageEmbed.

const { Client, MessageEmbed } = require('discord.js')
const { ReactionController } = require('discord.js-reaction-controller')
const { getBasicInfo } = require('ytdl-core')

const client = new Client()

const fetchYouTubeVideoInfo = videoUrl => async () => {
  const { videoDetails } = await getBasicInfo(videoUrl)

  return new MessageEmbed()
    .setColor('RED')
    .setTitle(videoDetails.title)
    .setURL(videoDetails.video_url)
    .setImage(videoDetails.thumbnails[videoDetails.thumbnails.length - 1].url)
    .setTimestamp(Date.parse(videoDetails.publishDate))
    .setFooter('Uploaded on')
    .setAuthor(videoDetails.author.name, videoDetails.author.thumbnails[0].url, videoDetails.author.channel_url)
}


const videos = [
  'https://youtu.be/sWbD5q769Ms',
  'https://youtu.be/0-zJNiSvz8Q',
  'https://youtu.be/1x2izJEN9p0',
  'https://youtu.be/gNp4VNr44hg',
  'https://youtu.be/Vi_asBY5UX8',
  'https://youtu.be/plqoPcKQnyE',
  'https://youtu.be/308I91ljCWg'
]

client.on('message', message => {
  if (message.content.startsWith('>pagination')) {
    const controller = new ReactionController(client)

    controller
      .addReactionHandler('๐Ÿค”', (reaction) => {
        reaction.message.channel.send('thinking')
          .catch(console.error)
      })

    controller.addPages(videos.map(url => fetchYouTubeVideoInfo(url)))

    controller.sendTo(message.channel, message.author)
      .catch(console.error)
  }
})

client.login()

discord.js-reaction-controller's People

Contributors

dependabot[bot] avatar inkohx avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

waki285

discord.js-reaction-controller's Issues

Discord.js v13

Using v13 I cannot seem to work around DiscordAPIError: Cannot send an empty message with the example programs you provided. I did declare intents and provide a valid Discord token.

const client = new Discord.Client({
    intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES]
});

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.