javascript - 在 Discord RPC 的 JavaScript 函数中使用变量
问题描述
我一直在尝试让 Discord RPC 正常工作,以便在获取时更新歌曲标题。我知道睡眠功能目前可能不起作用,但主要问题是能够使用“文本”变量作为 Discord RPC 的输出。有没有人对我如何在丰富的存在中使用“文本”变量有任何建议?
const clientId = 'REDACTED';
const scopes = ['rpc', 'rpc.api', 'messages.read'];
const client = require('discord-rich-presence')(clientId);
const website = 'https://jetstreamradio.com/scSongData'
//var text = '';
function updateRichPres() {
fetch(website)
.then(res => res.text())
//.then(text => console.log(text))
.then(text => res.text())
.then(function (text) { return text })
.then(client.updatePresence({
state: text,
details: 'Now listening to...',
startTimestamp: Date.now(),
largeImageKey: 'jetstream_logo_tunein',
//smallImageKey: 'jetstream_logo_tunein',
instance: true,
}));
while (true) {
//await sleep(60000);
console.log('x');
}
};
client.on('connected', () => {
console.log('connected!');
updateRichPres()
});```
解决方案
您没有将函数传递给 .then()。这里发生的是 client.updatePresence 调用是在 fetch 完成之前进行的,因为您是直接调用它,而不是作为回调提供它。
它应该是:
fetch(website).then(res =>
client.updatePresence({
state: res.text(),
details: 'Now listening to...',
startTimestamp: Date.now(),
largeImageKey: 'jetstream_logo_tunein',
instance: true
})
);
箭头 (=>) 是用于创建函数的 JavaScript 语法。
在这个相当复杂的用例中使用它之前,我建议您了解更多关于基本 JavaScript 语法的知识。
推荐阅读
- c# - 所选 excel WorkSheet 的 m_ObjectToDataMap 为“null”
- c++ - 如果没有逻辑方法来定义比较运算符,如何使用自定义类作为 std::map 的键?
- c# - ASP.NET Core 2.2 锁定未激活
- angular - 结构指令获取子指令
- javascript - 如何使用javascript从另一个文件中设置元素的innerHTML?
- c# - 将 IN 子句中的多个数字作为参数传递给 Oracle 查询时出现无效数字异常
- angular - gulp 错误:错误:找不到模块'@microsoft/sp-build-web'
- python - 如何选择 QTableWidget 的多个单元格?
- html - 为什么我的
出现在同一行的段落?
- python - python发生异常错误时播放声音