简体   繁体   English

如何使用 discord.js 制作问候语

[英]How to make a greeting message with discord.js

I am making a bot in the discord using discord.js and I want that when a person joins the server and open a chat with the bot, the bot is going to send the first message in the chat, something like "my name is charlie, how can I help you", how can I do that?我正在使用 discord.js 在 discord 中创建一个机器人,我希望当一个人加入服务器并与机器人打开聊天时,机器人将发送聊天中的第一条消息,例如“我的名字是查理,我怎么能帮到你”,我该怎么做? All the examples I see are similar to the code below, but it is not working.我看到的所有示例都类似于下面的代码,但它不起作用。

client.on('guildMemberAdd', member => {
    const channel = member.guild.channels.find(ch => ch.name === 'member-log');
    if (!channel) return;
    channel.send(`Welcome to the server, ${member}!`);
});

EDIT 1: I tried member.guild.channels.cache.find as said below and the error has disappeared, but the bot is not saying anything when I go to the chat, I think that channel is null so it returned without saying anything, how can I fix this?编辑 1:我尝试了 member.guild.channels.cache.find 如下所述,错误消失了,但是当我去聊天时机器人没有说什么,我认为频道是空的,所以它什么也没说就返回了,我怎样才能解决这个问题?

EDIT 2: Below you can see how my code is right now, I am trying to get my bot channel so that when a person gets in my server, the bot sends him a welcome message, but the channel was not found, I do not understand the problem.编辑 2:您可以在下面看到我的代码现在的状态,我正在尝试获取我的机器人频道,以便当有人进入我的服务器时,机器人会向他发送欢迎消息,但找不到该频道,我没有了解问题。

client.on("guildMemberAdd", (member) => {
  const channel = member.guild.channels.cache.find((ch) => ch.id === myBotId);
  if (!channel) {
    console.log("no channel");
    return;
  }
  channel.send(`Welcome to the server, ${member}!`);
});

您的问题是您需要使用cache属性:

message.guild.channels.cache.find();
const channel = member.guild.channels.find(ch => ch.name === 'member-log'); if (!channel) return; channel.send(`Welcome to the server, ${member}!`);

The code you posted sends a Welcome to the server, ${member}!您发布的代码向服务器发送了欢迎消息,${member}! message to the member-log channel in your guild.消息到您公会的会员日志频道。 Since the code you are using looks fine, it is most likely that you don't have a channel with the name member-log .由于您使用的代码看起来不错,很可能您没有名为member-log的频道。

Create a member-log channel to fix the issue or find the channel in which you want to send the greeting like so const channel = member.guild.channels.find(ch => ch.name === 'your-channel-name');创建一个会员日志频道来解决这个问题,或者像这样找到你想要发送问候的const channel = member.guild.channels.find(ch => ch.name === 'your-channel-name');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM