我正在尝试将给不和谐机器人的反应转换为使用特定反应时发送消息的机器人。我无法回应

时间:2020-05-13 17:48:21

标签: javascript botframework discord discord.js

我能够创建一个能够在使用特定反应时发挥作用的机器人,但希望该机器人在使用特定反应时能够在聊天中发送消息。原始代码(用于响应bot)已被注释掉,而我的错误在底部


const Discord = require('discord.js');
const bot = new Discord.Client({
    partials: ['MESSAGE','REACTION']
});
const { token } = require('./config.json');

const Yard = '709954577713201173'
const MessageNumber = '7710157795517005854'

bot.once('ready', () => {
    console.log('Ready!');
});

bot.login(token);

bot.on('messageReactionAdd', async (reaction, user) => {
    console.log('Message Reaction Add Top');

    reaction.channel.send = async () => {
        try {
        //applyRole instead of reaction.channel.send
            let emojiName = reaction.emoji.name;
            let role = reaction.message.guild.roles.cache.find;
            let member = reaction.message.guild.members.cache.find(member => member.id == user.id);
            if (reaction.emoji.name === '⚔️' && member) {
                // if (role && member) {
                console.log('React and Member Found');
                await reaction.channel.send('Pong.');
            // await member.roles.add(Yard);
            }
        }
        catch (err) {
            console.log(err);
        }
    };
    if (reaction.message.partial) {
        try {
            let msg = await reaction.message.fetch()
            console.log(msg.id);
            if (msg.id === MessageNumber && reaction.emoji.name === '⚔️') {
                console.log('Cached - Applied');
                reaction.channel.send('Pong.');
                // applyRole();
            }
        }
        catch (err) {
            console.log(err);
        }
    }
    else {
        console.log('Not a Partial');
        if (reaction.message.id === MessageNumber) {
            console.log('Not a Partial - applied')
            reaction.channel.send('Pong.');
            // applyRole();
        }
    }
});

错误:


(node:11908) UnhandledPromiseRejectionWarning: TypeError: Cannot set property 'send' of undefined
    at Client.<anonymous> (C:\Users\sorak\Desktop\dkp-bot\test.js:19:24)
    at Client.emit (events.js:315:20)
    at MessageReactionAdd.handle (C:\Users\sorak\node_modules\discord.js\src\client\actions\MessageReactionAdd.js:44:17)
    at Object.module.exports [as MESSAGE_REACTION_ADD] (C:\Users\sorak\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_REACTION_ADD.js:4:37)
    at WebSocketManager.handlePacket (C:\Users\sorak\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)     
    at WebSocketShard.onPacket (C:\Users\sorak\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\Users\sorak\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\Users\sorak\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\sorak\node_modules\ws\lib\websocket.js:800:20)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11908) 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:11908) [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)

您已经具有名称为reaction的变量。只需从该行删除 let

let reaction.channel.send = async () => {

答案 1 :(得分:0)

我在这段代码中犯了很多错误,因为我没有在需要机器人发送消息的地方执行react.message.channel.send,所以我使用emoji.id代替了react.emoji .name和其他问题。 (也没有回叫applyRole,我后来将其更改为sendMsg)

这终于可以为我工作:

const Discord = require('discord.js');
const bot = new Discord.Client({
    partials: ['MESSAGE','REACTION']
});
const { token } = require('./config.json');

const MessageNumber = '710334220643336233'

bot.once('ready', () => {
    console.log('Ready!');
});

bot.login(token);

bot.on('messageReactionAdd', async (reaction, user) => {
    console.log('Message Reaction Add Top');

    let sentMsg = async () => {
        try {
            let emojiName = reaction.emoji.name;
            let role = reaction.message.guild.roles.cache.find;
            let member = reaction.message.guild.members.cache.find(member => member.id == user.id);
            if (reaction.emoji.name === '⚔️' && member) {
                console.log('React and Member Found');
                await reaction.message.channel.send('Pong.');
            }
        }
        catch (err) {
            console.log(err);
        }
    };
    if (reaction.message.partial) {
        try {
            let msg = await reaction.message.fetch()
            console.log(msg.id);
            if (msg.id === MessageNumber && reaction.emoji.name === '⚔️') {
                console.log('Cached - sent');
                sentMsg();
            }
        }
        catch (err) {
            console.log(err);
        }
    }
    else {
        console.log('Not a Partial');
        if (reaction.message.id === MessageNumber) {
            console.log('Not a Partial - applied')
            // reaction.message.channel.send('piss smh');
            sentMsg();
        }
    }
});