-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreporter.py
304 lines (259 loc) · 8.18 KB
/
reporter.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import discord
from discord.ext import commands, tasks
from gatherer import Channel_gatherer
from captcha_handler import Captcha_handler
import io
import time
import json
import threading
import asyncio
import nest_asyncio
from queue import Queue
import sys
from os.path import exists
nest_asyncio.apply()
PATH = 'names.list'
REPORT_CHANNEL_ID = 976504870251155455 #change this
with open('self.token','r') as file:
self_token = file.read().strip()
def format_msg(channels):
msg = '['
for i in range(len(channels)):
my_dict = {'name':channels[i][0],'topic':channels[i][1],'visible':channels[i][2]}
msg += json.dumps(my_dict,indent=4)
if i != len(channels) - 1:
msg += ',\n'
msg += ']'
return msg
def format_list(names):
msg = '\n**Guilds tracking list**:\n```diff\n'
for i in range(len(names)):
msg += f'+ {i} - {names[i]}\n'
msg += '```'
return msg
def format_file(channels,name):
msg = format_msg(channels)
msg_b = bytes(msg,'utf-8')
file = io.BytesIO(msg_b)
file = discord.File(file,filename=name+'.json')
return file
def save_names(names):
with open(PATH,'w') as file:
for name in names:
file.write(name+'\n')
def save_blacklist(channel_blacklist):
with open('blacklist.txt','w') as file:
for channel_id in channel_blacklist:
file.write(str(channel_id)+'\n')
class Channel_Reporter(commands.Cog):
running = True
channel = None
bot = None
events = {}
threads = []
names = []
refresh_ctx = None
channel_blacklist = []
def __init__(self ,bot : commands.bot.Bot ,intents: discord.Intents, application_id: int):
self.bot = bot
@tasks.loop(seconds=0.25)
async def on_change_loop(self):
'''
Listening for events on the gatherer thread
'''
event = self.on_change[0]
queue = self.on_change[1]
for thread in self.threads:
if not thread.is_alive():
self.threads.remove(thread)
await self.init_gatherer()
if not queue.empty():
package = await queue.get()
channel = package[0]
kind = package[1]
await self.handle_changes(channel,kind)
#event.clear()
def start_thread(self, events : list ,names : list):
async def run():
captcha_handler = Captcha_handler()
client = Channel_gatherer(captcha_handler = captcha_handler)
client.events = events
client.names = names
client.on_change = self.on_change
client.proxy = 'de.proxiware.com:29549'
client.run(self_token)
asyncio.run(run())
async def init_gatherer(self):
#start thread
print('-- Starting gatherering threads --')
thread = threading.Thread(target=self.start_thread, args=(self.events,self.names,), daemon=True)
thread.start()
self.threads.append(thread)
@commands.Cog.listener()
async def on_ready(self):
'''
After login init proccess. load names.guilds,channels and start thread(s)
'''
print(self.bot.guilds)
if not len(self.threads): # check if a desconect happened (on_ready is called again)
print('-- Bot initiated --')
self.load_names()
self.on_change = [threading.Event(),asyncio.Queue()]#for on_change communication
for command in self.get_commands(): #dic of command : [event queue]
self.events[command.name] = [threading.Event(),Queue()]
if command.name == 'refresh':
self.refresh_ctx = command#get command
self.channel = self.bot.get_channel(REPORT_CHANNEL_ID)
try:
await self.init_gatherer()#start thread
except Exception as e:
print(e)
self.bot.close()
self.load_blacklist()
await self.load_guilds()
await self.send_msg(f'Bot initiated!')
self.on_change_loop.start()
async def handle_changes(self,channel,kind):
'''
Formats and sends channel changes as json to channel
'''
await self.load_guilds()
for guild in self.guilds_temp:
if channel.guild.name == guild.name:
if channel.id not in self.channel_blacklist:#blacklist check
hidden = channel.permissions_for(channel.guild.me).view_channel
channel_name = channel.name
channel_type = channel.type.name
channel_topic = ''
if channel_type == 'text' or channel_type == 'forum' or channel_type == 'stage':
channel_topic = channel.topic
channel_temp = [[channel_name,channel_topic,hidden]]
file = format_file(channel_temp,channel.guild.name+f'-{kind}')
await self.send_msg(f'Channel (**{channel.id}**) **{kind}d** on **__{guild.name}__**!"',file)
async def send_msg(self,msg,file=None):
await self.channel.send(msg,file = file)
def load_blacklist(self):
self.channel_blacklist = []
if not exists('blacklist.txt'):
with open('blacklist.txt','w') as file:
pass
with open('blacklist.txt','r') as file:
for channel_id in file:
channel_id = channel_id.split('\n')[0]
if len(channel_id):
self.channel_blacklist.append(int(channel_id.strip()))
def load_names(self):
self.names = []
if not exists(PATH):#create file
with open(PATH,'w') as file:
pass
with open(PATH,'r') as file:
for name in file:
name = name.split('\n')[0]
if len(name):
self.names.append(name.strip())
async def load_guilds(self):
command_name = 'refresh'
queue,event = self.gather_com(command_name)
guilds = queue.get()
self.guilds_temp = guilds
def gather_com(self,command_name):
event = self.events[command_name][0]
queue = self.events[command_name][1]
event.set()#send event to thread execute command
return queue,event
@commands.command()
async def ban(self,ctx):
'''
>>ban [channel_id]
# Blacklists a channel
'''
channel_id = int(ctx.message.content[5:].strip())
if channel_id not in self.channel_blacklist:
self.channel_blacklist.append(channel_id)
save_blacklist(self.channel_blacklist)
await self.send_msg(f'>**{channel_id}** blacklisted!')
print(f'{channel_id} blacklisted')
else:
await self.send_msg(f'>**{channel_id}** already blacklisted!')
@commands.command()
async def unban(self,ctx):
'''
>>unban [channel_id]
# Unblacklists a channel
'''
channel_id = int(ctx.message.content[7:].strip())
if channel_id in self.channel_blacklist:
self.channel_blacklist.remove(channel_id)
save_blacklist(self.channel_blacklist)
await self.send_msg(f'>**{channel_id}** unblacklisted!')
print(f'{channel_id} unblacklisted')
else:
await self.send_msg(f'>**{channel_id}** is not blacklisted!')
@commands.command()
async def refresh(self,ctx):
'''
>>refresh
# Returns .txt files with all channels informations from all guilds on track list
'''
command_name = ctx.command.name
queue,event = self.gather_com(command_name)
guilds = queue.get()
self.guilds_temp = guilds
for guild in guilds:
file = format_file(guild.channels,guild.name)
await self.send_msg(f'Full **__{guild.name}__** channel list :',file)
@commands.command()
async def add(self,ctx):
'''
>>add [invite link/guild name]
# Adds a guild to track list
# (Use invite link if self-bot is not already member of guild)
'''
command_name = ctx.command.name
queue,event = self.gather_com(command_name)
temp_str = ctx.message.content[5:].strip()
queue.put(temp_str)
while event.is_set():#wait gatherer finish refreshing
pass
temp_name = queue.get()
print(temp_name,self.names)
if not len(temp_name):
await self.send_msg(f'> __**{temp_str}**__ not found!')
elif temp_name in self.names:
await self.send_msg(f'> __**{temp_name}**__ is already on track list!')
else:
self.names.append(temp_name)
save_names(self.names)
await self.send_msg(f'> __**{temp_name}**__ added!')
self.load_names()
await self.load_guilds()
@commands.command(name='del')
async def dell(self,ctx):
'''
>>del [guild name]
# Deletes a guild from the track list
'''
temp_name = ctx.message.content[5:].strip()
command_name = ctx.command.name
queue,event = self.gather_com(command_name)
queue.put(temp_name)
if temp_name in self.names:
self.names.remove(temp_name)
save_names(self.names)
await self.channel.send(f'> __**{temp_name}**__ deleted!')
else:
await self.channel.send(f'> __**{temp_name}**__ not found!')
self.load_names()
@commands.command()
async def list(self,ctx):
'''
>>list
# Lists track list
'''
self.load_names()
msg = format_list(self.names)
await self.channel.send(msg)
@commands.Cog.listener()
async def on_command_error(self,ctx,error):
pass