如何使用discord.js将DM发送给用户?

时间:2020-05-20 00:59:39

标签: discord.js

我将一个愚蠢的机器人放在一起,它将在出现提示时加入语音通道并播放Gordon Ramsay的剪辑。经过反复试验后,语音功能效果很好。目前,我正在尝试让DM列出可以播放的mp3,但我遇到了错误。我认为我向DM用户的if语句正在与我的逻辑进行交互以在语音通道中播放随机mp3。我对此还很陌生。

client.on('message', async message => {
if(message.member.voice.channel && message.content === `${prefix}list`) {
    message.author.send('placeholder');
}
if (message.member.voice.channel && message.content === `${prefix}gordon`) {
    const connection = await message.member.voice.channel.join();
    const dispatcher = connection.play(mp3[Math.floor(Math.random() * mp3.length)]);
    dispatcher.on('start', () => {
        console.log('Audio is now playing!\n Deleting command in chat.');
        message.delete();
    });
    dispatcher.on('finish', () => {
        console.log('Audio has finished playing!');
        connection.disconnect();
    });
    dispatcher.on('error', console.error);
}});

它成功发送了DM,但有错误。

```(node:14168) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'voice' of null
at Client.<anonymous> (c:\Users\Havok\Desktop\GordonBot\index.js:53:20)
at Client.emit (events.js:310:20)
at MessageCreateAction.handle (c:\Users\Havok\Desktop\GordonBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (c:\Users\Havok\Desktop\GordonBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (c:\Users\Havok\Desktop\GordonBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
at WebSocketShard.onPacket (c:\Users\Havok\Desktop\GordonBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
at WebSocketShard.onMessage (c:\Users\Havok\Desktop\GordonBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
at WebSocket.onMessage (c:\Users\Havok\Desktop\GordonBot\node_modules\ws\lib\event-target.js:125:16)
at WebSocket.emit (events.js:310:20)
at Receiver.receiverOnMessage 
(node:14168) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:14168) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.







2 个答案:

答案 0 :(得分:0)

当漫游器收到DM消息时,message.member将是null(因为它不能是公会成员;它不是在公会中发送的。)

在尝试使用message.member.voice或播放音乐之前,先检查消息是否是在公会中发送的:

client.on('message', async message => {
    if (!message.guild) return;
    // rest of code...
})

答案 1 :(得分:0)

dm用户

message.user.send(“ msg”);

client.on('message', async message => {
if(message.member.voice.channel && message.content === `${prefix}list`) {
messsge.user.send("Message");
}
})