如何将代码从v11迁移到Discord.js v12



我升级到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引入的突破性更改?

以下是人们在Discord.js v12中遇到的一些最常见的突破性更改。

经理

Client#usersGuild#roles等属性现在是管理器,而不是项的缓存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事件。

图像URL

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

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

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

更多信息

要了解有关v12突破性更改的更多信息,请查看更新指南和更改日志。文档也是查找特定方法/属性的良好资源。

最新更新