【问题标题】:How can I migrate my code to Discord.js v12 from v11?如何将我的代码从 v11 迁移到 Discord.js v12?
【发布时间】:2021-01-02 18:32:29
【问题描述】:

我升级到 Discord.js v12,但它破坏了我现有的 v11 代码。以下是一些导致错误的示例:

// TypeError: client.users.get is not a function
const user = client.users.get('123456789012345678')

// TypeError: message.guild.roles.find is not a function
const role = message.guild.roles.find(r => r.name === 'Admin')

// TypeError: message.member.addRole is not a function
await message.member.addRole(role)

// TypeError: message.guild.createChannel is not a function
await message.guild.createChannel('welcome')

// TypeError: message.channel.fetchMessages is not a function
const messages = await message.channel.fetchMessages()

const {RichEmbed} = require('discord.js')
// TypeError: RichEmbed is not a constructor
const embed = new RichEmbed()

const connection = await message.channel.join()
// TypeError: connection.playFile is not a function
const dispatcher = connection.playFile('./music.mp3')

如何将我的代码迁移到 Discord.js v12 并修复这些错误?我在哪里可以看到引入的重大更改 v12?

【问题讨论】:

  • 致以“需要更多关注”为由投票结束此问题的人:这个问题是this meta discussion 提出的一个规范问题,该问题将回答与升级到 Discord.js v12 相关的问题,最值得注意的是管理人员的介绍。有a lot 的与升级相关的问题已被标记为该问题的重复项。我不认为这个问题太宽泛。如果这被分成多个问题,它将具有相同的信息(阅读迁移指南和文档)。
  • 如果您仍然认为这个问题过于宽泛,请随时就 meta 展开讨论,但在这个阶段我认为不需要对这个问题采取任何行动,尤其是考虑到 Discord.js v13已发布。

标签: javascript discord.js


【解决方案1】:

以下是人们遇到的 Discord.js v12 中引入的一些最常见的重大更改。

经理

Client#usersGuild#roles 等属性现在是ma​​nagers,而不是缓存的Collection 项。要访问此集合,请使用 cache 属性:

const user = client.users.cache.get('123456789012345678')
const role = message.guild.roles.cache.find(r => r.name === 'Admin')

此外,GuildMember#addRoleGuild#createChannelTextBasedChannel#fetchMessages 等方法已移至各自的管理器:

await message.member.roles.add(role)
await message.guild.channels.create('welcome')
const messages = await message.channel.messages.fetch()

Collection

Collection 类(例如 client.users.cacheguild.roles.cacheguild.channels.cache)现在只接受 函数,而不接受 .find.findKey 的属性键和值:

// v11: collection.find('property', 'value')
collection.find(item => item.property === 'value')

.exists.deleteAll.filterArray.findAll 也已被删除:

// v11: collection.exists('property', 'value')
collection.some(item => item.property === 'value')

// v11: collection.deleteAll()
Promise.all(collection.map(item => item.delete()))

// v11: collection.filterArray(fn)
collection.filter(fn).array()

// v11: collection.findAll('property', value')
collection.filter(item => item.property === 'value').array()

.tap 现在对集合而不是集合中的每个项目运行一个函数:

// v11: collection.tap(item => console.log(item))
collection.each(item => console.log(item))

// New .tap behaviour:
collection.tap(coll => console.log(`${coll.size} items`))

RichEmbed/MessageEmbed

RichEmbed 类已被移除;改用 MessageEmbed 类,该类现在用于所有嵌入(而不是仅接收到的嵌入)。

const {MessageEmbed} = require('discord.js')
const embed = new MessageEmbed()

addBlankField 方法也已被删除。该方法只是简单地添加了一个带有零宽度空格 (\u200B) 的字段作为名称和值,因此要添加一个空白字段,请执行以下操作:

embed.addField('\u200B', '\u200B')

语音

所有VoiceConnection/VoiceBroadcast#play***方法都统一在一个play方法下:

const dispatcher = connection.play('./music.mp3')

Client#createVoiceBroadcast 已移至ClientVoiceManager

const broadcast = client.voice.createVoiceBroadcast()

此外,StreamDispatcher 扩展了 Node.js 的 stream.Writable,因此请使用 dispatcher.destroy() 而不是 dispatcher.end()end 事件已被移除,取而代之的是原生 finish 事件。

图片网址

User#displayAvatarURLGuild#iconURL 等属性现在是方法

const avatar = user.displayAvatarURL()
const icon = mesage.guild.iconURL()

您还可以传递ImageURLOptions 来自定义格式和大小等内容。

更多信息

要了解有关 v12 重大更改的更多信息,请查看 the updating guidechangelogdocumentation 也是查找特定方法/属性的好资源。

【讨论】:

    猜你喜欢
    • 2021-04-09
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多