使用开机自检消息更改用户的语音通道



我正在为一个特定的应用程序制作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);
});

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

您需要使用bot变量来找到您正在工作的特定公会,然后找到您想要更改其语音通道的特定成员,然后执行此操作

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

最新更新