Skip to content

Commit

Permalink
조건에 맞는 스팸 리포트 자동 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-maki committed Feb 17, 2024
1 parent a3171f0 commit 7092459
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRestAPIClient, createStreamingAPIClient } from 'masto';
import { createRestAPIClient, createStreamingAPIClient, mastodon } from 'masto';
import dayjs from 'dayjs';

const rest = createRestAPIClient({
Expand All @@ -11,18 +11,40 @@ const streaming = createStreamingAPIClient({
accessToken: process.env.MASTODON_ACCESS_TOKEN,
});

for await(const event of streaming.public.subscribe()) {
if(event.event === 'update' && !event.payload.inReplyToId) {
if(event.payload.account.acct.includes('@') && event.payload.account.username === event.payload.account.displayName) {
if(event.payload.mentions.length >= 1 && dayjs(event.payload.account.createdAt).isSame(dayjs(), 'day')) {
if(/^[a-z0-9]{10}$/.test(event.payload.account.username)) {
console.log(`${event.payload.account.acct} is a bot`);
rest.v1.admin.accounts.$select(event.payload.account.id).action.create({
type: 'suspend',
text: 'Mention Bot (Auto)'
})
}
}
function checkIsSpam(status: mastodon.v1.Status) {
//console.log(status.inReplyToId, status.account.acct, status.account.username, status.account.displayName, status.mentions.length, dayjs(status.account.createdAt).isSame(dayjs(), 'day'), /^[a-z0-9]{10}$/.test(status.account.username));
return status.inReplyToId === null &&
status.account.acct.includes('@') &&
(status.account.username === status.account.displayName || status.account.displayName.length === 0) &&
status.mentions.length >= 1 &&
dayjs(status.account.createdAt).isSame(dayjs(), 'day') &&
/^[a-z0-9]{10}$/.test(status.account.username);
}

setInterval(async () => {
const reports = await rest.v1.admin.reports.list({});
for(const report of reports) {
if(report.category === 'spam' && report.statuses.some((status) => checkIsSpam(status))) {
console.log(`Reported ${report.targetAccount.acct} is a bot`);
rest.v1.admin.accounts.$select(report.targetAccount.id).action.create({
type: 'suspend',
text: 'Mention Bot (Auto)',
reportId: report.id,
})
}
else if(report.category === 'spam') {
console.log(`Reported ${report.targetAccount.acct} is not a bot`);
console.log(JSON.stringify(report));
}
}
}
}, 10_000);

// for await(const event of streaming.public.subscribe()) {
// if(event.event === 'update' && checkIsSpam(event.payload)) {
// console.log(`${event.payload.account.acct} is a bot`);
// rest.v1.admin.accounts.$select(event.payload.account.id).action.create({
// type: 'suspend',
// text: 'Mention Bot (Auto)'
// })
// }
// }

0 comments on commit 7092459

Please sign in to comment.