pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

How can I migrate my code to Discord.js v12 from v11?(如何將我的代碼從 v11 遷移到 Discord.js v12?)
本文介紹了如何將我的代碼從 v11 遷移到 Discord.js v12?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我升級到 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#usersGuild#roles 等屬性現在是 ma??nagers,而不是緩存的 Collection的項目.要訪問此集合,請使用 cache 屬性:

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#addRoleGuild#createChannelTextBasedChannel#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.cacheguild.roles.cacheguild.channels.cache)現在只接受 functions,而不是屬性鍵和值,用于 .find.findKey:

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 也已被刪除:

// 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 方法也已被刪除.該方法只是簡單地添加了一個帶有零寬度空格 (u200B) 的字段作為名稱和值,因此要添加一個空白字段,請執行以下操作:

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*** 方法已統一在一個 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#displayAvatarURLGuild#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 重大更改的更多信息,請查看 the更新指南和changelog.文檔也是尋找特定方法的好資源/屬性.

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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Using discord.js to detect image and respond(使用 discord.js 檢測圖像并響應)
Check if user ID exists in Discord server(檢查 Discord 服務器中是否存在用戶 ID)
Guild Member Add does not work (discordjs)(公會成員添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 創建我的第一個機器人,但總是錯誤 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何為我的 Discord.js 機器人編寫事件/命令處理程序?)
How to find a User ID from a Username in Discord.js?(如何從 Discord.js 中的用戶名中查找用戶 ID?)
主站蜘蛛池模板: 煤矿人员精确定位系统_矿用无线通信系统_煤矿广播系统 | 岛津二手液相色谱仪,岛津10A液相,安捷伦二手液相,安捷伦1100液相-杭州森尼欧科学仪器有限公司 | 渗透仪-直剪仪-三轴仪|苏州昱创百科 | 蔬菜配送公司|蔬菜配送中心|食材配送|饭堂配送|食堂配送-首宏公司 | 合肥网络推广_合肥SEO网站优化-安徽沃龙First | 艺术涂料|木纹漆施工|稻草漆厂家|马来漆|石桦奴|水泥漆|选加河南天工涂料 | 高低温老化试验机-步入式/低温恒温恒湿试验机-百科 | 视觉检测设备_自动化检测设备_CCD视觉检测机_外观缺陷检测-瑞智光电 | 包塑软管|金属软管|包塑金属软管-闵彬管业 | 餐饮加盟网_特色餐饮加盟店_餐饮连锁店加盟 | 304不锈钢无缝管_不锈钢管厂家 - 隆达钢业集团有限公司 | 【黄页88网】-B2B电子商务平台,b2b平台免费发布信息网 | 自动检重秤-动态称重机-重量分选秤-苏州金钻称重设备系统开发有限公司 | 环氧乙烷灭菌器_压力蒸汽灭菌器_低温等离子过氧化氢灭菌器 _低温蒸汽甲醛灭菌器_清洗工作站_医用干燥柜_灭菌耗材-环氧乙烷灭菌器_脉动真空压力蒸汽灭菌器_低温等离子灭菌设备_河南省三强医疗器械有限责任公司 | 品牌设计_VI设计_电影海报设计_包装设计_LOGO设计-Bacross新越品牌顾问 | 质检报告_CE认证_FCC认证_SRRC认证_PSE认证_第三方检测机构-深圳市环测威检测技术有限公司 | pH污水传感器电极,溶解氧电极传感器-上海科蓝仪表科技有限公司 | 环氧乙烷灭菌器_压力蒸汽灭菌器_低温等离子过氧化氢灭菌器 _低温蒸汽甲醛灭菌器_清洗工作站_医用干燥柜_灭菌耗材-环氧乙烷灭菌器_脉动真空压力蒸汽灭菌器_低温等离子灭菌设备_河南省三强医疗器械有限责任公司 | 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | 金属切削液-脱水防锈油-电火花机油-抗磨液压油-深圳市雨辰宏业科技发展有限公司 | 东莞ERP软件_广州云ERP_中山ERP_台湾工厂erp系统-广东顺景软件科技有限公司 | 上海诺狮景观规划设计有限公司| 东莞精密模具加工,精密连接器模具零件,自動機零件,冶工具加工-益久精密 | 爱德华真空泵油/罗茨泵维修,爱发科-比其尔产品供应东莞/杭州/上海等全国各地 | 泰国专线_泰国物流专线_广州到泰国物流公司-泰廊曼国际 | 电子海图系统-电梯检验系统-智慧供热系统开发-商品房预售资金监管系统 | 合肥抖音SEO网站优化-网站建设-网络推广营销公司-百度爱采购-安徽企匠科技 | 政府回应:200块在义乌小巷能买到爱情吗?——揭秘打工族省钱约会的生存智慧 | RTO换向阀_VOC高温阀门_加热炉切断阀_双偏心软密封蝶阀_煤气蝶阀_提升阀-湖北霍科德阀门有限公司 | 沈阳缠绕包装机厂家直销-沈阳海鹞托盘缠绕包装机价格 | 气力输送_输送机械_自动化配料系统_负压吸送_制造主力军江苏高达智能装备有限公司! | 日本SMC气缸接头-速度控制阀-日本三菱伺服电机-苏州禾力自动化科技有限公司 | 洛阳装修公司-洛阳整装一站式品牌-福尚云宅装饰 | 北京企业宣传片拍摄_公司宣传片制作-广告短视频制作_北京宣传片拍摄公司 | 手术室净化厂家_成都实验室装修公司_无尘车间施工单位_洁净室工程建设团队-四川华锐16年行业经验 | 【法利莱住人集装箱厂家】—活动集装箱房,集装箱租赁_大品牌,更放心 | 无菌实验室规划装修设计-一体化实验室承包-北京洁净净化工程建设施工-北京航天科恩实验室装备工程技术有限公司 | 南京试剂|化学试剂|分析试剂|实验试剂|cas号查询-专业60年试剂销售企业 | 无水硫酸铝,硫酸铝厂家-淄博双赢新材料科技有限公司 | 北京自然绿环境科技发展有限公司专业生产【洗车机_加油站洗车机-全自动洗车机】 | 假肢-假肢价格-假肢厂家-河南假肢-郑州市力康假肢矫形器有限公司 |