我发送到传入的Webhook的提及将呈现为纯文本。
注意:使用请求包发送发帖请求。
尝试了以下内容:
发送提及为<@userid>
结果:<@userid>
//作为纯文本
request.post(
`${channels[message.channel.name]}`,
{
json: {
text:
'To: ' + mapDiscordToSlackNames(message.mentions.users) + '\n' +
'Discord channel: #' + message.channel.name + '\n' +
'Link: <' + message.url + '|Link to post>' + '\n' +
结果:收件人:@soda //作为纯文本,而不是@soda用户
整个代码
// require the discord.js module
const Discord = require('discord.js');
const devs = require('./devs.json');
const channels = require('./channels.json');
const dotenv = require('dotenv');
const path = require('path');
var request = require('request');
dotenv.load({
path: path.join(__dirname, `.env`),
silent: true
});
// create a new Discord client
const client = new Discord.Client();
// Map discord usernames of devs to slack usernames
function mapDiscordToSlackNames(discordUsers) {
return discordUsers.map( user => {
return '@' + devs[user.username];
})
}
// when the client is ready, run this code
// this event will only trigger one time after logging in
client.once('ready', () => {
console.log('Discord Connected!');
});
// on message on discord
client.on('message', message => {
console.log(channels[message.channel.name]);
request.post(
`${channels[message.channel.name]}`,
{
json: {
text:
'To: ' + mapDiscordToSlackNames(message.mentions.users) + '\n' +
'Discord channel: #' + message.channel.name + '\n' +
'Link: <' + message.url + '|Link to post>' + '\n' +
'Original Message: \n' +
'\t"' + message.author.username + ': ' + message.cleanContent + '"\n' +
`Attachements: ${message.attachments.map(attachment => attachment.url)}`
},
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
);
});
// login to Discord with app's token
client.login(process.env.DISCORD_TOKEN);
devs是一个json对象,它返回对应于不和谐用户名的松弛用户名。
答案 0 :(得分:1)
结果证明我是通过在字符串中转义'<'和'>'来发送用户ID的,
'<@userid>'
,所以它作为纯文本传递。
要闲时提及某人,请执行“收件人:<@” +用户ID +“>”
用户ID以U开头,可以在URL的
team/
之后找到 您的工作区,例如:Cxxxxx/team/Uxxxxx/