我有一个非常简单的React应用程序,Discord bot和Node.js服务器。
我希望用户在前端应用程序中输入的文本,将由Discord通道中的Discord bot发送。
如果用户键入"hello world
";并通过点击按钮提交,Discord机器人应该输入"hello world
";
前:
function App() {
const [input, setinput] = useState('')
return (
<>
<input onChange={(e) => setinput(e.target.value)}></input>
<button onClick={() => axios.post('http://localhost:8080/post', { post: input })} > push me</button>
</>
)
}
机器人,,BACK[仅相关部分]:
app.post('/pass', async (req, res) => {
post = req.body.post})
client.on('messageCreate', msg => {
if (msg.content === 'ping') {
msg.reply(`${post}`);
}
})
这段代码正在工作。post
是用户输入的文本,但它只在某些条件下显示(用户在Discord上输入消息"ping
")
但是我如何无条件地做同样的事情,让我的bot每次用户在前端提交这条消息时都发布一条消息?
任何帮助都将非常感激。
我找到了使用Discord webhooks的方法:
app.post('/pass', async (req, res) => {
post = req.body.post
const embed = new EmbedBuilder()
.setTitle(`${post}`)
.setColor(0x00FFFF);
webhookClient.send({
content: '',
username: 'Mighty John Silver',
avatarURL: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTLHezDv-KefVm8icZAoVxZOJAsICm2gB5QKg&usqp=CAU',
embeds: [embed],
});
})