node.js - 我如何制作一个 setpresence 命令来自己触发它?
问题描述
我正在尝试执行设置状态命令,以便能够在不重新启动我的机器人并更改我的代码的情况下更改状态,但我无法做到,这是我的代码:
const Discord = require("discord.js")
const bot = new Discord.Client()
const fetch = require('node-fetch')
const config = require("./config.json")
bot.login(config.token)
bot.on("ready", () => {console.log("Loaded up!")});
bot.on("message", message => {
if (message.author.bot) return;
if (message.content.indexOf(config.prefix) !== 0) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase()
if (command === "set") {
bot.user.setPresence({
status: "online",
game: {
name: "a",
type: "WATCHING" }})}});
解决方案
查看 discordjs 的文档。我可以看到一个在机器人运行时设置您的 clientUsers 存在的示例。
工作代码:
const Discord = require("discord.js");
const config = require("./config.json")
const bot = new Discord.Client();
bot.on("ready", () => {
console.log("Initialized bot, listening...")
});
bot.on("message", message => {
if (message.author.bot || !message.content.startsWith(config.prefix)) return;
let [command, ...args] = message.content.slice(config.prefix.length).split(/ +/g);
if (command === "set") {
bot.user.setPresence({ activity: { name: 'with discord.js' }, status: 'idle' });
return;
}
if (command === "ping") {
message.channel.send("Pong")
return;
}
})
bot.login(config.token)
文档链接: https ://discord.js.org/#/docs/main/stable/class/ClientUser?scrollTo=setPresence
推荐阅读
- java - How to summarize a object type in swagger?
- php - 无法将管理员类型分配给我的创建管理员命令
- elasticsearch - Painless: Get time difference
- python - DJANGO 将 AJAX 请求中的新结果值渲染到 HTML 页面
- reactjs - React Profiler - 开始分析并重新加载页面?
- matlab - 如果输入数字列表而不是单个数字,则函数返回错误数据
- angular - 在Angular 2中将数据从父视图传递到子视图
- ruby-on-rails - 在 Rails 应用程序中添加具有多个 SMTP 服务器的 ActionMailer
- javascript - 无论数组大小如何,随机获取半个字符串数组
- javascript - 使用 javascript 在小型设备上单击锚链接时使我的下拉菜单崩溃