-
Notifications
You must be signed in to change notification settings - Fork 0
/
ban.js
105 lines (87 loc) · 2.8 KB
/
ban.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const fs = require('fs')
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const tmi = require('tmi.js')
const { StaticAuthProvider } = require('@twurple/auth')
const { ApiClient } = require('@twurple/api')
const config = require('./config.json')
const { now } = require('./utils')
const [year, month, day, hour, minutes] = now()
const validFile = `banlist_${year}${month}${day}_${hour}${minutes}.txt`
const argv = yargs(hideBin(process.argv))
.alias('f','file')
.nargs('f', 1)
.describe('f', 'Load a file')
.alias('i','index')
.nargs('i', 1)
.describe('i', 'Starting user index')
.alias('w','write')
.nargs('w', 0)
.describe('w', 'Write banned users in a separate file')
.demandOption(['f'])
.help()
.alias('help','h')
.alias('version','v')
.argv
if(!argv.file) {
console.error('ERROR - no file selected')
process.exit(1)
}
const file = fs.readFileSync(argv.file, 'utf-8').split('\n')
const { channels, username, client_id, access_token } = config
const authProvider = new StaticAuthProvider(client_id, access_token)
const main = async () => {
const client = new ApiClient({ authProvider })
const moderator = await client.users.getUserByName(username)
console.log('users to ban: %s',file.length)
let i = argv.index || 0
while(file[i]) {
try {
[usernameToBan,reason] = file[i].split(',')
const userToBan = await client.users.getUserByName(usernameToBan)
if(userToBan === null) {
console.log(`${usernameToBan} not found`)
continue
}
console.log('--------------------')
console.log(i,`- user: ${usernameToBan}, ${userToBan.id}`)
console.log('--------------------')
let j = 0
let isValid = false
while(channels[j]) {
try {
const broadcaster = await client.users.getUserByName(channels[j])
const ban = await client.moderation.banUser(
broadcaster,
moderator,
{
duration: null,
user: userToBan,
reason,
}
)
isValid = true
console.log(`${channels[j]} - ✅`)
} catch ({ _body, ...request }) {
console.log({ request })
const { message } = JSON.parse(_body)
console.log(`${channels[j]} - ${message}`)
if( message.includes('already banned') ) {
isValid = true
}
} finally {
j += 1
}
}
if(isValid && argv.write) {
fs.appendFileSync(validFile,`${user},${reason}\n`)
}
} catch (error) {
console.error(error)
} finally {
i += 1
}
}
process.exit(0)
}
main()