-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(src/push): 新增 Discord Webhook 推送
- Loading branch information
1 parent
18c292d
commit 7ac075c
Showing
2 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { AxiosResponse } from 'axios' | ||
import debug from 'debug' | ||
import { Send } from '../interfaces/send' | ||
import { ajax } from '@/utils/ajax' | ||
|
||
const Debugger = debug('push:discord') | ||
|
||
/** | ||
* Discord Webhook 推送 | ||
* | ||
* @author CaoMeiYouRen | ||
* @date 2023-09-17 | ||
* @export | ||
* @class Discord | ||
*/ | ||
export class Discord implements Send { | ||
/** | ||
* Webhook Url 可在服务器设置 -> 整合 -> Webhook -> 创建 Webhook 中获取 | ||
* | ||
* @author CaoMeiYouRen | ||
* @date 2023-09-17 | ||
* @private | ||
*/ | ||
private DISCORD_WEBHOOK: string | ||
|
||
/** | ||
* 机器人显示的名称 | ||
* | ||
* @author CaoMeiYouRen | ||
* @date 2023-09-17 | ||
* @private | ||
*/ | ||
private DISCORD_USERNAME?: string | ||
|
||
/** | ||
* | ||
* @author CaoMeiYouRen | ||
* @date 2023-09-17 | ||
* @param DISCORD_WEBHOOK Webhook Url 可在服务器设置 -> 整合 -> Webhook -> 创建 Webhook 中获取 | ||
* @param [DISCORD_USERNAME] 机器人显示的名称 | ||
*/ | ||
constructor(DISCORD_WEBHOOK: string, DISCORD_USERNAME?: string) { | ||
Debugger('DISCORD_WEBHOOK: %s, DISCORD_USERNAME: %s', DISCORD_WEBHOOK, DISCORD_USERNAME) | ||
this.DISCORD_WEBHOOK = DISCORD_WEBHOOK | ||
if (DISCORD_USERNAME) { | ||
this.DISCORD_USERNAME = DISCORD_USERNAME | ||
} | ||
if (!this.DISCORD_WEBHOOK) { | ||
throw new Error('DISCORD_WEBHOOK 是必须的!') | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @author CaoMeiYouRen | ||
* @date 2023-09-17 | ||
* @param content 要发送的内容 | ||
* @return | ||
*/ | ||
async send(content: string): Promise<AxiosResponse<any>> { | ||
return ajax({ | ||
url: this.DISCORD_WEBHOOK, | ||
method: 'POST', | ||
data: { | ||
username: this.DISCORD_USERNAME, | ||
content, | ||
}, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters