简体   繁体   中英

Unresolved method in WebStorm (discord.js, code works)

I'm wrapping my head around this. Autocompletion does not work .then(r => { HERE }) either.

Kinda starting out with this and would be way easier if it just works (works outside of the promise).

Code runs without any problems as well. delete method is recognized as well but not at that part.

I have this problem in a bigger project as well and it gets me confused.

Trying to find something on the web for a few hours, but couldn't find anything that helps me out. Hope I wasn't blind at some point:P

Whole test source:

const Discord = require('discord.js');

const client = new Discord.Client();

client.on("message", message => {
    if (message.content === 'test'){
        message.channel.send('something').then(r => r.delete(5000));
    }
});

Problem: 在此处输入图像描述

If you need to delete command triget message you can use

const Discord = require('discord.js');

const client = new Discord.Client();
    client.on("message", message => {
        if (message.content === 'test'){
            message.channel.send('something') 
            message.delete(1000)
            .catch(console.error)
        }
    });

If you use need to delete response message after some time you code must work, but you can try use reply method.

client.on("message", message => {
    if (message.content === 'test'){
        message.reply('somethink')
        .then(msg => {
            msg.delete(10000)
        })
        .catch(console.error);
    }
});

Maybe problem in you discord.js version? In v.12 version you need use msg.delete({ timeout: 10000 })

Just means webstorm can't discern what functions the object will have after the promise resolves. This is because the message create action in discord.js has multiple return types. So it's possible for the message object not to be passed into r, for instance in the event that the message failed to send, possibly by trying to send a message to a channel without the proper permissions.

If you add a check to confirm that r is the same type as message before trying to call .delete() I believe the warning will go away.

You can observe the potential error this highlight is warning you of by removing the bots permission to send messages in a channel, then by sending "test" to that same channel.

having had the error recently and having found a solution, if for example you want a text channel, by doing a get you can have a text or a voice. so check if the channel instance TextChannel (for my example)

let channel = client.guilds.cache.get(process.env.GUILD_ID).channels.cache.get(config.status_channel);
        if (channel instanceof TextChannel){
            await channel.messages.fetch()
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM