Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么可以';我不能让我的discord机器人向角色添加用户吗?_Javascript_Node.js_Discord_Discord.js_Bots - Fatal编程技术网

Javascript 为什么可以';我不能让我的discord机器人向角色添加用户吗?

Javascript 为什么可以';我不能让我的discord机器人向角色添加用户吗?,javascript,node.js,discord,discord.js,bots,Javascript,Node.js,Discord,Discord.js,Bots,我一直在到处寻找答案,但似乎什么都不管用。也许这里有人能帮我 “!invision1@user spam”应导致bot发送消息“用户因垃圾邮件而被监禁”,并向提到的用户添加一个名为“囚徒”的角色 const Discord=require('Discord.js'); const config=require('./config.json'); const client=new Discord.client(); client.once('ready',()=>{ console.log('Re

我一直在到处寻找答案,但似乎什么都不管用。也许这里有人能帮我

“!invision1@user spam”应导致bot发送消息“用户因垃圾邮件而被监禁”,并向提到的用户添加一个名为“囚徒”的角色

const Discord=require('Discord.js');
const config=require('./config.json');
const client=new Discord.client();
client.once('ready',()=>{
console.log('Ready!');
});
函数getUserFromTitle(提及){
如果(!提及)返回;
如果(用(“”)表示开始){
提及=提及.切片(2,-1);
如果(提及.startsWith(“!”){
提及=提及。切片(1);
}
返回client.users.cache.get(提及);
}
}
函数getUserFromInetitionRegex(提及){
const matches=提及.match(/^$/);
常量id=匹配项[1];
返回client.users.cache.get(id);
}
client.on('消息',(消息)=>{
如果(!message.content.startsWith(config.prefix))返回;
const withoutPrefix=message.content.slice(config.prefix.length);
常量拆分=不带outprefix.split(+/);
const命令=拆分[0];
常量args=split.slice(1);
如果(命令==='invision1'){
如果(参数[0]){
让user=getuserfromtoite(args[0]);
如果(!用户){
返回消息。回复(“无效用户”);
}
返回message.channel.send(
`${user.username}已被监禁${args[1]}`
);
user.roles.add('749291623170572428');
}
returnmessage.channel.send(`Add@notice以监禁用户。`);
}
});

因为您的函数,
getuserfromtoite
返回一个
User
对象,而不是
GuildMember
对象

更改:

返回client.users.cache.get(提及);
与:

//记住将消息作为第二个参数传递,因为消息当前未定义。
返回消息.guild.members.cache.get(提及);

const Discord=require(“Discord.js”);
const client=new Discord.client();
const config=require(“./config.json”);
client.on(“ready”,()=>{
log(`${client.user.tag}已准备就绪。`);
});
client.on(“消息”,异步消息=>{
if(message.author.bot)返回false;
如果(!message.guild)返回false;
if(message.content.indexOf(config.prefix)!=0)返回false;
常量参数=message.content.slice(config.prefix.length).split(+/+/g);
const commandName=arguments.shift().toLowerCase();
如果(commandName==“监禁1”){
if(!arguments[0])返回message.channel.send(“添加@title以监禁用户”);
if(!arguments[1])返回message.channel.send(“请提供原因”);
const user=message.guild.members.cache.get(getUserFromNite(参数[0]))| | message.guild.members.cache.get(参数[0]);
const role=message.guild.roles.cache.get(“749291623170572428”);
if(!user)返回message.channel.send(“无效用户”);
if(!role)返回message.channel.send(“无效角色”);
user.roles.add(角色)。然后(角色=>{
message.channel.send(`${user.user.tag}因${arguments.slice(22000.join(“”)})而被囚禁;
}).catch(error=>message.channel.send(`无法添加角色。${error}`));
};
});
函数getUserFromTitle(提及){
如果(!提及)返回false;
const Matches=提及.match(/^$/);
返回匹配项?匹配项[1]:false;
};
client.login(process.env.DISCORD\u访问\u令牌)

Try
user.roles.add(“”)取而代之?@Acc实验室我试过你的方法,但仍然无效如果这是你的意思,它也不起作用。消息仍然未定义,我得到“TypeError:无法读取未定义的属性'guild'”
函数GetUserFromIty(提及,消息){if(!提及)返回;if(提及.startsWith(''){提及=提及.slice(2,-1);if(提及.startsWith('!'){提及=提及.slice(1);}return message.guild.members.cache.get(notify);}}
如下调用函数:
getuserfromtify(args[0],message)。返回“未定义已因未定义而被监禁”@AK我为您重写了整个代码。告诉我你是否有同样的问题。