@1mmunity/discord-bot-base 中文文档教程

发布于 3年前 浏览 26 更新于 3年前

Table of contents

About

这是一个不和谐的机器人基地,因此更容易启动机器人。 新:斜杠命令 - 想轻松地创建斜杠命令吗? 那么这是给你的!

Installation

npm i @1mmunity/discord-bot-base

Node.js 需要 12.0.0 或更新版本。

Getting Started

Quickstart

  1. Require it in your script.
const DBB = require('@1mmunity/discord-bot-base')
  1. Initialize the DBB Client.
const DBB = require('@1mmunity/discord-bot-base')
const client = new DBB.Client({
  ownerId: 'id'
})

请参阅 ClientOptions 来自文档

  1. Login to your client
const DBB = require('@1mmunity/discord-bot-base')
const client = new DBB.Client({
  ownerId: 'id'
})

client.login('token')
  1. Register defaults (optional)
const DBB = require('@1mmunity/discord-bot-base')
const client = new DBB.Client({
  ownerId: 'id'
})

client.registry.registerAllDefaults()

client.login('token')

这就是快速入门! 注册事件:

  • ready
  • message
  • guildCreate
  • guildDelete

注册命令:

  • eval - Evaluate JavaScript code
  • ban - Bans mentioned members from a guild
  • kick - Kicks mentioned members from a guild
  • warn - DMs users from a guild that they have been warned
  • help - Gets a list of commands
  • ping - Gets client's websocket ping
  • user - Gets a detailed information of a mentioned member

您设置选项 options.events.{event}: Boolean, options.commands.{command}: Boolean, options.prefix : 字符串

Examples

您可以在 Tests 文件夹中查看示例。
standard.js

const DBB = require('../')
const client = new DBB.Client({
  ownerId: 'id'
})

client.registry.registerAllDefaults()

client.login('token')

semi-standard.js

const DBB = require('../')
const client = new DBB.Client({
  ownerId: 'id'
})
const registry = client.registry

registry.registerDefaultCommands()
registry.registerDefaultEvents()
registry.handleEvents()
// registry.handleCommands() is used only on the message event

client.login('token')

non-standard.js

const DBB = require('../')
const client = new DBB.Client({
  ownerId: 'id'
})
const registry = client.registry

registry.addEvents([
  {
    name: 'ready',
    run: (client) => {
      console.log(`Logged in as ${client.user.tag}`)
    }
  },
  {
    name: 'message',
    run: (client, message) => {
      registry.handleCommands(message, {
        ignoreBots: true,
        guildOnly: true,
        prefix: '!',
        prefixRequired: true,
      })
    }
  }
])

// registers ready and message event

registry.addCommands([
  {
    name: 'test',
    aliases: ['t'],
    run: (client, message) => {
      return message.reply(client.user.tag)
    }
  }
])

registry.registerDefaultCommands({
  help: true
})

registry.handleEvents()

// registers test and help commands

client.login('token')

Slash Commands

制作斜杠命令,这是一个实验性功能,

const client = new DBB.Client({
  ownerId: ''
})
const { MessageEmbed } = require('discord.js')
const slashy = client.slash({
  testServers: ['id1', 'id2']
})
// testServers is for developmental reasons, because globally it will take up to an hour.

slashy.addCommand('ping', {
  description: 'Pong',
  run: (_client, message, args) => {
    // args is for options
    // message is for interaction
    // _client is the client that interacted
    // EXAMPLE:
    return message.send('Pong!')
  }
})

// with embed, options & register more than one
slashy.addCommands([
  {
    name: 'embed',
    description: 'Sends an embed',
    run: (_client, message) => {
      return message.send(null, [
        new MessageEmbed().setTitle('Hello World'),
        new MessageEmbed().setTitle('Hello World2')
      ])
      // send(content=String, embeds=Array, hidden=Boolean)
    }
  }, {
    name: 'apply',
    description: 'Apply for Moderator',
    options: [
      {
        name: 'Age',
        description: 'Your age, must be above 18.',
        required: true,
        type: 4 // 1: COMMAND_GROUP | 2: SUB_COMMAND_GROUP | 3: String | 4: Number | 5: Boolean | 6: Mention
      },
      {
        name: 'Reason',
        description: 'Reason to become a moderator.',
        required: true,
        type: 3
      }
    ],
    run: (_client, message, args) => {
      if (args.Age < 18) return message.send('Your age must be above 18 to be a moderator!', null, true) // hidden or ephermal if last parameter is true
      if (args.Reason.toLowerCase() === 'to troll') return message.send('Sorry, we aren\'t looking for trolls.', null, true)
      // ...
      return message.send('Your application has successfully being processed, please wait until further notice.')
    }
  }
])


client.registry.addEvent('ready', () => {
  slashy.handle() // handle Interactions; slashy.postCommands() then slashy.handleCommands()
  console.log('Ready!')
})

