添加OpenAI API额度的到期日期,格式化小数点 (#168)

This commit is contained in:
夏叶 2023-02-19 00:18:11 +08:00 committed by GitHub
parent a4f12eaf09
commit 624e1f6e1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 5 deletions

View file

@ -79,7 +79,7 @@ export class chatgpt extends plugin {
permission: 'master'
},
{
reg: '#OpenAI(剩余)?(余额|额度)',
reg: '#(OpenAI|openai)(剩余)?(余额|额度)',
fnc: 'totalAvailable',
permission: 'master'
},
@ -759,7 +759,7 @@ export class chatgpt extends plugin {
this.reply('当前未配置OpenAI API key请在插件配置文件config/index.js中配置。若使用免费的API3则无需关心计费。')
return false
}
// 查询OpenAI Plus剩余试用额度
// 查询OpenAI API剩余试用额度
fetch('https://api.openai.com/dashboard/billing/credit_grants', {
method: 'GET',
headers: {
@ -770,10 +770,15 @@ export class chatgpt extends plugin {
.then(response => response.json())
.then(data => {
if (data.error) {
console.log(data.error)
// console.log(data.error)
this.reply('获取失败:' + data.error.code)
return false
} else {
this.reply('总额度:$' + data.total_granted + '\n已使用额度$' + data.total_used + '\n当前剩余额度$' + data.total_available)
let total_granted = data.total_granted.toFixed(2)
let total_used = data.total_used.toFixed(2)
let total_available = data.total_available.toFixed(2)
let expires_at = new Date(data.grants.data[0].expires_at * 1000).toLocaleDateString().replace(/\//g, '-')
this.reply('总额度:$' + total_granted + '\n已经使用额度$' + total_used + '\n当前剩余额度$' + total_available + '\n到期日期(UTC)' + expires_at)
}
})
}

View file

@ -89,7 +89,7 @@ let helpData = [
{
icon: 'help',
title: '#OpenAI剩余额度',
desc: '查询OpenAI Plus剩余试用额度'
desc: '查询OpenAI API剩余试用额度'
},
{
icon: 'help',