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

5

我升级了 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相关的问题的规范问题,尤其是管理器的引入。有很多与升级相关的问题已被标记为此问题的重复。我不认为这个问题太广泛了;如果将其拆分成多个问题,它将具有相同的信息(阅读迁移指南和文档)。 - Lauren Yim
如果你仍然认为这个问题太宽泛,可以在元社区开启一次讨论,但在目前阶段,我认为对这个问题不需要采取任何行动,特别是考虑到 Discord.js v13 已经发布。 - Lauren Yim
1个回答

12

以下是 Discord.js v12 中一些常见的破坏性更改,会给人们带来影响。

管理器

Client#usersGuild#roles 等属性现在是管理器(managers),而不是缓存的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类(例如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重大变化的更多信息,请查看更新指南更改日志文档也是查找特定方法/属性的好资源。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接