首页 > 解决方案 > Cannot `.setActivity()` using external functions module

问题描述

I was trying to setup the bot's .setActivity(), but it doesn't seem to work, though, if I do it using the eval command, it actually sets it and works. Is this because I tried calling an external functions module?


Files

placeHolder(client, string) {
   const owner = client.users.cache.get(client.config.admin.owner.id);
   if (typeof string === "string") {
     return string
       .replace(/%totalCommands%/g, client.commands.array().length)
       .replace(/%clientName%/g, client.name)
       .replace(/%clientId%/g, client.user.id)
       .replace(/%clientDescription/g, client.package.description)
       .replace(/%clientUsername%/g, client.user.username)
       .replace(/%clientTag%/g, client.user.tag)
       .replace(/%clientDevName%/g, client.dev.name)
       .replace(/%clientDevHelpers%/g, client.dev.helpers.join(", "))
       .replace(/%clientDefaultPrefix%/g, client.def.prefix)
       .replace(/%clientGuildCount%/g, client.guilds.cache.size)
       .replace(/%clientChannelCount%/g, client.channels.cache.size)
       .replace(/%clientPackageName%/g, client.package.name)
       .replace(/%clientVersion%/g, client.package.version)
       .replace(/%clientLicense%/g, client.package.license)
       .replace(/%clientAvatarURL%/g, client.avatar)
       .replace(/%clientRepository%/g, client.package.repository)
       .replace(/%clientAuthor%/g, client.package.author)
       .replace(/%clientOwnerUsername%/g, owner.username)
       .replace(/%clientOwnerTag%/g, owner.tag)
       .replace(/%clientOwnerID%/g, owner.id)
       .replace(/%clientMainFile%/g, client.package.main)
       .replace(/%clientOwnerAvatarURL%/g, owner.avatarURL() || owner.defaultAvatarURL)
       .replace(/%clientOriginalAuthor%/g, client.package.original_author);
   } else {
     return string;
   }
 }
const Discord = require('discord.js');
const Essentials = require("../utils/essentials.js");

module.exports = {
  name: "ready", // Event Name
  async execute(
    client, event
  ) {
    let activity = {};
    async function setActv() {
      activity.type = client.config.client.presence.activity.default.name;
      activity.status = client.config.client.presence.activity.status;
      activity.name = await Essentials.placeHolder(client, client.config.client.presence.activity.default.name);
      client.user.setActivity(activity.name, {
        type: activity.type
      });
      client.user.setStatus(activity.status);
    }
    client.footer = Essentials.placeHolder(client, client.config.client.settings.footer);
    try {
      // Set Activity every 30 seconds
      setInterval(async () => {
        await setActv().catch(err => Essentials.log(client, error))
      }, 5000);
    } catch (error) {
      Essentials.log(client, error);
    }
  
    // Bot Ready Log //
    console.log(
      `Logged in as ${client.user.tag}.\n`
      + `There are ${client.users.cache.size} users and/or bots online.\n`
      + `${client.user.tag} connected to:\n${client.guilds.cache
        .map(g => g.name)
        .join(", ")}`
    );
  }
};
client.events = new Discord.Collection();
var eventFiles = fs
  .readdirSync(`./resources/events`)
  .filter(file => file.endsWith(".js"));
for (var file of eventFiles) {
  var event = require(`./resources/events/${file}`);
  client.events.set(event.name, event);
  console.log(`Loading event handler for event "${event.name}".`);
}

client.on("ready", async () => {
  const event = client.events.get("ready");
  if (event) {
    try {
      await event.execute(
        client, event
      )
    } catch (err) {
      Essentials.log(client, err)
    }
  } 
});
const Discord = require('discord.js');
const Essentials = require('../utils/essentials.js')

module.exports = {
  id: "eval",
  name: "Eval", // Command name
  description: "A simple eval tool to use on Discord.", // Command Description
  aliases: ["evl", "ev"], // Command Aliases
  category: "Utilities",
  cooldown: 5000, // Command cooldown
  examples: ["eval message.author.id", "eval process.cwd()"], // Command Examples
  usage: ["<args>"], // Command Usage
  permissions: ["SEND_MESSAGES", "READ_MESSAGE_HISTORY"], // Command Permissions
  memberPermissions: ["SEND_MESSAGES", "READ_MESSAGE_HISTORY", "MANAGE_SERVER"], // User is required to have these permissions
  admin: true, // Command is admin only
  async execute(client, command, message, args, auth, channel, guild) { // Function async execute()
    // Command Starts Here
    try {
      let evaled = eval(args.join(" "));
      if (typeof evaled !== "string") evaled = require("util").inspect(evaled);
      const avtr = message.author.avatarURL() || message.author.defaultAvatarURL;
      const embed = new Discord.MessageEmbed()
        .setAuthor(client.name, client.avatar)
        .setTitle(command.name)
        .setTimestamp()
        .setColor(client.color.default)
        .addField("**Input**",  '```' + args.join(" ") + '```')
        .addField("**Result**", '```' + Essentials.clean(evaled) + '```')
        .setFooter(`${message.author.username} evaled`, avtr);
      message.channel.send(embed);
    } catch (err) {
      const embed = Essentials.errorEmbed(err);
      message.channel.send((embed), { split: true });
    }
  }
};

I've tried a lot of combinations, ranging from actually putting it outside of the Event Handler and straight to the bot.js until actually using eval(), shown above is my current code. NO error codes were thrown, and all of the other components in ready.js works, including client.footer = ..., except for setting the activity, did I miss something?

标签: javascriptnode.jsfunctiondiscord.jseval

解决方案


推荐阅读