問題描述
我想編寫一個機器人,將用戶發送的消息嵌入特定頻道.如果您對 GTA RP 服務器有所了解,那就像 Twitter 或 Instagram 機器人.
這是一個例子:
我認為這與 console.log
和作者姓名有關,但我不確定所以這就是我在這里的原因.如何嵌入用戶的消息,例如這個?
你可以使用 MessageEmbed
,就像programmerRaj說的那樣,或者使用embed屬性#/docs/main/stable/typedef/MessageOptions" rel="nofollow noreferrer">MessageOptions
:
const {MessageEmbed} = require('discord.js')常量嵌入 = 新 MessageEmbed().setTitle('一些標題').setDescription('一些描述').setImage('圖片地址')//Discord.js v13//這兩個是同一個東西channel.send({embeds: [embed]})頻道.發送({嵌入:[{標題:'一些標題',描述:'一些描述',圖片:{url:'圖片網址'}}]})//Discord.js v12//這兩個是同一個東西頻道.發送(嵌入)頻道.發送({嵌入:{標題:'一些標題',描述:'一些描述',圖片:{url:'圖片網址'}}})
要在特定頻道中發送嵌入的用戶消息,您可以執行以下操作,其中 client
是您的 Discord.js Client
:
//你想發送消息的頻道const channel = client.channels.cache.get('channel id')client.on('消息',消息 => {//忽略機器人如果(message.author.bot)返回//發送嵌入常量嵌入 = 新 MessageEmbed().setDescription(message.content).setAuthor(message.author.tag, message.author.displayAvatarURL())channel.send({embeds: [embed]}).catch(console.error)//Discord.js v12://channel.send(embed).catch(console.error)})
請注意,上面的代碼將為不是由機器人發送的每條消息發送嵌入,因此您可能需要對其進行修改,使其僅在您需要時發送.p>
我建議閱讀 Discord.js 的嵌入指南(archive) 或鏈接的文檔有關如何使用嵌入的更多信息.
I want to code a bot that will embed a user's sent message in a specific channel. If you know anything about GTA RP servers, it's like a Twitter or Instagram bot.
Here's an example:
I think it's something about the console.log
and the author's name, but I'm not sure so that's why I'm here. How can I embed users' messages like
this?
You can use a MessageEmbed
, like programmerRaj said, or use the embed
property in MessageOptions
:
const {MessageEmbed} = require('discord.js')
const embed = new MessageEmbed()
.setTitle('some title')
.setDescription('some description')
.setImage('image url')
// Discord.js v13
// These two are the same thing
channel.send({embeds: [embed]})
channel.send({
embeds: [{
title: 'some title',
description: 'some description',
image: {url: 'image url'}
}]
})
// Discord.js v12
// These two are the same thing
channel.send(embed)
channel.send({
embed: {
title: 'some title',
description: 'some description',
image: {url: 'image url'}
}
})
To send an embed of users' message in a particular channel, you can do something like this, where client
is your Discord.js Client
:
// The channel that you want to send the messages to
const channel = client.channels.cache.get('channel id')
client.on('message',message => {
// Ignore bots
if (message.author.bot) return
// Send the embed
const embed = new MessageEmbed()
.setDescription(message.content)
.setAuthor(message.author.tag, message.author.displayAvatarURL())
channel.send({embeds: [embed]}).catch(console.error)
// Discord.js v12:
// channel.send(embed).catch(console.error)
})
Note that the above code will send the embed for every message not sent by a bot, so you will probably want to modify it so that it only sends it when you want it to.
I recommend reading Discord.js' guide on embeds (archive) or the documentation linked above for more information on how to use embeds.
這篇關于如何使用 Discord 機器人嵌入消息?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!