Skip to content

Instantly share code, notes, and snippets.

@nakkaa
Last active August 3, 2024 05:24
Show Gist options
  • Save nakkaa/b304e9319bf1b60d2af574f32b40bc1a to your computer and use it in GitHub Desktop.
Save nakkaa/b304e9319bf1b60d2af574f32b40bc1a to your computer and use it in GitHub Desktop.
Misskeyの通報/新規登録/管理者へのリプライをDiscordに通知飛ばすやつ
export default {
async fetch(request, env) {
if (request.headers.get('X-Misskey-Hook-Secret') !== env.SECRET) {
console.log('wrong secret');
return new Response('wrong secret');
}
const reqBody = await request.text();
if (!reqBody) {
console.log('no body');
return new Response('no body');
}
const body = JSON.parse(reqBody);
//console.log(body)
if (body.type === 'userCreated'){
const server = body.server;
const name = body.body.username;
const ok = await fetch(env.DISCORD, {
body: JSON.stringify({
content: `新規ユーザー登録がありました。\nserver: <${server}>\nname: ${name}\n`,
}),
method: 'POST',
headers: { 'Content-Type': 'application/json' },
}).then((res) => res.ok);
return new Response(ok ? 'ok' : 'error');
}
if (body.type === 'abuseReport'){
const server = body.server;
const name = body.body.targetUserId;
const text = body.body.comment;
const ok = await fetch(env.DISCORD, {
body: JSON.stringify({
content: `通報がありました。\nserver: <${server}>\nname: ${name}\ntext: ${text}\n`,
}),
method: 'POST',
headers: { 'Content-Type': 'application/json' },
}).then((res) => res.ok);
return new Response(ok ? 'ok' : 'error');
}
if (body.type === 'mention' || body.type === 'reply') {
const server = body.server;
const name = body.body.note.user.name;
const text = body.body.note.text;
const ok = await fetch(env.DISCORD, {
body: JSON.stringify({
content: `管理者にメンションがありました。\nserver: <${server}>\nname: ${name}\ntext: ${text}\n`,
}),
method: 'POST',
headers: { 'Content-Type': 'application/json' },
}).then((res) => res.ok);
return new Response(ok ? 'ok' : 'error');
}
return new Response('error');
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment