尝试设置Discord机器人的状态

huangapple go评论185阅读模式
英文:

Trying to set Discord bot presence

问题

抱歉,由于您要求只返回翻译好的部分,我将只翻译代码部分,不包括问题描述。以下是您提供的代码的翻译:

botpresence.cjs

const {
  Discord,
  Client,
  Intents,
  GatewayIntentBits,
  ActivityType,
} = require("discord.js");
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildPresences,
    GatewayIntentBits.DirectMessages,
  ],
});

module.exports.namedCjsExport = function namedCjsExport() {
  client.once("ready", () => {
    client.user.setPresence("Watching Dev", { type: "WATCHING" });
    setInterval(() => {
      client.user.setPresence("Watching Dev", { type: "WATCHING" });
    }, 60000);
  });
};

console.log("Bot is online!");

client.login(
  "TOKEN"
);

app.js

import defaultCjsExport, { namedCjsExport } from './botpresence.cjs';
namedCjsExport()
import "dotenv/config";
import express from "express";
import { InteractionType, InteractionResponseType } from "discord-interactions";
import {
  VerifyDiscordRequest,
  getRandomEmoji,
  DiscordRequest,
} from "./utils.js";

const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json({ verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));

app.post("/interactions", async function (req, res) {
  const { type, id, data } = req.body;

  if (type === InteractionType.PING) {
    return res.send({ type: InteractionResponseType.PONG });
  }

  if (type === InteractionType.APPLICATION_COMMAND) {
    const { name } = data;

    if (name === "test") {
      return res.send({
        type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
        data: {
          content: "Finally online and responding     " + getRandomEmoji(),
        },
      });
    }

    if (name === "test2") {
      return res.send({
        type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
        data: {
          content: "Going in for a second command",
        },
      });
    }
  }
});

app.listen(PORT, () => {
  console.log("Listening on port", PORT);
});

请注意,您的代码中可能有一些HTML实体编码(如"),这些需要根据需要进行处理。希望这些翻译有助于您理解代码。

英文:

ello, I am new to starting a discord bot project with the slash commands. I am trying to use some code from an old bot of mine to try and set the presence on my new bot. When launching, I get the console message and my bot goes online… status does not set.

The documentation I have been following:
Discord Documentation

Here is my code below:

botpresence.cjs

const {
  Discord,
  Client,
  Intents,
  GatewayIntentBits,
  ActivityType,
} = require("discord.js");
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildPresences,
    GatewayIntentBits.DirectMessages,
  ],
});

module.exports.namedCjsExport = function namedCjsExport() {
  client.once("ready", () => {
    client.user.setPresence("Watching Dev", { type: "WATCHING" });
    setInterval(() => {
      client.user.setPresence("Watching Dev", { type: "WATCHING" });
    }, 60000);
  });
};

console.log("Bot is online!");

client.login(
  "TOKEN"
);

app.js

import defaultCjsExport, { namedCjsExport } from './botpresence.cjs';
namedCjsExport()
import "dotenv/config";
import express from "express";
import { InteractionType, InteractionResponseType } from "discord-interactions";
import {
  VerifyDiscordRequest,
  getRandomEmoji,
  DiscordRequest,
} from "./utils.js";

const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json({ verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));


app.post("/interactions", async function (req, res) {
  const { type, id, data } = req.body;

  if (type === InteractionType.PING) {
    return res.send({ type: InteractionResponseType.PONG });
  }

  if (type === InteractionType.APPLICATION_COMMAND) {
    const { name } = data;

    if (name === "test") {
      return res.send({
        type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
        data: {
          content: "Finally online and responding     " + getRandomEmoji(),
        },
      });
    }

    if (name === "test2") {
      return res.send({
        type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
        data: {
          content: "Going in for a second command",
        },
      });
    }
  }
});

app.listen(PORT, () => {
  console.log("Listening on port", PORT);
});

It reads the bot is online in the console but does not show the status?

答案1

得分: 0

To setPresence()(或者)setActivity(),您需要提供一个PresenceData

例如,如果您想设置机器人的在线状态,您应该这样做:

client.user.setPresence({ activities: [{ name: 'Watching Dev', type: 3 }], status: 'online' });

有关ActivityType的更多信息,您可以从这里找到。

但是,您可能会发现setActivity()更适合您,也更容易使用:

client.user.setActivity('Watching Dev', { type: ActivityType.Watching });
英文:

To setPresence() (or) setActivity() you need to provide a PresenceData for it.

For example if you want to se the presence for your bot you should do:

client.user.setPresence({ activities: [{ name: 'Watching Dev', type: 3 }], status: 'online');

More about the ActivityType you can find from here.

But you could find setActivity() better suited, and easier for yourself:

client.user.setActivity('Watching Dev', { type: ActivityType.Watching });

huangapple
  • 本文由 发表于 2023年8月5日 06:27:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定