Skip to content

Manbo-js/collector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

    options type description
    dispose? boolean Whether to dispose data when it's deleted
    idle? number How long to stop the collector after inactivity in milliseconds
    max? number The maximum total amount of data to collect
    time? number How long to run the collector for in milliseconds
    filter? function The filter applied to this collector

Methods:

  • stop: Stop this collector

    param default description
    reason? user The reason to stop this collector

    Example:

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

    • ResetTimerOptions

      param type description
      idle? number How long to stop the collector after inactivity in milliseconds
      time? number How 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:

ESM
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`)
    })
})
CommonJS
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

    options type description
    dispose? boolean Whether to dispose data when it's deleted
    idle? number How long to stop this collecting method after inactivity in milliseconds.
    max? number The maximum total amount of data to collect
    time? number How long to run this collecting method for in milliseconds
    filter? function The filter applied to this collecting method
  • Returns:

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

Examples:

ESM
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`)
})
CommonJS
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

    options type description
    dispose? boolean Whether to dispose data when it's deleted.
    idle? number How long to stop the collector after inactivity in milliseconds.
    max? number The maximum total amount of data to collect
    time? number How long to run the collector for in milliseconds
    filter? function The filter applied to this collector
    channel? TextChannel The channel to listen to interactions from
    guild? Guild The guild to listen to interactions from
    interaction? AutoCompleteInteraction
    or CommandInteraction
    or ComponentInteraction
    The interaction response to listen to message component interactions from
    message? Message The message to listen to interactions from
    componentType? ComponentType The type of components to listen for
    interactionType? InteractionType The type of interactions to listen for

Methods:

  • stop: Stop this collector

    param default description
    reason? user The reason to stop this collector

    Example:

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

    • ResetTimerOptions

      param type description
      idle? number How long to stop the collector after inactivity in milliseconds
      time? number How 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:

ESM
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`)
    })
})
CommonJS
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

    options type description
    dispose? boolean Whether to dispose data when it's deleted.
    idle? number How long to stop the collecting method after inactivity in milliseconds.
    time? number How long to run the collecting method for in milliseconds
    filter? function The filter applied to this collecting method
    componentType? ComponentType The type of components to listen for
  • Returns:

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

Examples:

ESM
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`)
})
CommonJS
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

name callback params description
collect collected (The element collected) Emitted whenever something is collected
dispose disposed (The element disposed) Emitted whenever something is disposed
ignored ignored (The element ignored) Emitted whenever something is ignored
end collected (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!

Releases

No releases published

Packages

No packages published