admin管理员组文章数量:1291062
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('wele')
// 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')
How can I migrate my code to Discord.js v12 and fix these errors? Where can I see the breaking changes v12 introduced?
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('wele')
// 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')
How can I migrate my code to Discord.js v12 and fix these errors? Where can I see the breaking changes v12 introduced?
Share Improve this question edited Oct 2, 2021 at 13:04 Braiam 4,49611 gold badges49 silver badges83 bronze badges asked Sep 16, 2020 at 0:05 Lauren YimLauren Yim 14.1k2 gold badges37 silver badges66 bronze badges 2- To whoever voted to close this as ‘needs more focus’: this question was the result of this meta discussion about having a canonical question that would answer issues relating to upgrading to Discord.js v12, most notably the introduction of managers. There were a lot of questions relating to the upgrade that have been marked as duplicates of this question. I don’t believe this question is too broad; if this was split up into multiple questions it would have the same information (read the migration guide and the docs). – Lauren Yim Commented Oct 3, 2021 at 6:34
- If you still believe this question is too broad, feel free to open a discussion on meta, but at this stage I don’t think any action is needed on this question especially considering that Discord.js v13 has been released. – Lauren Yim Commented Oct 3, 2021 at 6:37
1 Answer
Reset to default 13Here are some of the most mon breaking changes introduced in Discord.js v12 that people run into.
Managers
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')
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('wele')
const messages = await message.channel.messages.fetch()
Collection
The Collection
class (e.g. client.users.cache
, guild.roles.cache
, guild.channels.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
have also been removed:
// 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
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
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()
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')
Voice
All of the VoiceConnection
/VoiceBroadcast#play***
methods have been unified under a single play
method:
const dispatcher = connection.play('./music.mp3')
Client#createVoiceBroadcast
has been moved to the ClientVoiceManager
:
const broadcast = client.voice.createVoiceBroadcast()
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.
Image URLs
Properties such as User#displayAvatarURL
and Guild#iconURL
are now methods:
const avatar = user.displayAvatarURL()
const icon = mesage.guild.iconURL()
You can also pass an ImageURLOptions
to customise things like format and size.
More information
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.
本文标签: javascriptHow can I migrate my code to Discordjs v12 from v11Stack Overflow
版权声明:本文标题:javascript - How can I migrate my code to Discord.js v12 from v11? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741509206a2382506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论