如何将我的代码从v11迁移到Discord.js v12? [英] How can I migrate my code to Discord.js v12 from v11?
问题描述
我升级到Discord.js v12,但是它破坏了我现有的v11代码。以下是导致错误的某些示例:
I upgraded to Discord.js v12, but it broke my existing v11 code. Here are some examples of things that cause errors:
// 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引入的重大更改?
How can I migrate my code to Discord.js v12 and fix these errors? Where can I see the breaking changes v12 introduced?
推荐答案
以下是Discord.js v12中引入的一些最常见的重大更改。
Here are some of the most common breaking changes introduced in Discord.js v12 that people run into.
诸如 Client#users
和 Guild#roles
现在是经理,而不是缓存的 Collection
物品。要访问此集合,请使用 缓存
属性:
Properties such as Client#users
and Guild#roles
are now managers, instead of the cached Collection
of items. To access this collection, use the cache
property:
const user = client.users.cache.get('123456789012345678')
const role = message.guild.roles.cache.find(r => r.name === 'Admin')
此外,诸如 GuildMember#addRole
, Guild#createChannel $ c $之类的方法c>和
TextBasedChannel#fetchMessages
已移至各自的管理器:
In addition, methods such as GuildMember#addRole
, Guild#createChannel
, and TextBasedChannel#fetchMessages
have moved to the respective managers:
await message.member.roles.add(role)
await message.guild.channels.create('welcome')
const messages = await message.channel.messages.fetch()
Collection
集合
类(例如 client.users.cache
, guild.roles.cache
)现在仅接受功能,不是属性键和值,对于 .find
和 .findKey
:
Collection
The Collection
class (e.g. client.users.cache
, guild.roles.cache
) now only accepts functions, not property keys and values, for .find
and .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
现在在集合上而不是集合中的每个项目上运行一个函数:
.tap
now runs a function on the collection instead of every item in the collection:
// 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
类,该类现在用于所有嵌入(而不是刚刚收到的嵌入)。
RichEmbed
/MessageEmbed
The RichEmbed
class has been removed; use the MessageEmbed
class instead which is now used for all embeds (instead of just received embeds).
const {MessageEmbed} = require('discord.js')
const embed = new MessageEmbed()
addBlankField
方法也已删除。此方法只是添加了一个零宽度空格( cu200B
)的字段作为名称和值,因此要添加一个空白字段,请执行以下操作:
The addBlankField
method has also been removed. This method simply added a field with a zero-width space (\u200B
) as the name and value, so to add a blank field do this:
embed.addField('\u200B', '\u200B')
语音
所有 VoiceConnection
/ VoiceBroadcast #play ***
方法已统一在一个 播放
方法:
Voice
All of the VoiceConnection
/VoiceBroadcast#play***
methods have been unified under a single play
method:
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
事件。
Additionally, StreamDispatcher
extends Node.js' stream.Writable
, so use dispatcher.destroy()
instead of dispatcher.end()
. The end
event has been removed in favour of the native finish
event.
诸如 User#displayAvatarURL
和 Guild#iconURL
之类的属性现在是方法:
Properties such as User#displayAvatarURL
and Guild#iconURL
are now methods:
const avatar = user.displayAvatarURL()
const icon = mesage.guild.iconURL()
您还可以传递 ImageURLOptions
自定义格式和大小等内容。
You can also pass an ImageURLOptions
to customise things like format and size.
要了解有关v12重大更改的更多信息,请查看更新指南和更改日志。 文档也是查找特定方法的好资源/属性。
To find out more about the v12 breaking changes, take a look at the updating guide and the changelog. The documentation is also a good resource for finding a particular method/property.
这篇关于如何将我的代码从v11迁移到Discord.js v12?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!