簡體   English   中英

如何將我的代碼從 v11 遷移到 Discord.js v12?

[英]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#usersGuild#roles等屬性現在是manager ,而不是緩存的項目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.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#displayAvatarURLGuild#iconURL等屬性現在是方法

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

您還可以傳遞ImageURLOptions來自定義格式和大小等內容。

更多信息

要了解有關 v12 重大更改的更多信息,請查看更新指南更改日志 文檔也是查找特定方法/屬性的好資源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM