javascript - 如何在 Discord.Js 中创建用户特定频道?
问题描述
如何在 Discord.Js 中创建用户特定频道?
所以我正在制作一个 Discord 机器人,你点击一个反应,它就会把你带到一个私人频道,其他人都看不到这个频道
这是我到目前为止所拥有的:
const Discord = require('discord.js');
const client = new Discord.Client();
const { bot } = require('./config.json');
const request = require('request');
client.on('message', message => {
var command = message.content.replace('t#', '');
var command = command.replace('t# ', '')
if(command.startsWith('post') === true){
message.delete();
var postEmbed = new Discord.RichEmbed();
postEmbed.setTitle('Twotter Post')
postEmbed.setAuthor(message.author.tag, message.author.avatarURL)
postEmbed.setDescription(command.replace('post ', ''))
postEmbed.setFooter('Created by Happy Fone on YouTube')
this.message = message;
message.channel.send(postEmbed).then(message => {
message.react('')
message.react('')
message.react('')
this.messageId = message.id;
});
}
});
client.on('messageReactionAdd', (messageReaction, user) => {
if(user.bot)return;
const { message, emoji } = messageReaction;
if(emoji.name == "") {
if(message.id == this.messageId) {
makeChannel(this.message)
}
}
});
function makeChannel(message){
var server = message.guild;
var name = message.author.username;
server.createChannel(name, "text");
}
client.login(bot.token)
我试图尽可能具体地表达我想要的东西。如果您需要更多信息,请说。
解决方案
Since you need the user that reacted to the message in makeChannel()
, you'll have to add a parameter for it. You don't actually need the relevant message in your function, so you can substitute that parameter for a Guild (which you do need).
function makeChannel(guild, user) {
const name = user.username;
...
}
// within messageReactionAdd listener
makeChannel(message.guild, user);
When creating the channel, you can pass in a ChannelData object containing permissions (PermissionResolvables) for it. By doing so, you can deny everyone (except for members with the Administrator permission) access to the channel except the user.
// within makeChannel()
guild.createChannel(name, {
type: 'text',
permissionOverwrites: [
{
id: guild.id, // shortcut for @everyone role ID
deny: 'VIEW_CHANNEL'
},
{
id: user.id,
allow: 'VIEW_CHANNEL'
}
]
})
.then(channel => console.log(`${name} now has their own channel (ID: ${channel.id})`))
.catch(console.error);
Refer to the examples in the Discord.js documentation for Guild#createChannel()
.
推荐阅读
- audio - window.webkitAudioContext 在 Safari 上不起作用
- php - 获取随机日、月、年、星期几
- java - 处理这些 if 子句的更简洁的方法?
- php - 网站所有控制者的呼叫服务
- asp.net - 为什么在页面级别撤销授权后,CheckUrlAccessForPrincipal 仍然返回 true?
- rabbitmq - RabbitMQ 交换队列最大长度
- python - 一步过滤 pandas GroupBy 输出(方法链接)
- microservices - 微服务中的共享库
- c# - c#如何建立UDP套接字连接
- php - 大于或小于 array_walk 递归