mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-13 06:57:05 +03:00
add warning webhook
This commit is contained in:
@@ -5,6 +5,7 @@ import { isUserVIP } from "../utils/isUserVIP";
|
||||
import { getHashCache } from "../utils/getHashCache";
|
||||
import { HashedUserID, UserID } from "../types/user.model";
|
||||
import { config } from "../config";
|
||||
import { generateWarningDiscord, warningData, dispatchEvent } from "../utils/webhookUtils";
|
||||
|
||||
type warningEntry = {
|
||||
userID: HashedUserID,
|
||||
@@ -21,6 +22,8 @@ function checkExpiredWarning(warning: warningEntry): boolean {
|
||||
return warning.issueTime > expiry && !warning.enabled;
|
||||
}
|
||||
|
||||
const getUsername = (userID: HashedUserID) => db.prepare("get", `SELECT "userName" FROM "userNames" WHERE "userID" = ?`, [userID], { useReplica: true });
|
||||
|
||||
export async function postWarning(req: Request, res: Response): Promise<Response> {
|
||||
if (!req.body.userID) return res.status(400).json({ "message": "Missing parameters" });
|
||||
|
||||
@@ -62,6 +65,27 @@ export async function postWarning(req: Request, res: Response): Promise<Response
|
||||
resultStatus = "removed from";
|
||||
}
|
||||
|
||||
const targetUsername = await getUsername(userID) ?? null;
|
||||
const issuerUsername = await getUsername(issuerUserID) ?? null;
|
||||
const webhookData = {
|
||||
target: {
|
||||
userID,
|
||||
username: targetUsername
|
||||
},
|
||||
issuer: {
|
||||
userID: issuerUserID,
|
||||
username: issuerUsername
|
||||
},
|
||||
reason
|
||||
} as warningData;
|
||||
|
||||
try {
|
||||
const warning = generateWarningDiscord(webhookData);
|
||||
dispatchEvent("warning", warning);
|
||||
} catch /* istanbul ignore next */ (err) {
|
||||
Logger.error(`Error sending warning to Discord ${err}`);
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
message: `Warning ${resultStatus} user '${userID}'.`,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user