首页 > 解决方案 > 我如何制作一个 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"  }})}});

标签: node.jsdiscord.js

解决方案


查看 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


推荐阅读