![](/img/trans.png)
[英]How I can update code from discord.js v12 to v13? - Welcome messages
[英]How can I migrate my code to Discord.js v12 from v11?
我升级到 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#users
和Guild#roles
等属性现在是manager ,而不是缓存的项目Collection
。 要访问此集合,请使用cache
属性:
const user = client.users.cache.get('123456789012345678')
const role = message.guild.roles.cache.find(r => r.name === 'Admin')
此外, GuildMember#addRole
、 Guild#createChannel
和TextBasedChannel#fetchMessages
已移至各自的管理器:
await message.member.roles.add(role)
await message.guild.channels.create('welcome')
const messages = await message.channel.messages.fetch()
Collection
Collection
类(例如client.users.cache
, guild.roles.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
方法也已被删除。 此方法只是添加了一个具有零宽度空间 ( \
) 作为名称和值的字段,因此要添加一个空白字段,请执行以下操作:
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#displayAvatarURL
和Guild#iconURL
等属性现在是方法:
const avatar = user.displayAvatarURL()
const icon = mesage.guild.iconURL()
您还可以传递ImageURLOptions
来自定义格式和大小等内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.