如何将嵌入式消息发送给机器人并进入服务器
P粉127901279
P粉127901279 2023-08-03 16:05:24
0
1
592
<p>我寻找的是将我传递的代码嵌入到机器人每次进入服务器时的第一个可用频道中。<br /><br />这将是代码片段。</p><p><br /></p> <pre class="brush:php;toolbar:false;">const { Client, GatewayIntent`your text`Bits, MessageEmbed } = require('discord.js'); const config = require('./config.json'); const { EmbedBuilder } = require('discord.js'); const client = new Client({ intents: [ GatewayIntentBits.GuildMessages, GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildPresences, GatewayIntentBits.MessageContent ] }); const prefix = config.prefix; client.on('ready', () => { console.log('Bot Ready'); }); client.on('messageCreate', message => { if (message.content === '!ping') { message.channel.send('pong'); } }); client.on('guildCreate', guild => { const channel = guild.channels.cache.find(channel => channel.type === 'text' && channel.permissionsFor(guild.me).has('SEND_MESSAGES')); if (channel) { const exampleEmbed = new MessageEmbed() .setColor(0xF99CF8) .setTitle('**B**') .setAuthor('S') .setThumbnail('https://i.imgur.com/N4') .setDescription('H') channel.send({ embeds: [exampleEmbed] }); } }); client.login(config.token);</pre> <p>由于这段代码,机器人在进入服务器时不会发送任何类型的消息,而是正常启动。</p>
P粉127901279
P粉127901279

全部回复(1)
P粉006847750

如果你正在使用 discord.js v14,那么你需要更新回复消息时使用嵌入消息(Embeds)的方法。只需将代码更改为以下内容:


client.on('guildCreate', guild => {
const channel = guild.channels.cache.find(channel => channel.type === 'text' && channel.permissionsFor(guild.me).has('SEND_MESSAGES'));
if (channel) {
    const exampleEmbed = new EmbedBuilder()
        .setColor(0xF99CF8)
        .setTitle('**B**')
        .setAuthor('S')
        .setThumbnail('https://i.imgur.com/N4')
        .setDescription('H') 
    channel.send({ embeds: [exampleEmbed] });
}});

另外,你需要将第一行修改为:

const { Client, GatewayIntentBits, EmbedBuilder} = require('discord.js');

只需删除第三行以使你的代码更清晰。

要了解更多细节,请访问此处 : https://discordjs.guide/popular-topics/embeds.html#embed-preview

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板