NodeJS discord.js async函数

fhity93d  于 2023-04-11  发布在  Node.js
关注(0)|答案(1)|浏览(143)

我创建的discord.js依赖函数出现错误:

async function timeoutUser(guildMember, exp, reason) {
  try {
    const now = Math.floor(Date.now() / 1000);
    const timeoutDuration = (exp - now) * 1000;

    console.log(`Timing out ${guildMember} in ${timeoutDuration} milliseconds for reason: ${reason}`);
    
    await guildMember.timeout({timeoutDuration, reason});
    console.log(`Successfully timed out ${guildMember} for ${timeoutDuration / 1000} seconds. Reason: ${reason}`);
  } catch (error) {
    console.error(`Failed to timeout user with Discord ID ${guildMember}: ${error}`);
  }
}

我得到的错误如下:

Error: Failed to remove timeout for user with Discord ID 785346463915311176: TypeError: guildMember.timeout is not a function

完整的错误显示在上面。据我所知,我使用的是discord.js文档中的记录。

iklwldmw

iklwldmw1#

已进行更新,guildMember不是GuildMember的示例:

async function timeoutUser(discordId, exp, reason) {
  try {
    const now = Math.floor(Date.now() / 1000);
    const timeoutDuration = (exp - now) * 1000;

    const guild = client.guilds.cache.get(TxAdminConfig.targetServerId);
    if (!guild) {
      console.error('Target server not found');
      return;
    }
  
    const member = await guild.members.fetch(discordId).catch(() => null);
    if (!member) {
      console.log(`User with Discord ID ${discordId} not found in the guild`);
      return;
    }

    console.log(`Timing out Discord ID ${discordId} in ${timeoutDuration} milliseconds for reason: ${reason}`);
    
    await member.timeout(timeoutDuration, `txAdmin Ban: ${reason}`);
    console.log(`Successfully timed out Discord ID ${discordId} for ${timeoutDuration / 1000} seconds. Reason: ${reason}`);
  } catch (error) {
    console.error(`Failed to timeout user with Discord ID ${discordId}: ${error}`);
  }
}

相关问题