在加入时向我的私人频道发送消息 &离开

2022-01-10 00:00:00 bots node.js javascript discord.js

嘿嘿,

我希望我的机器人在加入时向我的私人不和谐服务器发送嵌入消息 &离开服务器.但问题是它不会在任何地方发送任何东西.我的代码如下所示:

I want my bot to send a embed message to my private discord server when it joins & leaves a server. But the problem is that it does not send anything anywhere. My code looks like this:

exports.run = async (client, guild) => { 

    if(!guild.available) return
    
    
    if(!guild.owner && guild.ownerID) await guild.members.fetch(guild.ownerID);

if(!channel) return;

const embed = new MessageEmbed()
  .setTitle(`Bot joined a server`)
  .setDescription(`${guild.name}`)
  .setColor(0x9590EE)
  .setThumbnail(guild.iconURL())
  .addField(`Owner", "${guild.owner.user.tag}`)
  .addField(`Member Count", "${guild.memberCount}`)
  .setFooter(`${guild.id}`)
client.channels.cache.get('ID').send(embed)
    
  }

推荐答案

您的代码在加入服务器后未激活.为此,您有一个不错的活动(名称具有误导性)guildCreate - 它是每当客户加入公会时发出.

Your code doesn't activate upon joining the server. For that you have a nice event (that has a misleading name) guildCreate - it is emitted whenever the client joins a guild.

所以,你的代码应该是这样的

So, your code should look something like this


client.on('guildCreate', async guild => {
    let YourChannel = await client.channels.fetch('channelid');
  const embed = new Discord.MessageEmbed()
.setTitle(`Bot joined a server`)
.setDescription(`${guild.name}`)
.setColor(0x9590EE)
.setThumbnail(guild.iconURL())
.addField(`Owner`, `${guild.owner.user.tag}`)
.addField(`Member Count`, `${guild.memberCount}`)
.setFooter(`${guild.id}`)
YourChannel.send(embed);
});

离开公会也一样,使用 guildDelete 事件.

Same works for leaving the guild, use guildDelete event.

相关文章