client.registry.handleEvents()
client.login('token')
  • slashy.registerCommands(dir, options) - 注册命令,与注册表相同。 registerCommands(dir, options) 但对于斜杠命令,完全作为 slashy.addCommands(commandsArr) 的对象工作

  • >slashy.handleGlobal() - 像 < code>slashy.handle(),但这不适用于测试服务器,而是全局的。 可能最多需要一个小时,因此请使用 slashy.handle() 进行开发。

  • run(client, message, args) => {} - message 是自定义消息类,它不是来自 discord.js 的常规消息对象。

  • message.author - 来自 discord.js 的作者(用户)对象

  • message.member - 将作者表示为来自 discord.js 的

  • 成员hidden) - 您只能在命令中使用一次。 msg 是您要发送的消息(字符串)。 embeds 是您要发送的嵌入的数组不能用于临时命令)。 args 是选项(对象)的值。

Documentation

DBB 的文档。 您可以查看原始的 Discord.js 文档

Client

new DBB.Client(ClientOptions)
Parameter Type Optional Default Description
ClientOptions object false - Options for the client, see ClientOptions
ClientOptions.ownerId string false - The bot owner's ID, used for ownerOnly property in a command

DBB 的属性

方法

  • reportBotVitals - returns an object that has arrays of event and command names

Modules

模块。

Registry

new Modules.Registry(client)
Parameter Type Optional Default Description
client Client false - Client that has this registry

属性

  • client - The client that has this registry

方法

  • registerAllDefaults(options) - Registers default commands, events, and handlers
  • registerDefaultCommands(options) - Registers default commands only
  • registerDefaultEvents(options) - Registers default events only
  • registerCommands(dir, options) - Registers all files as commands in a directory
  • registerEvents(dir) - Registers all files as events in a directory
  • handleEvents(extraArg) - Handles all events IMPORTANT
  • handleCommands(message, options) - Handles incoming messages as commands, used in the message event IMPORTANT
  • addCommand(name, props) - Adds a command with the name of name, command contents are in props
  • addEvent(name, callbackfn) - Similar to the discord.js client.on(), but this adds an event to later be triggered on handleEvents
  • addCommands(commandsArr) - Adds multiple commands as an array, similar to addCommand
  • addEvents(eventsArr) - Adds multiple events as an array, similar to addEvent
  • registerCommand(dir) - Registers a file as a command in the provided file directory
  • registerEvent(dir) - Registers a file as an event in the provided file directory

请参阅示例

Others

reportBotVitals

client.reportBotVitals(options, callbackfn)
Parameter Type Optional Default Description
options object true * What should be included on report
options.commands boolean true true Should commands be included
options.events boolean true true Should events be included
callbackfn Function true - Callback function when vitals are reported
callbackfn(returnObj) object true object, Array> Object including events and commands array on the vitals

License

此包已根据 MIT 许可证获得许可。

Table of contents

About

This is a discord bot base so that it is easier to start a bot. NEW: Slash Commands - Want to make a slash command easily? Well this is for you!

Installation

npm i @1mmunity/discord-bot-base

Node.js 12.0.0 or newer is required.

Getting Started

Quickstart

  1. Require it in your script.
const DBB = require('@1mmunity/discord-bot-base')
  1. Initialize the DBB Client.
const DBB = require('@1mmunity/discord-bot-base')
const client = new DBB.Client({
  ownerId: 'id'
})

See ClientOptions from the documentations

  1. Login to your client
const DBB = require('@1mmunity/discord-bot-base')
const client = new DBB.Client({
  ownerId: 'id'
})

client.login('token')
  1. Register defaults (optional)
const DBB = require('@1mmunity/discord-bot-base')
const client = new DBB.Client({
  ownerId: 'id'
})

client.registry.registerAllDefaults()

client.login('token')

That's it for a quick start! Registered events:

  • ready
  • message
  • guildCreate
  • guildDelete

Registered Commands:

  • eval - Evaluate JavaScript code
  • ban - Bans mentioned members from a guild
  • kick - Kicks mentioned members from a guild
  • warn - DMs users from a guild that they have been warned
  • help - Gets a list of commands
  • ping - Gets client's websocket ping
  • user - Gets a detailed information of a mentioned member

You set options with options.events.{event}: Boolean, options.commands.{command}: Boolean, options.prefix: String.

Examples

You can see examples in the Tests folder.
standard.js

const DBB = require('../')
const client = new DBB.Client({
  ownerId: 'id'
})

client.registry.registerAllDefaults()

client.login('token')

semi-standard.js

const DBB = require('../')
const client = new DBB.Client({
  ownerId: 'id'
})
const registry = client.registry

registry.registerDefaultCommands()
registry.registerDefaultEvents()
registry.handleEvents()
// registry.handleCommands() is used only on the message event

client.login('token')

non-standard.js

const DBB = require('../')
const client = new DBB.Client({
  ownerId: 'id'
})
const registry = client.registry

registry.addEvents([
  {
    name: 'ready',
    run: (client) => {
      console.log(`Logged in as ${client.user.tag}`)
    }
  },
  {
    name: 'message',
    run: (client, message) => {
      registry.handleCommands(message, {
        ignoreBots: true,
        guildOnly: true,
        prefix: '!',
        prefixRequired: true,
      })
    }
  }
])

