【问题标题】:Change user's voice channel with POST message使用 POST 消息更改用户的语音通道
【发布时间】:2021-03-20 08:00:53
【问题描述】:

我正在为特定应用程序制作 Discord 机器人,该应用程序需要在机器人收到 POST 调用时更改用户的语音通道,但我正在努力正确调用 Discord 部分。 现在我可以通过使用此代码的命令来做到这一点

bot.on("message", async (message) => {
  let messageArray = message.content.split(" ");
  let command = messageArray[0];
  if (message.author.bot) return;
  if (message.channel.type === "dm") return;
  if (!command.startsWith(prefix)) return;

  if (command === `${prefix}tochannel`) {
    let channelID = messageArray[1];
    if (!channelID) return message.channel.send("Please specify the channel ID!");
    message.member.voice.setChannel(channelID);
  }
});

我在 Express 中还有一些代码可以调用带有两个文本字段的表单,仅用于测试

const app = express();
app.use(bodyParser.urlencoded({
  extended: true
}));

  app.get("/", function (req, res) {
    res.sendFile(__dirname + "/index.html");
  });

app.post("/", function (req, res) {
  var toChannel = Number(req.body.channelID);
  var userID = Number(req.body.userID);
  console.log(channelID);
  console.log(userID);
  res.send("Channel ID: " + channelID + "  >>  " + "User ID: " + userID);
});

我最大的噩梦是:如何让这两个宇宙相互交谈?

【问题讨论】:

    标签: node.js express discord discord.js


    【解决方案1】:

    您需要使用您的bot 变量找到您所在的特定公会,然后找到您要更改其语音频道的特定成员,然后执行此操作。这是您的快速 POST 处理程序中的一个示例:

    app.post("/", async function (req, res) {
      const toChannel = req.body.channelID
      const userID = req.body.userID
      console.log(channelID);
      console.log(userID);
    
      //Use your client object, in your case saved to the variable "bot"
      try {
            let guild = bot.channels.cache.get(toChannel).guild;
            let member = await guild.members.fetch(userID);
            member.voice.setChannel(toChannel);
    
            res.send(`Channel ID: ${channelID} >> User ID:  ${userID}`);
      } catch(e) {
            res.send(`An error occurred: ${err.stack}`);
      }
    });
    

    请注意,这是一个未经测试的示例,我不知道它是否完全按原样工作。您可能需要对其进行一些调整以使其正常工作,并且在继续尝试更改成员的频道之前,您应该检查以确保找到公会和成员。但我希望它能让您大致了解您将如何进行此操作,您可以使用您的 bot 变量来执行此操作。

    相关资源:
    https://discord.js.org/#/docs/main/stable/class/Client

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-20
      • 2020-10-19
      • 1970-01-01
      • 2019-01-12
      • 2020-12-24
      • 2019-10-12
      • 2021-08-27
      • 1970-01-01
      相关资源
      最近更新 更多