Skip to content

Commit

Permalink
Merge pull request #1 from t1nyb0x/develop
Browse files Browse the repository at this point in the history
Webhook通知処理追加
  • Loading branch information
t1nyb0x authored Oct 20, 2024
2 parents dd4d86d + dd3081c commit 5bdd6c6
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 36 deletions.
44 changes: 10 additions & 34 deletions src/abuseReport.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
import { getUserName } from './utils/getUserName';

export default async function abuseReport(body: any, webhookUrl: string) {
const server = body.server;
const targetUserId = body.body.targetUserId;
const text = body.body.comment;
const reporterUserId = body.body.reporterId;

const targetUserName = await getUsername(server, targetUserId);
const reporterUserName = await getUsername(server, reporterUserId);
const targetUserName = await getUserName(server, targetUserId);
const reporterUserName = await getUserName(server, reporterUserId);

const isOk = await fetch(webhookUrl, {
body: JSON.stringify({
embeds: [
{
title: '通報がありました',
title: '通報がありました',
color: 15409955,
description: `通報がありました。\n### 通報内容\n ${text}\n### 通報があったサーバー\n${server}`,
fields: [
{
name: '通報があったサーバー',
value: `${server}`,
},
{
name: '通報されたユーザー',
value: `${targetUserName}(${targetUserId})`,
},
{
name: '通報内容',
value: `${text}`,
value: `[${targetUserName}](${server}/users/${targetUserId})`,
inline: true,
},
{
name: '通報を行ったユーザー',
value: `${reporterUserName}(${reporterUserId})`,
value: `[${reporterUserName}](${server}/users/${reporterUserId})`,
inline: true,
},
],
},
Expand All @@ -40,24 +37,3 @@ export default async function abuseReport(body: any, webhookUrl: string) {

return isOk;
}

async function getUsername(server: string, userId: string): Promise<string> {
// set body
const requestBodySchema = {
userId: userId,
};

const init = {
body: JSON.stringify(requestBodySchema),
method: 'POST',
headers: {
'content-type': 'application/json;charset=UTF-8',
},
};

const response = await fetch(server + '/api/users/show', init);
const userData = await response.text();
const userName = JSON.parse(userData).username;

return userName;
}
40 changes: 40 additions & 0 deletions src/abuseReportResolved.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { getUserName } from './utils/getUserName';

export default async function abuseReportResolved(body: any, webhookUrl: string) {
const targetUserName = await getUserName(body.server, body.body.targetUserId);
const reporterUserName = await getUserName(body.server, body.body.reporterId);
const asigneeUserName = await getUserName(body.server, body.body.assigneeId);

const isOk = await fetch(webhookUrl, {
body: JSON.stringify({
embeds: [
{
title: '通報を解決しました',
color: 3359727,
description: `通報が解決されました。\n### 通報内容\n ${body.body.comment}\n### 通報があったサーバー\n${body.server}`,
fields: [
{
name: '通報されたユーザー',
value: `[${targetUserName}](${body.server}/users/${body.body.targetUserId})`,
inline: true,
},
{
name: '通報を行ったユーザー',
value: `[${reporterUserName}](${body.server}/users/${body.body.reporterId})`,
inline: true,
},
{
name: '通報を解決したユーザー',
value: `[${asigneeUserName}](${body.server}/users/${body.body.assigneeId})`,
inline: true,
},
],
},
],
}),
method: 'POST',
headers: { 'Content-Type': 'application/json' },
}).then((res) => res.ok);

return isOk;
}
17 changes: 17 additions & 0 deletions src/inactiveModeratorsInvitationOnlyChanged.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default async function inactiveModeratorsInvitationOnlyChanged(body: any, webhookUrl: string) {
const isOk = await fetch(webhookUrl, {
body: JSON.stringify({
embeds: [
{
title: '新規登録が招待制に移行されました',
color: 15409955,
description: `### 注意\nモデレーターのアクティブが一定期間なかったため、新規登録が招待制に移行しました。\n新規登録を開放したい場合は管理設定から変更してください。\n### 対象サーバー\n${body.server}`,
},
],
}),
method: 'POST',
headers: { 'Content-Type': 'application/json' },
}).then((res) => res.ok);

return isOk;
}
17 changes: 17 additions & 0 deletions src/inactiveModeratorsWarning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default async function inactiveModeratorsWarning(body: any, webhookUrl: string) {
const isOk = await fetch(webhookUrl, {
body: JSON.stringify({
embeds: [
{
title: 'まもなく新規登録が招待制へ移行します',
color: 14931798,
description: `### 注意\nモデレーターのアクティブが一定期間なかったため、まもなく新規登録が招待制へ移行します。\nモデレーターがログインすることで、この自動処理は中止されます。\n### 対象サーバー\n${body.server}`,
},
],
}),
method: 'POST',
headers: { 'Content-Type': 'application/json' },
}).then((res) => res.ok);

return isOk;
}
20 changes: 19 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
* Learn more at https://developers.cloudflare.com/workers/
*/
import abuseReport from './abuseReport';
import abuseReportResolved from './abuseReportResolved';
import inactiveModeratorsInvitationOnlyChanged from './inactiveModeratorsInvitationOnlyChanged';
import inactiveModeratorsWarning from './inactiveModeratorsWarning';
import mention from './mention';
import userCreated from './userCreated';

Expand All @@ -32,7 +35,7 @@ export default {

const body = JSON.parse(reqBody);

//console.log(body)
// console.log(body);

if (body.type === 'userCreated') {
const isOk = await userCreated(body, env.DISCORD);
Expand All @@ -44,6 +47,21 @@ export default {
return new Response(isOk ? 'ok' : 'error');
}

if (body.type === 'abuseReportResolved') {
const isOk = await abuseReportResolved(body, env.DISCORD);
return new Response(isOk ? 'ok' : 'error');
}

if (body.type === 'inactiveModeratorsWarning') {
const isOk = await inactiveModeratorsWarning(body, env.DISCORD);
return new Response(isOk ? 'ok' : 'error');
}

if (body.type === 'inactiveModeratorsInvitationOnlyChanged') {
const isOk = await inactiveModeratorsInvitationOnlyChanged(body, env.DISCORD);
return new Response(isOk ? 'ok' : 'error');
}

if (body.type === 'mention' || body.type === 'reply') {
const isOk = await mention(body, env.DISCORD);
return new Response(isOk ? 'ok' : 'error');
Expand Down
2 changes: 1 addition & 1 deletion src/userCreated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function userCreated(body: any, webhookUrl: string) {
},
{
name: 'ユーザー名',
value: `${name}`,
value: `[${name}](${server}/users/${body.body.id})`,
},
],
},
Expand Down
20 changes: 20 additions & 0 deletions src/utils/getUserName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export async function getUserName(server: string, userId: string): Promise<string> {
// set body
const requestBodySchema = {
userId: userId,
};

const init = {
body: JSON.stringify(requestBodySchema),
method: 'POST',
headers: {
'content-type': 'application/json;charset=UTF-8',
},
};

const response = await fetch(server + '/api/users/show', init);
const userData = await response.text();
const userName = JSON.parse(userData).username;

return userName;
}

0 comments on commit 5bdd6c6

Please sign in to comment.