Discord.js:使用昵称代替用户名

时间:2020-06-09 11:38:52

标签: discord.js

我已经使用了初学者指南,开始为我们的公会不和谐而使用一个简单的机器人。但是,出于对我的爱,我无法找到一种方法来让bot引用服务器特定的昵称而不是用户名。

代码看起来像这样:

bot.on('message', function (user, userID, channelID, message, evt) {
    if (message.substring(0, 1) == '!') {
        var args = message.substring(1).split(' ');
        var cmd = args[0];

        args = args.splice(1);
        switch(cmd) {
            case 'ping':
                bot.sendMessage({
                    to: channelID,
                    message: 'Pong! was returned to user with nickname ' + ????????????.nickname
                });
            break;
         }
     }
}

有人知道命令/变量是什么来检索昵称而不是用户名吗?

非常感谢您:)

1 个答案:

答案 0 :(得分:0)

我不太理解您的问题,但是如果我理解正确的话。您需要在回复消息中获取member.nickname。

在第一个message.channel.send('text')事件中,仅返回message对象。

要在同一频道上回复,可以使用bot.on('message', message => { if (message.substring(0, 1) == '!') { var args = message.substring(1).split(' '); var cmd = args[0]; args = args.splice(1); switch(cmd) { case 'ping': message.channel.send(`Pong! was returned to user with nickname ${message.member.displayName}`) break; } } }) 如果需要获取成员昵称,则可以使用member.displayName,它将返回成员服务器的昵称,如果成员没有昵称,则返回成员用户名+标记

private fun sendNotification(messageBody: String) {
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT)

        val channelId = "" // TODO CHANNEL ID NEEDED
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationBuilder = NotificationCompat.Builder(this, channelId)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT)
            notificationManager.createNotificationChannel(channel)
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
    }