@1mmunity/discord-bot-base 中文文档教程
Table of contents
About
这是一个不和谐的机器人基地,因此更容易启动机器人。 新:斜杠命令 - 想轻松地创建斜杠命令吗? 那么这是给你的!
Installation
npm i @1mmunity/discord-bot-base
Node.js 需要 12.0.0 或更新版本。
Getting Started
Quickstart
- Require it in your script.
const DBB = require('@1mmunity/discord-bot-base')
- Initialize the DBB Client.
const DBB = require('@1mmunity/discord-bot-base')
const client = new DBB.Client({
ownerId: 'id'
})
请参阅 ClientOptions 来自文档
- Login to your client
const DBB = require('@1mmunity/discord-bot-base')
const client = new DBB.Client({
ownerId: 'id'
})
client.login('token')
- 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 codeban
- Bans mentioned members from a guildkick
- Kicks mentioned members from a guildwarn
- DMs users from a guild that they have been warnedhelp
- Gets a list of commandsping
- Gets client's websocket pinguser
- 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 handlersregisterDefaultCommands(options)
- Registers default commands onlyregisterDefaultEvents(options)
- Registers default events onlyregisterCommands(dir, options)
- Registers all files as commands in a directoryregisterEvents(dir)
- Registers all files as events in a directoryhandleEvents(extraArg)
- Handles all events IMPORTANThandleCommands(message, options)
- Handles incoming messages as commands, used in themessage
event IMPORTANTaddCommand(name, props)
- Adds a command with the name ofname
, command contents are inprops
addEvent(name, callbackfn)
- Similar to the discord.jsclient.on()
, but this adds an event to later be triggered onhandleEvents
addCommands(commandsArr)
- Adds multiple commands as an array, similar toaddCommand
addEvents(eventsArr)
- Adds multiple events as an array, similar toaddEvent
registerCommand(dir)
- Registers a file as a command in the provided file directoryregisterEvent(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 |
Object including events and commands array on the vitals |
License
此包已根据 MIT 许可证获得许可。
你可能也喜欢
- 6vd-hh5 中文文档教程
- @4geit/rct-notification-menu-component 中文文档教程
- @512ks/nec-common-web 中文文档教程
- @5calls/react-components 中文文档教程
- @aa-test/ngx-bootstrap 中文文档教程
- @aaa-backend-stack/file-storage-local 中文文档教程
- @abdullahceylan/eslint-config-react 中文文档教程
- @abenfield/react-beautiful-dnd 中文文档教程
- @abhishekojha/npmtest 中文文档教程
- @ableneo/prettier-config 中文文档教程