英文:
guildMemberAdd event doesn't work in discord.js bot
问题
I want to know what is the solution to this problem and what I'm supposed to do to fix this error.
我想知道这个问题的解决方案以及如何修复这个错误。
I tried to fix for about 3 hours, but I got no results.
我尝试修复了大约3个小时,但没有结果。
I would like this event to trigger when someone joins the server to list nickname, tag and account creation time using EmbedBuilder
.
我希望当有人加入服务器时,可以使用 EmbedBuilder
列出昵称、标签和帐户创建时间。
guildMemberAdd Event:
const { GuildMember, EmbedBuilder } = require("discord.js");
const moment = require("moment");
module.exports = {
name: "guildMemberAdd",
/**
*
* @param {GuildMember} member
*/
async execute(member, client) {
const guildConfig = client.guildConfig.get(member.guild.id);
if (!guildConfig) return;
const guildRoles = member.guild.roles.cache;
let assigneRole = member.user;
guildRoles.get(guildConfig.memberRole);
if (!assigneRole) assigneRole = " Not configured.";
else
await member.roles
.add(assigneRole)
.catch(() => {
assigneRole = "Failed due to role hierarchy";
});
const welcomChannel = (await member.guild.channels.fetch()).get(
guildConfig.welcomChannel
);
if (!welcomChannel) return;
let color = "#74e21e";
const accountCreation = parseInt(member.user.createdTimestamp / 1000);
const joiningTime = parseInt(member.joinedAt / 1000);
const MonthAgo = moment().subtract(2, "month").unix();
const weekAgo = moment().subtract(2, "weeks").unix();
const dayAgo = moment().subtract(2, "days").unix();
if (accountCreation >= MonthAgo) {
color = "#e2bb1e";
}
if (accountCreation >= weekAgo) {
color = "#e24d1c";
}
if (accountCreation >= dayAgo) {
color = "#e21e1e";
}
const Embed = new EmbedBuilder()
.setAuthor({
name: `${member.user.tag}| ${member.id}`,
iconURL: member.displayAvatarURL({ dynamic: true }),
})
.setColor(color)
.setThumbnail(member.user.displayAvatarURL({ dynamic: true, size: 256 }))
.setDescription([
`- User: ${member.user}`,
`- Role Assigend: ${assigneRole}`,
`- Account Created: <t:${accountCreation}:D> | <t:${accountCreation}:R>`,
`- Account Joined: <t:${joiningTime}:D> | <t:${joiningTime}:R>`,
].join("\n"))
.setFields({ text: "Joined" })
.setTimestamp();
welcomChannel.send({ Embed: [Embed] });
},
};
Error:
node:events:491
throw er; // Unhandled 'error' event
^
CombinedPropertyError: Received one or more errors
at ArrayValidator.handle (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:457:70)
at ArrayValidator.parse (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:201:88)
at EmbedBuilder.spliceFields (D:\\Vinnint\\node_modules\\@discordjs\\builders\\dist\\index.js:207:31)
at EmbedBuilder.setFields (D:\\Vinnint\\node_modules\\@discordjs\\builders\\dist\\index.js:215:10)
at Object.execute (D:\\Vinnint\\Events\\guildMemberAdd.js:54:10)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:394:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21) {
errors: [
[
0,
CombinedPropertyError: Received one or more errors
at ObjectValidator.handleIgnoreStrategy (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:1252:70)
at ObjectValidator.handleStrategy (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:1105:47)
at ObjectValidator.handle (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:1205:17)
at ObjectValidator.run (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:187:23)
at ArrayValidator.handle (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:451:37)
at ArrayValidator.parse (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:201:88)
at EmbedBuilder.spliceFields (D:\\Vinnint\\node_modules\\@discordjs\\builders\\dist\\index.js:207:31)
at EmbedBuilder.setFields (D:\\Vinnint\\node_modules\\@discordjs\\builders\\dist\\index.js:215:10)
at Object.execute (D:\\Vinnint\\Events\\guildMemberAdd.js:54:10)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
errors: [
[ 'name', [MissingPropertyError] ],
[ 'value', [MissingPropertyError] ]
]
}
]
]
}
Node.js v18.12.1
英文:
I want to know what is the solution to this problem and what I'm supposed to do to fix this error.
I tried to fix for about 3 hours, but I got no results.
I would like this event to trigger when someone joins the server to list nickname, tag and account creation time using EmbedBuilder
.
guildMemberAdd Event:
const {GuildMember,EmbedBuilder} = require("discord.js");
const moment = require("moment");
module.exports = {
name: "guildMemberAdd",
/\*\*
\*
\* @param {GuildMember} member
\*/
async execute(member, client){
const guildConfig = client.guildConfig.get(member.guild.id);
if(!guildConfig) return;
const guildRoles = member.guild.roles.cache;
let assigneRole = member.user
guildRoles.get(guildConfig.memberRole);
if(!assigneRole) assigneRole = " Not configured.";
else await member.roles.add(assigneRole)
.catch(()=> { assigneRole = "Failed due to role hierarchy"});
const welcomChannel = (await member.guild.channels.fetch()).get(guildConfig.welcomChannel);
if(!welcomChannel) return
let color = "#74e21e"
const accountCreation = parseInt(member.user.createdTimestamp / 1000);
const joiningTime = parseInt(member.joinedAt / 1000);
const MonthAgo = moment().subtract(2,"month").unix();
const weekAgo = moment().subtract(2,"weeks").unix();
const dayAgo = moment().subtract(2,"days").unix();
if(accountCreation >= MonthAgo){
color = "#e2bb1e"
}
if(accountCreation >= weekAgo){
color = "#e24d1c"
}
if(accountCreation >= dayAgo){
color = "#e21e1e"
}
const Embed = new EmbedBuilder()
.setAuthor({name: `${member.user.tag}| ${member.id}`, iconURL: member.displayAvatarURL({dynamic: true})})
.setColor(color)
.setThumbnail(member.user.displayAvatarURL({dynamic: true,size: 256}))
.setDescription([
`- User: ${member.user}`,
`- Role Assigend: ${assigneRole}`,
`- Account Created: <t:${accountCreation}:D> | <t:${accountCreation}:R>`,
`- Account Joined: <t:${joiningTime}:D> | <t:${joiningTime}:R>`
].join("\n"))
.setFields({text: "Joined"})
.setTimestamp();
welcomChannel.send({Embed: [Embed]});
}
}
Error:
node:events:491
throw er; // Unhandled 'error' event
^
CombinedPropertyError: Received one or more errors
at ArrayValidator.handle (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:457:70)
at ArrayValidator.parse (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:201:88)
at EmbedBuilder.spliceFields (D:\\Vinnint\\node_modules\\@discordjs\\builders\\dist\\index.js:207:31)
at EmbedBuilder.setFields (D:\\Vinnint\\node_modules\\@discordjs\\builders\\dist\\index.js:215:10)
at Object.execute (D:\\Vinnint\\Events\\guildMemberAdd.js:54:10)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:394:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21) {
errors: \[
\[
0,
CombinedPropertyError: Received one or more errors
at ObjectValidator.handleIgnoreStrategy (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:1252:70)
at ObjectValidator.handleStrategy (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:1105:47)
at ObjectValidator.handle (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:1205:17)
at ObjectValidator.run (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:187:23)
at ArrayValidator.handle (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:451:37)
at ArrayValidator.parse (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:201:88)
at EmbedBuilder.spliceFields (D:\\Vinnint\\node_modules\\@discordjs\\builders\\dist\\index.js:207:31)
at EmbedBuilder.setFields (D:\\Vinnint\\node_modules\\@discordjs\\builders\\dist\\index.js:215:10)
at Object.execute (D:\\Vinnint\\Events\\guildMemberAdd.js:54:10)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
errors: \[
\[ 'name', \[MissingPropertyError\] \],
\[ 'value', \[MissingPropertyError\] \]
\]
}
\]
\]
}
Node.js v18.12.1
答案1
得分: 1
我看到你的EmbedBuilder代码中有一些小错误。
setFields
不存在。使用addFields
来向嵌入中添加字段。addFields
需要同时提供name
和value
。- 注意:
addFields
不是必需的。
- 注意:
- 通道
send
方法需要一个embeds
数组。
const Embed = new EmbedBuilder()
.setAuthor({
name: `${member.user.tag} | ${member.id}`,
iconURL: member.displayAvatarURL({ dynamic: true }),
})
.setColor(color)
.setThumbnail(member.user.displayAvatarURL({ dynamic: true, size: 256 }))
.setDescription(
[
`- 用户:${member.user}`,
`- 分配的角色:${assigneRole}`,
`- 帐户创建时间:<t:${accountCreation}:D> | <t:${accountCreation}:R>`,
`- 加入时间:<t:${joiningTime}:D> | <t:${joiningTime}:R>`,
].join("\n")
)
.addField("事件", "加入")
.setTimestamp();
await welcomChannel.send({ embeds: [Embed] });
英文:
I see a couple of small errors in your EmbedBuilder code.
setFields
does not exist. UseaddFields
to add fields to an EmbedaddFields
requires both aname
and avalue
- Note:
addFields
is not required
- Note:
- Channel
send
method expects an array ofembeds
const Embed = new EmbedBuilder()
.setAuthor({
name: `${member.user.tag} | ${member.id}`,
iconURL: member.displayAvatarURL({ dynamic: true }),
})
.setColor(color)
.setThumbnail(member.user.displayAvatarURL({ dynamic: true, size: 256 }))
.setDescription(
[
`- User: ${member.user}`,
`- Role Assigend: ${assigneRole}`,
`- Account Created: <t:${accountCreation}:D> | <t:${accountCreation}:R>`,
`- Account Joined: <t:${joiningTime}:D> | <t:${joiningTime}:R>`,
].join("\n")
)
.addFields({ name: "Event", value: "Joined" })
.setTimestamp();
await welcomChannel.send({ embeds: [Embed] });
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论