javascript - 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
- Essentials:
./resources/utils/essentials.js
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;
}
}
- Event Handler for
ready
:./resources/events/ready.js
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(", ")}`
);
}
};
- Main bot file:
./bot.js
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)
}
}
});
- Eval command:
./resources/commands/eval.js
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?
解决方案
推荐阅读
- apache - Httpd 服务无法在 Cpanle/WHM 中启动和取消屏蔽
- java - 使用 OIDC 时未在 websocket OnOpen 调用中设置 Quarkus SecurityIdentity
- reactjs - 使用 useContext 添加和删除数据以及计数 - React - Typescript
- c# - 保存在 wave 中的 NAudio 文件在文件中添加了填充
- python - Condense List[Dict[key, list]] 以便重复键的列表转换为每个唯一键的列表列表
- vim - 在 CentOS 上将 vim 升级到版本 8:文件不包含节标题
- java - 从 CLI 构建的 Maven 失败但从 Eclipse 成功
- python - Python/Tkinter:是否可以通过单击外部来检测 OptionMenu/Menubutton 弹出窗口何时关闭?
- spring - 使用spring依赖管理无法解析依赖
- sockets - 在 FTP 服务器中,为什么服务器应该打开端口而不是客户端?