有没有办法在成功加载后延迟发送 Discord 消息?
Is there a way to delay sending a Discord message after something has successfully loaded?
因此,我尝试创建一个应该在用户离开 Discord 后立即执行的 RichEmbed 消息。在此 RichEmbed 中应该是来自 giphy 的随机 GIF,我使用此节点模块在 getGiphyPic() 函数中创建了它:https://github.com/risan/giphy-random
执行此事件时,将在没有 .setImage() 的情况下发送嵌入。我试过 console.log 并且似乎在成功创建 URL 之前发送了消息。
我已经尝试使事件函数异步并等待图像变量的创建,我还尝试在 giphyRandom 函数之后创建一个 promise,但这似乎并不能解决我的问题。
const Discord = require('discord.js');
const { token, giphyToken } = require('./config.json');
const client = new Discord.Client();
const giphyRandom = require("giphy-random");
function getGiphyPic() {
(async () => {
await giphyRandom(giphyToken, {
tag: "fail",
rating: "pg-13"
}).then(resp => {
let { data } = resp;
console.log('Created GiphyURL: ' + JSON.stringify(data.image_url))
return JSON.stringify(data.image_url);
});
})();
};
client.on('guildMemberRemove', async function (member) {
var logChannel = client.channels.get('60370230389694091');
if (!logChannel) return;
var image = await getGiphyPic();
var embed = new Discord.RichEmbed()
.setColor(0xc62828)
.setAuthor('Someone has left the Server!', member.user.avatarURL);
.setImage(image);
logChannel.send(embed).then(message => console.log("Sent message!"));
});
您可以使用提供给您的承诺...
const Discord = require('discord.js');
const { token, giphyToken } = require('./config.json');
const client = new Discord.Client();
const giphyRandom = require("giphy-random");
function getGiphyPic() {
return giphyRandom(giphyToken, {
tag: "fail",
rating: "pg-13"
}).then(resp => {
let { data } = resp;
console.log('Created GiphyURL: ' + JSON.stringify(data.image_url))
return data.image_url; // give correct response
});
};
client.on('guildMemberRemove', function (member) {
var logChannel = client.channels.get('60370230389694091');
if (!logChannel) return;
getGiphyPic().then(image => {
var embed = new Discord.RichEmbed()
.setColor(0xc62828)
.setAuthor('Someone has left the Server!', member.user.avatarURL);
.setImage(image);
logChannel.send(embed).then(message => console.log("Sent message!"));
});
});
async / await 对于复杂的流控制很有用,但在这里似乎不需要,如果你想使用它,你就太复杂了:
function getGiphyPic() {
return giphyRandom(giphyToken, {
tag: "fail",
rating: "pg-13"
}).then(resp => {
let { data } = resp;
console.log('Created GiphyURL: ' + JSON.stringify(data.image_url))
return data.image_url;
});
};
只需 return promise 和 async / await 将处理其余部分。
因此,我尝试创建一个应该在用户离开 Discord 后立即执行的 RichEmbed 消息。在此 RichEmbed 中应该是来自 giphy 的随机 GIF,我使用此节点模块在 getGiphyPic() 函数中创建了它:https://github.com/risan/giphy-random 执行此事件时,将在没有 .setImage() 的情况下发送嵌入。我试过 console.log 并且似乎在成功创建 URL 之前发送了消息。
我已经尝试使事件函数异步并等待图像变量的创建,我还尝试在 giphyRandom 函数之后创建一个 promise,但这似乎并不能解决我的问题。
const Discord = require('discord.js');
const { token, giphyToken } = require('./config.json');
const client = new Discord.Client();
const giphyRandom = require("giphy-random");
function getGiphyPic() {
(async () => {
await giphyRandom(giphyToken, {
tag: "fail",
rating: "pg-13"
}).then(resp => {
let { data } = resp;
console.log('Created GiphyURL: ' + JSON.stringify(data.image_url))
return JSON.stringify(data.image_url);
});
})();
};
client.on('guildMemberRemove', async function (member) {
var logChannel = client.channels.get('60370230389694091');
if (!logChannel) return;
var image = await getGiphyPic();
var embed = new Discord.RichEmbed()
.setColor(0xc62828)
.setAuthor('Someone has left the Server!', member.user.avatarURL);
.setImage(image);
logChannel.send(embed).then(message => console.log("Sent message!"));
});
您可以使用提供给您的承诺...
const Discord = require('discord.js');
const { token, giphyToken } = require('./config.json');
const client = new Discord.Client();
const giphyRandom = require("giphy-random");
function getGiphyPic() {
return giphyRandom(giphyToken, {
tag: "fail",
rating: "pg-13"
}).then(resp => {
let { data } = resp;
console.log('Created GiphyURL: ' + JSON.stringify(data.image_url))
return data.image_url; // give correct response
});
};
client.on('guildMemberRemove', function (member) {
var logChannel = client.channels.get('60370230389694091');
if (!logChannel) return;
getGiphyPic().then(image => {
var embed = new Discord.RichEmbed()
.setColor(0xc62828)
.setAuthor('Someone has left the Server!', member.user.avatarURL);
.setImage(image);
logChannel.send(embed).then(message => console.log("Sent message!"));
});
});
async / await 对于复杂的流控制很有用,但在这里似乎不需要,如果你想使用它,你就太复杂了:
function getGiphyPic() {
return giphyRandom(giphyToken, {
tag: "fail",
rating: "pg-13"
}).then(resp => {
let { data } = resp;
console.log('Created GiphyURL: ' + JSON.stringify(data.image_url))
return data.image_url;
});
};
只需 return promise 和 async / await 将处理其余部分。