您可以在控制台日志或不和谐频道中回答 dms 的机器人

A bot where you can answer dms in the console log or a discord channel

我搜索了一下,得到了一个带有 readline 的代码,它看起来像这样:

const Disc = require('discord.js');
const client = new Disc.Client();
const token = 'token' 
const readline = require('readline');

client.login(token);

client.on('message', function(message){
    if(message.channel.type === 'dm'){
        console.log("[" + message.author.username + "]: " + message.content) 


        const rl = readline.createInterface({
            input: process.stdin,
            output: process.stdout
        });

        rl.question('REPLY TO ' + message.author.username + ': ', (answer) => {
            message.author.send(`${answer}`);
            rl.close();
        });
     }
 });

但是它不起作用helpp

实际上这是我最近才做的一个主题,所以我将引导您完成它并为您提供一些代码。

首先,我想说的是,当您提出 post 时,请包含一个明确的问题。听上去,您需要一个将 dms 记录到控制台或响应它们的机器人。我就回答这两个问题。

检查 DM 的最简单方法是查看消息通道类型是否为 DM。查看 here 以获取有关频道 class 的更多信息。您可以通过这样做来检查频道是否为特定类型:

if (message.channel.type === 'dm'){ } // change dm to the type you want

这必须放在您的消息功能中,所以现在,如果您继续操作,代码将如下所示:

bot.on('message', async message => {
    if (message.channel.type === 'dm'){ }
});

从那里它只是将代码添加到 if 语句的内部。你总是想要一个 return 语句在里面,以防万一什么都没发生,所以它不会尝试在频道中做任何事情。

对于你想要的,这会将 DM 记录到控制台并回复它,如果它等于某个消息。

bot.on('message', async message => {
    if (message.channel.type === 'dm'){ 
        console.log(message.content);
        if(message.content === "something"){
            return await message.channel.send("Hi!");
        }
        return;
    }
});

这应该是你想要的,如果你有任何问题,请在这里评论,我会尽快回复:)

编辑:

bot.on('message', async message => {
    if (message.channel.type === 'dm'){ 
        console.log(`${message.author.username} says: ${message.content}`);
        const rl = readline.createInterface({
            input: process.stdin,
            output: process.stdout
        });

        rl.question(`REPLY TO ${message.author.username}: `, (answer) => {
            message.author.send(`${answer}`);
            rl.close();
        });

    }
});