guildMemberAdd事件在discord.js机器人中不起作用。

huangapple go评论81阅读模式
英文:

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(&quot;discord.js&quot;);

const moment = require(&quot;moment&quot;);

module.exports = {
name: &quot;guildMemberAdd&quot;,
/\*\*
\*
\* @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 = &quot; Not configured.&quot;;
        else await member.roles.add(assigneRole)
        .catch(()=&gt; { assigneRole = &quot;Failed due to role hierarchy&quot;});
    
        const welcomChannel = (await member.guild.channels.fetch()).get(guildConfig.welcomChannel);
        if(!welcomChannel) return
        let color = &quot;#74e21e&quot;
        const accountCreation = parseInt(member.user.createdTimestamp / 1000);
        const joiningTime = parseInt(member.joinedAt / 1000);
    
        const MonthAgo = moment().subtract(2,&quot;month&quot;).unix();
        const weekAgo = moment().subtract(2,&quot;weeks&quot;).unix();
        const dayAgo = moment().subtract(2,&quot;days&quot;).unix();
    
        if(accountCreation &gt;= MonthAgo){
            color = &quot;#e2bb1e&quot;
        }
        if(accountCreation &gt;= weekAgo){
            color = &quot;#e24d1c&quot;
        }
        if(accountCreation &gt;= dayAgo){
            color = &quot;#e21e1e&quot;
        }
    
        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: &lt;t:${accountCreation}:D&gt; | &lt;t:${accountCreation}:R&gt;`,
            `- Account Joined: &lt;t:${joiningTime}:D&gt; | &lt;t:${joiningTime}:R&gt;`
        ].join(&quot;\n&quot;))
        .setFields({text: &quot;Joined&quot;})
        .setTimestamp();
        welcomChannel.send({Embed: [Embed]});
    }

}

Error:

node:events:491
throw er; // Unhandled &#39;error&#39; 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 &#39;error&#39; 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: \[
\[ &#39;name&#39;, \[MissingPropertyError\] \],
\[ &#39;value&#39;, \[MissingPropertyError\] \]
\]
}
\]
\]
}

Node.js v18.12.1

答案1

得分: 1

我看到你的EmbedBuilder代码中有一些小错误。

  • setFields 不存在。使用 addFields 来向嵌入中添加字段。
  • addFields 需要同时提供 namevalue
    • 注意: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}`,
      `- 帐户创建时间:&lt;t:${accountCreation}:D&gt; | &lt;t:${accountCreation}:R&gt;`,
      `- 加入时间:&lt;t:${joiningTime}:D&gt; | &lt;t:${joiningTime}:R&gt;`,
    ].join("\n")
  )
  .addField("事件", "加入")
  .setTimestamp();
await welcomChannel.send({ embeds: [Embed] });
英文:

I see a couple of small errors in your EmbedBuilder code.

  • setFields does not exist. Use addFields to add fields to an Embed
  • addFields requires both a name and a value
    • Note: addFields is not required
  • Channel send method expects an array of 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(
    [
      `- User: ${member.user}`,
      `- Role Assigend: ${assigneRole}`,
      `- Account Created: &lt;t:${accountCreation}:D&gt; | &lt;t:${accountCreation}:R&gt;`,
      `- Account Joined: &lt;t:${joiningTime}:D&gt; | &lt;t:${joiningTime}:R&gt;`,
    ].join(&quot;\n&quot;)
  )
  .addFields({ name: &quot;Event&quot;, value: &quot;Joined&quot; })
  .setTimestamp();
await welcomChannel.send({ embeds: [Embed] });

huangapple
  • 本文由 发表于 2023年1月11日 03:29:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75074929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定