为什么我在获取信息时出现错误?

问题描述 投票:0回答:1

调用脚本时出现了错误。能否请教一下?

下面我贴上代码和错误。

let chn = client.channels.find(channel => channel.id === '717019301357420554');
let msg = chn.fetchMessage('717369584801546273');
msg.edit(embedit);
TypeError: msg.edit is not a function
fetch discord.js message edit
1个回答
0
投票

这是11版吗?

不管怎么说取东西都是异步的,所以你需要等待msg解析。

https:/discord.js.org#docsmain11.1.0classTextChannel?scrollTo=fetchMessage。

下面是你可以如何去做。

首先是使用 await,它需要在一个异步函数中使用

let chn = client.channels.find(channel => channel.id === '717019301357420554');
let msg = await chn.fetchMessage('717369584801546273');
msg.edit(embedit);

二是 .then

let chn = client.channels.find(channel => channel.id === '717019301357420554');
chn.fetchMessage('717369584801546273').then(msg => msg.edit(embedit));

如果你想把它保存在一个变量中

let chn = client.channels.find(channel => channel.id === '717019301357420554');
let msg;
chn.fetchMessage('717369584801546273').then(m => msg = m);
//note you will have to wait for the promise to resolve to use 
//the variable msg correctly
//any code beyond this isn't guranteed to have access to 

这些都是一些糟糕的变量名称,你不应该使用缩写,如 chn 颇为 channelembedit => embEdit. 但取决于你

© www.soinside.com 2019 - 2024. All rights reserved.