Skip to content

Commit

Permalink
feat(src/push): 新增 Discord Webhook 推送
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Sep 16, 2023
1 parent 18c292d commit 7ac075c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
71 changes: 71 additions & 0 deletions src/push/discord.ts
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,
},
})
}
}
10 changes: 5 additions & 5 deletions src/push/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export type TelegramOption = {
*/
export class Telegram implements Send {

TELEGRAM_BOT_TOKEN: string
TELEGRAM_CHAT_ID: string
TELEGRAM_SEND_SILENTLY?: boolean = false
TELEGRAM_PROTECT_CONTENT?: boolean = false
TELEGRAM_MESSAGE_THREAD_ID?: string
private TELEGRAM_BOT_TOKEN: string
private TELEGRAM_CHAT_ID: string
private TELEGRAM_SEND_SILENTLY?: boolean = false
private TELEGRAM_PROTECT_CONTENT?: boolean = false
private TELEGRAM_MESSAGE_THREAD_ID?: string

constructor(option: TelegramOption) {
Debugger('option: %O', option)
Expand Down

0 comments on commit 7ac075c

Please sign in to comment.