0.0.3 • Published 2 years ago

manbo-collector v0.0.3

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

Collector

A Collector for manbo based bots

Features

  • Works with latest manbo package.
  • Supports MessageCollector and awaitMessages.
  • Supports ReactionCollector and awaitReactions.
  • Supports InteractionCollector and awaitInteractions.
  • Configurable message, channel, guild, interaction options.
  • Configurable time, idle, dispose, max options.
  • Active community and fast support.

Install

NPM

npm install manbo-collector

Yarn

yarn add manbo-collector

PNPM

pnpm add manbo-collector

API

Message

MessageCollector

Create a collector to collect messages in the channel

Parameters:

  • client: The client to apply a collector
    • Type: Manbo.Client (see more information in Client)
    • Required: true
  • channel: The channel to collect messages
    • Type: Manbo.TextChannel (see more information in TextChannel)
    • Required: true
  • options: Collector options

    optionstypedescription
    dispose?booleanWhether to dispose data when it's deleted
    idle?numberHow long to stop the collector after inactivity in milliseconds
    max?numberThe maximum total amount of data to collect
    time?numberHow long to run the collector for in milliseconds
    filter?functionThe filter applied to this collector

Methods:

  • stop: Stop this collector

    paramdefaultdescription
    reason?userThe reason to stop this collector

    Example:

    collector.stop('custom reason')
    collector.stop() // reason: `user` (default)
  • resetTimer: Reset the time to end this collector

    • ResetTimerOptions

      paramtypedescription
      idle?numberHow long to stop the collector after inactivity in milliseconds
      time?numberHow long to run the collector for in milliseconds

    Example:

    collector.resetTimer({idle: 15_000, time: 30_000})
    collector.resetTimer({idle: 150_000})
    collector.resetTimer({time: 30_000})
  • empty: Empty the collected data of this collector

    Example:

    collector.empty()

Examples:

import { MessageCollector } from 'manbo-collector'
import { Client, Constants, Message } from 'manbo'

const client = new Client("Bot BOT_TOKEN", {
    intents: Constants.Intents.all
})

client.on('messageCreate', async (message: Message) => {
    // its only to show how collector can be used,
    // you need to configure your own message handler in oreder to use collector precisely

    // filter message, only collect original message author's message
    const filterFunction = (msg: Message) => msg.author.id === message.author.id

    const collector = new MessageCollector(client, message.channel, {
        dispose: true, // delete data when collector ends
        idle: 10_000, // stop collector when there are no messaegs for 10 seconds
        max: 5, // collect max five messages
        time: 30_000, // end collector after 30 seconds
        filter: filterFunction
    })

    collector.on('collect', (collectedMessage) => {
        console.log(`collected message: ${collectedMessage.id}`)
    })

    collector.on('end', (collectedMessages) => {
        console.log(`collected ${collectedMessages.length} messages`)
    })
})
const { MessageCollector } = require('manbo-collector')
const { Client, Constants } = require('manbo')

const client = new Client("Bot BOT_TOKEN", {
    intents: Constants.Intents.all
})

client.on('messageCreate', async (message) => {
    // its only to show how collector can be used,
    // you need to configure your own message handler in oreder to use collector precisely

    // filter message, only collect original message author's message
    const filterFunction = (msg) => msg.author.id === message.author.id

    const collector = new MessageCollector(client, message.channel, {
        dispose: true, // delete data when collector ends
        idle: 10_000, // stop collector when there are no messaegs for 10 seconds
        max: 5, // collect max five messages
        time: 30_000, // end collector after 30 seconds
        filter: filterFunction
    })

    collector.on('collect', (collectedMessage) => {
        console.log(`collected message: ${collectedMessage.id}`)
    })

    collector.on('end', (collectedMessages) => {
        console.log(`collected ${collectedMessages.length} messages`)
    })
})

awaitMessages

A method to collect messages in TextChannel

Parameters:

  • client: The client to apply in a method
    • Type: Manbo.Client (see more information in Client)
    • Required: true
  • channel: The channel to collect messages
    • Type: Manbo.TextChannel (see more information in TextChannel)
    • Required: true
  • options: Collect options

    optionstypedescription
    dispose?booleanWhether to dispose data when it's deleted
    idle?numberHow long to stop this collecting method after inactivity in milliseconds.
    max?numberThe maximum total amount of data to collect
    time?numberHow long to run this collecting method for in milliseconds
    filter?functionThe filter applied to this collecting method
  • Returns:

    Promise<Manbo.Message[]> (see more information in awaitMessages)

Examples:

import { awaitMessages } from 'manbo-collector'
import { Client, Constants, Message } from 'manbo'

const client = new Client("Bot BOT_TOKEN", {
    intents: Constants.Intents.all
})

client.on('messageCreate', async (message: Message) => {
    // its only to show how collector can be used,
    // you need to configure your own message handler in oreder to use collector precisely

    // filter message, only collect original message author's message
    const filterFunction = (msg: Message) => msg.author.id === message.author.id

    const collectedMessages = await awaitMessages(client, message.channel, {
        dispose: true, // delete data when collector ends
        idle: 10_000, // stop collector when there are no messaegs for 10 seconds
        max: 5, // collect max five messages
        time: 30_000, // end collector after 30 seconds
        filter: filterFunction
    })

    console.log(`collected ${collectedMessages.length} messages`)
})
const { awaitMessages } = require('manbo-collector')
const { Client, Constants } = require('manbo')

const client = new Client("Bot BOT_TOKEN", {
    intents: Constants.Intents.all
})

client.on('messageCreate', async (message) => {
    // its only to show how collector can be used,
    // you need to configure your own message handler in oreder to use collector precisely

    // filter message, only collect original message author's message
    const filterFunction = (msg) => msg.author.id === message.author.id

    const collectedMessages = await awaitMessages(client, message.channel, {
        dispose: true, // delete data when collector ends
        idle: 10_000, // stop collector when there are no messaegs for 10 seconds
        max: 5, // collect max five messages
        time: 30_000, // end collector after 30 seconds
        filter: filterFunction
    })

    console.log(`collected ${collectedMessages.length} messages`)
})

Reaction

ReactionCollector

Create a collector to collect message reactions in a message

same as MessageCollector but with reactions

awaitReactions

A method to collect message reactions in a message

same as awaitMessages but with reactions

too lazy to add same of this again and again and again and again...

Interaction

InteractionCollector

Create a collector to collect interactions in the channel

Parameters:

  • client: The client to apply a collector

    • Type: Manbo.Client (see more information in Client)
    • Required: true
  • options: Collector options

    optionstypedescription
    dispose?booleanWhether to dispose data when it's deleted.
    idle?numberHow long to stop the collector after inactivity in milliseconds.
    max?numberThe maximum total amount of data to collect
    time?numberHow long to run the collector for in milliseconds
    filter?functionThe filter applied to this collector
    channel?TextChannelThe channel to listen to interactions from
    guild?GuildThe guild to listen to interactions from
    interaction?AutoCompleteInteraction or CommandInteraction or ComponentInteractionThe interaction response to listen to message component interactions from
    message?MessageThe message to listen to interactions from
    componentType?ComponentTypeThe type of components to listen for
    interactionType?InteractionTypeThe type of interactions to listen for

Methods:

  • stop: Stop this collector

    paramdefaultdescription
    reason?userThe reason to stop this collector

    Example:

    collector.stop('custom reason')
    collector.stop() // reason: `user` (default)
  • resetTimer: Reset the time to end this collector

    • ResetTimerOptions

      paramtypedescription
      idle?numberHow long to stop the collector after inactivity in milliseconds
      time?numberHow long to run the collector for in milliseconds

    Example:

    collector.resetTimer({idle: 15_000, time: 30_000})
    collector.resetTimer({idle: 150_000})
    collector.resetTimer({time: 30_000})
  • empty: Empty the collected data of this collector

    Example:

    collector.empty()

Examples:

import { InteractionCollector } from 'manbo-collector'
import { Client, Constants, Message } from 'manbo'

const client = new Client("Bot BOT_TOKEN", {
    intents: Constants.Intents.all
})

client.on('messageCreate', async (message: Message) => {
    // its only to show how collector can be used,
    // you need to configure your own message handler in oreder to use collector precisely

    const msg = await message.channel.createMessage({
        content: 'An InteractionCollector',
        components: [
            {
                type: Constants.ComponentTypes.ACTION_ROW,
                components: [
                    {
                        type: Constants.ComponentTypes.BUTTON,
                        custom_id: `a custom ID of button component`,
                        label: `A label to be displayed in button`,
                        style: Constants.ButtonStyles.SECONDARY,
                        emoji: {
                            id: null,
                            name: '✅'
                        }
                    }
                ]
            }
        ]
    })
    
    
    const collector = new InteractionCollector(client, msg.channel, {
        dispose: true, // delete data when collector ends
        idle: 10_000, // stop collector when there are no messaegs for 10 seconds
        max: 5, // collect max five messages
        time: 30_000 // end collector after 30 seconds
        // I'd prefer to handle all interaction becuase it would cause interaction fail
        // filter: None
    })

    collector.on('collect', async (collectedInteraction) => {
        // defer interaction
        await collectedInteraction.defer(64)
        
        // handle filter
        if (collectedInteraction.member!.id !== message.author.id)
            await collectedInteraction.createFollowup({
                content: `Only message author can use this button!`,
                flags: 64
            })
        else {
            // since i didnt used filter, you can just add internal Collection cleanup or just use filter :>
            await collectedInteraction.createFollowup({
                content: `collected interaction: ${collectedInteraction.id}`,
                flags: 64
            })
            /*
            You can also do things like:
            
            await msg.edit({
                content: `edit original message`
            }) 
            
            or just acknowldge interaction without `.defer()` using `editParent`:
            
            await collectedInteraction.editParent({
                content: `also edits original message but also acknowledgs interaction`
            })
            */
        }
    })

    collector.on('end', (collectedInteractions) => {
        console.log(`collected ${collectedInteractions.length} interactions`)
    })
})
const { MessageCollector } = require('manbo-collector')
const { Client, Constants } = require('manbo')

const client = new Client("Bot BOT_TOKEN", {
    intents: Constants.Intents.all
})

client.on('messageCreate', async (message) => {
    // its only to show how collector can be used,
    // you need to configure your own message handler in oreder to use collector precisely

    const msg = await message.channel.createMessage({
        content: 'An InteractionCollector',
        components: [
            {
                type: Constants.ComponentTypes.ACTION_ROW,
                components: [
                    {
                        type: Constants.ComponentTypes.BUTTON,
                        custom_id: `a custom ID of button component`,
                        label: `A label to be displayed in button`,
                        style: Constants.ButtonStyles.SECONDARY,
                        emoji: {
                            id: null,
                            name: '✅'
                        }
                    }
                ]
            }
        ]
    })


    const collector = new InteractionCollector(client, msg.channel, {
        dispose: true, // delete data when collector ends
        idle: 10_000, // stop collector when there are no messaegs for 10 seconds
        max: 5, // collect max five messages
        time: 30_000 // end collector after 30 seconds
        // I'd prefer to handle all interaction becuase it would cause interaction fail
        // filter: None
    })

    collector.on('collect', async (collectedInteraction) => {
        // defer interaction
        await collectedInteraction.defer(64)

        // handle filter
        if (collectedInteraction.member.id !== message.author.id)
        await collectedInteraction.createFollowup({
            content: `Only message author can use this button!`,
            flags: 64
        })
    else {
            // since i didnt used filter, you can just add internal Collection cleanup or just use filter :>
            await collectedInteraction.createFollowup({
                content: `collected interaction: ${collectedInteraction.id}`,
                flags: 64
            })
            /*
            You can also do things like:
            
            await msg.edit({
                content: `edit original message`
            }) 
            
            or just acknowldge interaction without `.defer()` using `editParent`:
            
            await collectedInteraction.editParent({
                content: `also edits original message but also acknowledgs interaction`
            })
            */
        }
    })

    collector.on('end', (collectedInteractions) => {
        console.log(`collected ${collectedInteractions.length} interactions`)
    })
})

awaitComponentInteractions

A method to collect component interactions

Parameters:

  • client: The client to apply in a method
    • Type: Manbo.Client (see more information in Client)
    • Required: true

  • options: Collect options

    optionstypedescription
    dispose?booleanWhether to dispose data when it's deleted.
    idle?numberHow long to stop the collecting method after inactivity in milliseconds.
    time?numberHow long to run the collecting method for in milliseconds
    filter?functionThe filter applied to this collecting method
    componentType?ComponentTypeThe type of components to listen for
  • Returns:

    Promise<MappedComponentTypes[]> (see more information in MappedComponentTypes)

Examples:

import { awaitMessages } from 'manbo-collector'
import { Client, Constants, Message } from 'manbo'

const client = new Client("Bot BOT_TOKEN", {
    intents: Constants.Intents.all
})

client.on('messageCreate', async (message: Message) => {
    // its only to show how collector can be used,
    // you need to configure your own message handler in oreder to use collector precisely

    // filter message, only collect original message author's message
    const filterFunction = (msg: Message) => msg.author.id === message.author.id

    const collectedMessages = await awaitMessages(client, message.channel, {
        dispose: true, // delete data when collector ends
        idle: 10_000, // stop collector when there are no messaegs for 10 seconds
        max: 5, // collect max five messages
        time: 30_000, // end collector after 30 seconds
        filter: filterFunction
    })

    console.log(`collected ${collectedMessages.length} messages`)
})
const { awaitMessages } = require('manbo-collector')
const { Client, Constants } = require('manbo')

const client = new Client("Bot BOT_TOKEN", {
    intents: Constants.Intents.all
})

client.on('messageCreate', async (message) => {
    // its only to show how collector can be used,
    // you need to configure your own message handler in oreder to use collector precisely

    // filter message, only collect original message author's message
    const filterFunction = (msg) => msg.author.id === message.author.id

    const collectedMessages = await awaitMessages(client, message.channel, {
        dispose: true, // delete data when collector ends
        idle: 10_000, // stop collector when there are no messaegs for 10 seconds
        max: 5, // collect max five messages
        time: 30_000, // end collector after 30 seconds
        filter: filterFunction
    })

    console.log(`collected ${collectedMessages.length} messages`)
})

Events

namecallback paramsdescription
collectcollected (The element collected)Emitted whenever something is collected
disposedisposed (The element disposed)Emitted whenever something is disposed
ignoredignored (The element ignored)Emitted whenever something is ignored
endcollected (The data collected by the collector) reason (The reason the collector has ended)Emitted whenever the collector stops collecting

Example:

collector.on('collect', (collected) => {
    console.log(`collected: ${collected}`)
})
collector.on('dispose', (disposed) => {
    console.log(`disposed: ${disposed}`)
})
collector.on('ignored', (ignored) => {
    console.log(`ignored: ${ignored}`)
})
collector.on('end', (collected, reason) => {
    console.log(`collector ended with reason: ${reason}`)
    console.log(`collected: ${collected}`)
})

Contribute

PRs are always welcomed! Feel free to submit PRs!