// registers ready and message event

registry.addCommands([
  {
    name: 'test',
    aliases: ['t'],
    run: (client, message) => {
      return message.reply(client.user.tag)
    }
  }
])

registry.registerDefaultCommands({
  help: true
})

registry.handleEvents()

// registers test and help commands

client.login('token')

Slash Commands

Make slash commands, this is an experimental feature,

const client = new DBB.Client({
  ownerId: ''
})
const { MessageEmbed } = require('discord.js')
const slashy = client.slash({
  testServers: ['id1', 'id2']
})
// testServers is for developmental reasons, because globally it will take up to an hour.

slashy.addCommand('ping', {
  description: 'Pong',
  run: (_client, message, args) => {
    // args is for options
    // message is for interaction
    // _client is the client that interacted
    // EXAMPLE:
    return message.send('Pong!')
  }
})

// with embed, options & register more than one
slashy.addCommands([
  {
    name: 'embed',
    description: 'Sends an embed',
    run: (_client, message) => {
      return message.send(null, [
        new MessageEmbed().setTitle('Hello World'),
        new MessageEmbed().setTitle('Hello World2')
      ])
      // send(content=String, embeds=Array, hidden=Boolean)
    }
  }, {
    name: 'apply',
    description: 'Apply for Moderator',
    options: [
      {
        name: 'Age',
        description: 'Your age, must be above 18.',
        required: true,
        type: 4 // 1: COMMAND_GROUP | 2: SUB_COMMAND_GROUP | 3: String | 4: Number | 5: Boolean | 6: Mention
      },
      {
        name: 'Reason',
        description: 'Reason to become a moderator.',
        required: true,
        type: 3
      }
    ],
    run: (_client, message, args) => {
      if (args.Age < 18) return message.send('Your age must be above 18 to be a moderator!', null, true) // hidden or ephermal if last parameter is true
      if (args.Reason.toLowerCase() === 'to troll') return message.send('Sorry, we aren\'t looking for trolls.', null, true)
      // ...
      return message.send('Your application has successfully being processed, please wait until further notice.')
    }
  }
])


client.registry.addEvent('ready', () => {
  slashy.handle() // handle Interactions; slashy.postCommands() then slashy.handleCommands()
  console.log('Ready!')
})

client.registry.handleEvents()
client.login('token')
  • slashy.registerCommands(dir, options) - Registers commands, the same as registry.registerCommands(dir, options) but for slash commands, works exactly as an object of slashy.addCommands(commandsArr)

  • slashy.handleGlobal() - Works like slashy.handle(), but this is not for test servers, instead globally. Might take up to an hour, so use slashy.handle() for development.

  • run(client, message, args) => {} - message is a custom message class, it IS NOT the regular message object from discord.js.

  • message.author - Author (user) object from discord.js

  • message.member - Represents author as member from discord.js

  • message.send(msg, embeds, hidden) - You can only use this one time in your command. msg is the message you want to send (string). embeds is an array of embeds that you want to send (Can\'t be used in ephermal commands). args is the values of the options (object).

Documentation

Documentations for DBB. You can see the original Discord.js Documentations.

Client

new DBB.Client(ClientOptions)
Parameter Type Optional Default Description
ClientOptions object false - Options for the client, see ClientOptions
ClientOptions.ownerId string false - The bot owner's ID, used for ownerOnly property in a command

Properties

Methods

  • reportBotVitals - returns an object that has arrays of event and command names

Modules

Modules for DBB.

Registry

new Modules.Registry(client)
Parameter Type Optional Default Description
client Client false - Client that has this registry

Properties

  • client - The client that has this registry

Methods

  • registerAllDefaults(options) - Registers default commands, events, and handlers
  • registerDefaultCommands(options) - Registers default commands only
  • registerDefaultEvents(options) - Registers default events only
  • registerCommands(dir, options) - Registers all files as commands in a directory
  • registerEvents(dir) - Registers all files as events in a directory
  • handleEvents(extraArg) - Handles all events IMPORTANT
  • handleCommands(message, options) - Handles incoming messages as commands, used in the message event IMPORTANT
  • addCommand(name, props) - Adds a command with the name of name, command contents are in props
  • addEvent(name, callbackfn) - Similar to the discord.js client.on(), but this adds an event to later be triggered on handleEvents
  • addCommands(commandsArr) - Adds multiple commands as an array, similar to addCommand
  • addEvents(eventsArr) - Adds multiple events as an array, similar to addEvent
  • registerCommand(dir) - Registers a file as a command in the provided file directory
  • registerEvent(dir) - Registers a file as an event in the provided file directory

See Examples

Others

reportBotVitals

client.reportBotVitals(options, callbackfn)
Parameter Type Optional Default Description
options object true * What should be included on report
options.commands boolean true true Should commands be included
options.events boolean true true Should events be included
callbackfn Function true - Callback function when vitals are reported
callbackfn(returnObj) object true object, Array> Object including events and commands array on the vitals

License

This package is licensed under MIT License.

更多

友情链接

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文