私信直接发消息欢迎新用户加入服务器

Send a direct message on private message to welcome new users who join the server

我想让机器人向加入服务器的新用户发送 direct/private 消息。它能够 post 频道上的欢迎消息,但它不断抛出直接消息的错误。

const Discord = require('discord.js');
const bot = new Discord.Client ();

bot.on('ready', () => {
  console.log('I am ready!');
});

bot.on('guildMemberAdd', member => {
       member.guild.defaultChannel.send(`Welcome to the server, ${member}!`);
       console.log(`${member.user.username} has joined`);
});

bot.login('TOKEN_HERE');

Member 对象有一个发送方法,就像通道一样。

注意 sendMessage 已弃用

const Discord = require('discord.js');
const bot = new Discord.Client ();

bot.on('guildMemberAdd', member => {
   member.send("Welcome to the server!");
});

bot.login('TOKEN_HERE');

使用 Python 可以 运行 脚本如下 (Source)

# bot.py
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user.name} has connected to Discord!')

@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(
        f'Hi {member.name}, welcome to my Discord server!'
    )

client.run(TOKEN)

on_member_join() 处理新成员加入公会的事件。

在此示例中,它使用 member.create_dm() 创建直接消息通道。然后,使用该渠道 .send() 向该新成员发送直接消息。

client.on('guildMemberAdd', member => {
  member.send(`Welcome to the server, ${member}!`);
});

只需添加,或者如果您想要更加个性化或使用嵌入

client.on('guildMemberAdd', member => {
  const embed = new Discord.MessageEmbed()
    .setColor('color number')
    .setTitle('your welcome message')
    .setDescription(`yourwelcome message, ${member}!`)
    .setThumbnail(member.user.displayAvatarURL())
    .addFields(
      { name: 'User:', value: `${member.user.tag}`, inline: true },
      { name: 'ID:', value: `${member.user.id}`, inline: true },
      { name: 'Joined at:', value: `${member.joinedAt}`, inline: true },
    )   
  member.send(embed);
})