每次发送不同的欢迎信息

时间:2018-10-31 03:34:40

标签: javascript bots discord discord.js

代码是每次新成员登录到服务器时都会发送不同的欢迎消息(DM消息)。

client.on('guildMemberAdd', ReBeL => {
  var bel = ["Welcome 1 @", "Welcome 2  @!", "Welcome 3 @"]
  var moon = bel[Math.floor(Math.random() * bel.length)];
  moon = moon.replace('@', ReBeL.user)
  setTimeout(function() {
    member.createDM().then(function(channel) {
      return channel.send(moon)
    }).catch(console.error)
  }, 4000)
});

这是错误:

member.createDM().then(function (channel) {
^

ReferenceError: member is not defined
at Timeout._onTimeout (/home/cabox/workspace/bot.js:10:4)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)

1 个答案:

答案 0 :(得分:0)

使用这种方法即可。我测试过了。

client.on('guildMemberAdd', member => {
var ReBeL = member.user.username;
var bel = ["Welcome 1 @","Welcome 2  @!","Welcome 3 @"]
var moon = bel[Math.floor(Math.random() * bel.length)];
moon = moon.replace('@', ReBeL)
setTimeout(function() {
member.createDM().then(function (channel) {
return channel.send(moon) 
 }).catch(console.error)
 },4000)});

您也可以添加服务器和成员数。您需要这样做:

client.on('guildMemberAdd', member => {
var ReBeL = member.user.username;
var ReBeLserver = member.guild.name;
var ReBelcount = member.guild.memberCount;
var bel = [
    "Welcome @, we hope you will enjoy staying in @server ",
    "Welcome @ you are #th member! We hope you will enjoy staying in @server"
]
var moon = bel[Math.floor(Math.random() * bel.length)];
moon = moon.replace('@', ReBeL)
moon = moon.replace('@server', ReBeLserver)
moon = moon.replace('#', ReBelcount)
setTimeout(function() {
member.createDM().then(function (channel) {
return channel.send(moon) 
 }).catch(console.error)
 },4000)});

这是一个嵌入示例,我也添加了鉴别器。

client.on('guildMemberAdd', member => {
var user = member.user.username;
var server = member.guild.name;
var count = member.guild.memberCount;
var discriminator = member.user.discriminator;
var bel = [
    "Welcome @#$, we hope you will enjoy staying in @server ",
    "Welcome @#$ you are -th member! We hope you will enjoy staying in @server"
]
var welcome = bel[Math.floor(Math.random() * bel.length)];
welcome = welcome.replace('@', user)
welcome = welcome.replace('@server', server)
welcome = welcome.replace('-', count)
welcome = welcome.replace('$', discriminator)
setTimeout(function() {
member.createDM().then(function (channel) {
    let welcomeembed = new Discord.RichEmbed()
    .setColor(`RANDOM`)
    .setDescription(welcome)
return channel.send(welcomeembed) 
 }).catch(console.error)
 },100)});