我最近决定将Discord js库升级到v12。 我想当有人打开游戏(与您的朋友一起高尔夫)时,让他扮演一个角色。但是我无法在不协调的js v12中对其进行编程。你能帮助我吗? 这是我在v11中的代码:
Map
答案 0 :(得分:1)
这是discordjs.guide的有关从v11升级到v12的指南:
https://discordjs.guide/additional-info/changes-in-v12.html#managers-cache
您还可以在此处查看官方文档:
https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-presenceUpdate
首先,他们将参数更新为oldPresence
和newPresence
,
所以:
client.on("presenceUpdate", (oldPresence, newPresence) => {
//....
}
第二,他们将集合从guild.roles
更改为guild.roles.cache
:
const playRole = guild.roles.cache.get("633589794357641235");
第三条if语句使用newMember.user.presence
(现在仅为newPresence
),游戏已被修改为Activity
类,并且没有更多的<Presence>.game
,因此您将必须遍历<Presence>.activites
数组:
if (newPresence.find(e => e.name === "Golf With Your Friends")) {
//...
}
最后,他们将<GuildMember>.addRole
移至<GuildMember>.roles.add
:
newPresence.member.roles.add(playRole);
完整代码:
bot.on("presenceUpdate", (oldPresence, newPresence) => {
let guild = oldPresence.guild;
//guild.roles => guild.roles.cache
let playRole = guild.roles.cache.get("633589794357641235");
if (!playRole) return;
if (newPresence.find(e => e.name === "Golf With Your Friends")) {
newPresence.member.roles.add(playRole);
}
});
答案 1 :(得分:0)
const Discord = require('discord.js')
const client = new Discord.Client()
client.on('ready', () => {
console.log('bot sucessfully logged')
})
client.on("presenceUpdate", (oldPresence, newPresence) => {
let guild = oldPresence.guild;
//guild.roles => guild.roles.cache
let playRole = guild.roles.cache.get("837208147302154252");
if (!playRole) return;
if (newPresence.find(e => e.name === "Testing")) {
newPresence.member.roles.add(playRole);
}
});
client.login("token")
错误是 if (newPresence.find(e => e.name === "Testing")) { ^
TypeError: newPresence.find 不是函数