我正在try 做一个函数,将验证一个Discord邀请,并判断邀请是否是临时的,只是从一个Discord邀请URL.如果邀请是临时的,我希望它将过期日期返回为new Date
.如果它没有过期,我希望函数返回false
.
async function getInviteExpiration(inviteUrl) {
// Extract the invite code from the URL
const regex = /discord\.gg\/([a-zA-Z0-9]+)/;
const match = inviteUrl.match(regex);
if (!match) {
throw new Error('Invalid invite URL');
}
const inviteCode = match[1];
const response = UrlFetchApp.fetch(`https://discord.com/api/invites/${inviteCode}?with_counts=true`);
if (response.getResponseCode() !== 200) {
throw new Error('Failed to fetch discord invite information');
}
const inviteData = JSON.parse(response.getContentText());
console.log(inviteData.expires_at); // Temporary, just for testing
console.log(new Date(inviteData.expires_at)); // Temporary, just for testing
if (inviteData.expires_at === null) {
return false; // Invite never expires
} else {
return new Date(inviteData.expires_at); // Return expiration date
}
}
我做了一个测试函数来测试getInviteExpiration
.
function test() {
const permanentUrl = "https://discord.gg/minecraft";
console.log(getInviteExpiration(permanentUrl)); // Expecting false
const tempUrl = "https://discord.gg/DdMJXskh";
console.log(getInviteExpiration(tempUrl)); // Expecting Date
}
这是输出的控制台
null
Wed Dec 31 1969 18:00:00 GMT-0600 (Central Standard Time)
{}
2024-04-18T02:27:18+00:00
Wed Apr 17 2024 21:27:18 GMT-0500 (Central Daylight Time)
{}
为什么在getInviteExpiration
函数中,我得到的对象是空的,而它可以解析console.log(new Date(inviteData.expires_at))
?