-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChatHelper.py
717 lines (496 loc) · 24.3 KB
/
ChatHelper.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
from discord.ext import commands, tasks
import discord.utils
import discord
import Info
import re
import pandas as pd
from discord.utils import get
import requests
import CoinInfo
import json
import asyncio
import re
import textdistance
import numpy as np
from matplotlib import pyplot as plt
import cv2 as cv
import urllib
from skimage.metrics import structural_similarity as ssim
from scipy.spatial.distance import euclidean
import requests
import time
import unidecode
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import io
from DataBase import DataBaseManagement
class ChatCommands(commands.Cog):
def __init__(self,bot):
self.bot = bot
self.info = Info.ServerInformation("Data Files/server_info.json")
with open("Data Files/wen.json") as f:
self.wen = json.load(f)
self.wen_lock = asyncio.Lock()
self.counter = {}
self.counter_locks = {}
for channel in self.info.server_info["scam_channels"]:
self.counter[channel] = 0
self.counter_locks[channel] = asyncio.Lock()
self.board_lock = asyncio.Lock()
self.white_list_lock = asyncio.Lock()
client = self.info.server_info["twitch_client"]
sec = self.info.server_info["twitch_secret"]
response = requests.post(f"https://id.twitch.tv/oauth2/token"
f"?client_id={client}"
f"&client_secret={sec}"
"&grant_type=client_credentials")
if response.status_code == 200:
self.bearer_token = response.json()['access_token']
self.headers = { "client-id": self.info.server_info["twitch_client"],
"Authorization": "Bearer "+self.bearer_token
}
self.notify_channels = self.info.server_info["twtich_watchlist"]
self.notify_times = {}
self.notify_times_lock = {}
for i in self.notify_channels:
self.notify_times[i] = {}
self.notify_times[i]["time-last-online"] = np.inf
self.notify_times[i]["already-notified"] = False
#self.notify_times_lock[i] = asyncio.Lock()
@tasks.loop(seconds=60,reconnect=True)
async def notify_twitch_live(self):
await self.bot.wait_until_ready()
notify_channel = await self.bot.fetch_channel(844468484036493352)
for i in self.notify_channels:
live = self.is_live(i)
if live:
self.notify_times[i]["time-last-online"] = int(time.time())
if not self.notify_times[i]["already-notified"]:
await notify_channel.send("{} is streaming on https://twitch.tv/{}".format(i,i))
self.notify_times[i]["already-notified"] = True
else:
if int(time.time()) - self.notify_times[i]["time-last-online"] > 600:
self.notify_times[i]["already-notified"] = False
def is_live(self,channel):
if self.bearer_token == None:
raise ValueError("Bearer Token has not been set")
end_point = "https://api.twitch.tv/helix/search/channels?query={}".format(channel)
r = requests.get(end_point, headers=self.headers)
if r.status_code == 200:
return r.json()["data"][0]["is_live"]
else:
raise ValueError("Bearer token was not authenicated by twitch")
@commands.command()
async def wen(self,ctx):
await self.bot.wait_until_ready()
await self.wen_lock.acquire()
self.wen["wen"] += 1
with open('Data Files/wen.json', 'w') as f:
json.dump(self.wen,f,indent=4)
await ctx.channel.send("Wen Count Is : {}".format(self.wen["wen"]))
self.wen_lock.release()
@commands.command()
async def whitelist(self,ctx,member):
await self.bot.wait_until_ready()
if(not self.is_staff_member(ctx.author)):
await ctx.channel.send("Only staff members can use this command.")
return
try:
member_string = member
user_obj = await ctx.guild.fetch_member(int(member_string))
await self.white_list_lock.acquire()
with open('Data Files/whitelist.json', 'r') as f:
white_list = json.load(f)
white_list["whitelist"].append(int(user_obj.id))
with open('Data Files/whitelist.json', 'w') as f:
json.dump(white_list,f,indent=4)
await ctx.channel.send("{} Added to the whiltelist".format(user_obj.mention))
except:
await ctx.channel.send("Invalid User Passed. Example usage is $whitelist userid")
print("Invalid User passed.")
return
self.white_list_lock.release()
@commands.command()
async def chartex(self,ctx):
await self.bot.wait_until_ready()
self.info.update()
message = self.info.server_info["chartex"]
await ctx.send(message)
@commands.command()
async def twitter(self,ctx):
await self.bot.wait_until_ready()
self.info.update()
message = self.info.server_info["twitter"]
await ctx.send(message)
@commands.command()
async def contract(self,ctx):
await self.bot.wait_until_ready()
self.info.update()
message = self.info.server_info["contract"]
await ctx.send(message)
@commands.command()
async def website(self,ctx):
await self.bot.wait_until_ready()
self.info.update()
message = self.info.server_info["website"]
await ctx.send(message)
@commands.command()
async def leash_chart(self,ctx):
await self.bot.wait_until_ready()
self.info.update()
message = self.info.server_info["leash_chart"]
await ctx.send(message)
@commands.command()
async def leash_contract(self,ctx):
await self.bot.wait_until_ready()
self.info.update()
message = self.info.server_info["leash_contract"]
await ctx.send(message)
@commands.command()
async def market_cap(self,ctx):
await self.bot.wait_until_ready()
await ctx.send("This Command has been removed. Look at the online users to see marketcap")
'''
self.info.update()
circ_supply = 1e15
try:
value = CoinInfo.get_shib_price_usd()
await ctx.send("Current Shib Marketcap is ${}".format(f"{round(value*circ_supply):,}" ))
except:
print('Error getting price data')
'''
@commands.command()
async def reddit(self,ctx):
await self.bot.wait_until_ready()
self.info.update()
message = self.info.server_info["reddit"]
await ctx.send(message)
@commands.command()
async def gas(self,ctx):
await self.bot.wait_until_ready()
self.info.update()
try:
gas =CoinInfo.CoinQuery().get_gas().split()
message = "Fastest: {} Fast: {} Normal: {} ".format(gas[0],gas[1],gas[2])
except:
message = "Cannot get prices from https://ethgasstation.info/"
await ctx.send(message)
def get_json(self,enpoint,request_type = "get",params = None):
r = requests.Session()
if(request_type == "get"):
p = r.get(enpoint,json=params)
else:
p = r.post(enpoint,json=params)
return p.json()
@commands.command()
async def leash_market_cap(self,ctx):
await self.bot.wait_until_ready()
URL = "https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2"
params = {
"query" : "{ pair(id: \"0x874376be8231dad99aabf9ef0767b3cc054c60ee\"){ \n token1Price }}"
}
ETH_PARAM = {
"query": "{ pair(id: \"0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc\"){ token0Price }}"
}
circ_supply = 107646.849
try:
#json = CoinInfo.get_holders()
#message = "Current Holders: "+str(json)
price_in_eth = float(self.get_json(URL,request_type="post",params=params)["data"]["pair"]["token1Price"])
eth_price = float(self.get_json(URL,request_type="post",params=ETH_PARAM)["data"]["pair"]["token0Price"])
message = "This current leash Marketcap is ${}".format(f"{round(eth_price*circ_supply*eth_price):,}" )
except:
message = "Cannot get holders, likely api limitation by ethplorer.io"
await ctx.send(message)
@commands.command()
async def leash_price_bot(self,ctx):
await self.bot.wait_until_ready()
message = "Leash Price Tracker discord link - " + self.info.server_info["leash_price_link"]
await ctx.send(message)
@commands.command()
async def bone_price_bot(self,ctx):
await self.bot.wait_until_ready()
message = "Bone Price Tracker discord link - " + self.info.server_info["bone_price_link"]
await ctx.send(message)
@commands.command()
async def leash_holder_bot(self,ctx):
await self.bot.wait_until_ready()
message = "Leash Holder Tracker discord link - " + self.info.server_info["leash_holder_link"]
await ctx.send(message)
@commands.command()
async def shib_pricer_bot(self,ctx):
await self.bot.wait_until_ready()
message = "Shib Price Tracker discord link - " + self.info.server_info["shib_pricer_bot_link"]
await ctx.send(message)
@commands.command()
async def gas_tracker_bot(self,ctx):
await self.bot.wait_until_ready()
message = "Gas Tracker discord link - " + self.info.server_info["gas_price_link"]
await ctx.send(message)
@commands.command()
async def shib_holder_bot(self,ctx):
await self.bot.wait_until_ready()
message = "Shib Holder Tracker bot discord link - " + self.info.server_info["shid_holder_bot"]
await ctx.send(message)
@commands.command()
async def github(self,ctx):
await self.bot.wait_until_ready()
message = "https://github.com/coleman2246/Shiba-Bot"
await ctx.send(message)
@commands.command()
async def market_crab(self,ctx):
await self.bot.wait_until_ready()
message = "<:crabbe:834946109809098753>"
await ctx.send(message)
@commands.command()
async def board(self,ctx):
if(not self.is_staff_member(ctx.author)):
await ctx.channel.send("Only staff members can use this command.")
return
await self.board_lock.acquire()
with open('Data Files/data.json', 'r') as f:
channel_data = json.load(f)
self.board_lock.release()
await self.send_board_embed(ctx,channel_data)
async def send_board_embed(self,ctx,channel_data):
embed_dict = {}
embed_dict["title"] = "Shib Staff Board"
embed_dict["fields"] = []
for curr_channel in channel_data.keys():
if(len(channel_data[curr_channel]) != 0):
chan_obj = await self.bot.fetch_channel(curr_channel)
temp = {}
temp["name"] = "**"+chan_obj.name+"**"
temp["value"] = ""
for member in channel_data[curr_channel]:
member_name = await ctx.guild.fetch_member(member)
temp["value"] += " **"+member_name.mention+"** "
embed_dict["fields"].append(temp)
await ctx.channel.send("",embed=discord.Embed.from_dict(embed_dict))
async def assign_user(self,ctx,channel,user):
await self.board_lock.acquire()
with open('Data Files/data.json', 'r') as f:
channel_data = json.load(f)
try:
channel = channel[2:-1]
channel = await self.bot.fetch_channel(channel)
if(int(user.id) in channel_data[str(channel.id)] ):
channel_data[str(channel.id)].remove(int(user.id))
await ctx.channel.send("User Removed from Channel")
else:
await ctx.channel.send("User Added to Channel")
channel_data[str(channel.id)].append(user.id)
except:
await ctx.channel.send("Please Enter Valid Channel")
self.board_lock.release()
return
await self.send_board_embed(ctx,channel_data)
with open('Data Files/data.json', 'w') as f:
json.dump(channel_data,f,indent=4)
self.board_lock.release()
@tasks.loop(seconds=60*10,reconnect=True)
async def imposter(self):
await self.bot.wait_until_ready()
notify_channel = await self.bot.fetch_channel(844468484036493352)
guild = self.bot.get_guild(740287152843128944)
members = await guild.chunk()
print(len(members),guild.member_count,guild.name)
staff_members = []
for role_id in self.info.server_info["staff_roles"]:
curr_role = guild.get_role(role_id)
staff_members += curr_role.members
staff_names = []
staff_pics = {}
print("Getting Staff Images")
for i in staff_members:
if i.nick == None:
staff_names.append(i.name)
else:
staff_names.append(i.nick)
img = await i.avatar_url_as(static_format="jpg",size=32).read()
stream = io.BytesIO(img)
img = Image.open(stream).convert('RGB')
#img.show(command='fim')
open_cv_image = np.array(img)
open_cv_image = open_cv_image[:, :, ::-1].copy()
gray = cv.cvtColor(open_cv_image,cv.COLOR_BGR2GRAY)
#kp, des = sift.detectAndCompute(gray,None)
staff_pics[i.id] = gray
print("Started loop")
similar_names = []
banned_words = ["admin","mod","support"]
await self.white_list_lock.acquire()
with open('Data Files/whitelist.json', 'r') as f:
white_list = json.load(f)["whitelist"]
self.white_list_lock.release()
for memeber_depth,member in enumerate(members):
effective_name = unidecode.unidecode(member.name)
if member.id not in white_list:
status = member.activity
if(status != None and isinstance(status,discord.CustomActivity) and status.name != None ):
for word in banned_words:
if word.upper() in status.name.upper():
await notify_channel.send("<@&805576038486769705> Forbidden Status Words: {} Member: {} Status: {}".format(word,member.mention,status.name))
for i,staff in enumerate(staff_members):
if staff.id != member.id:
staff_start = staff_names[i].split()[0]
if(staff_start.upper()[:3] in effective_name.upper()):
distance = textdistance.levenshtein(staff_names[i].upper(),effective_name.upper())
if(distance < 8):
member_pic = await member.avatar_url_as(static_format="jpg",size=32).read()
stream = io.BytesIO(member_pic)
img = Image.open(stream).convert('RGB')
#img.show(command='fim')
open_cv_image = np.array(img)
open_cv_image = open_cv_image[:, :, ::-1].copy()
gray = cv.cvtColor(open_cv_image,cv.COLOR_BGR2GRAY)
dim = (32, 32)
gray = cv.resize(gray,dim,interpolation = cv.INTER_AREA)
s = ssim(gray,staff_pics[staff.id])
#print(("Staff: {} Member: {} Name Distance: {} Picture Similairty: {}".format(staff_names[i],member.mention,distance,s)))
if(s > .25 or (distance < 3 and s > .15) ):
await notify_channel.send("<@&805576038486769705> Staff: {} Member: {} Name Distance: {} Picture Similairty: {}".format(staff_names[i],member.mention,distance,s))
print("Done Loop")
@commands.command()
async def assign(self,ctx,channel,author=None):
await self.bot.wait_until_ready()
if(not self.is_staff_member(ctx.author)):
await ctx.channel.send("Only staff members can use this command.")
return
if(author == None):
await self.assign_user(ctx,channel,ctx.author)
else:
try:
auth_string = author[3:-1]
print(auth_string)
user_obj = await ctx.guild.fetch_member(int(auth_string))
except:
await ctx.channel.send("Invalid User Passed. Example usage is $assign #channel @user")
print("Invalid User passed.")
return
await self.assign_user(ctx,channel,user_obj)
@commands.command()
async def members(self,ctx):
shib_server = self.bot.get_guild(740287152843128944)
await ctx.channel.send("There is Currently **{}** members in {}".format(shib_server.member_count,shib_server.name))
@commands.Cog.listener()
async def on_member_join(self,member):
await self.bot.wait_until_ready()
with open("Data Files/join_message") as f:
data = f.read()
await member.send(data)
staff_members = []
staff_pics = {}
shib_server = self.bot.get_guild(740287152843128944)
for role_id in self.info.server_info["staff_roles"]:
curr_role = shib_server.get_role(role_id)
staff_members += curr_role.members
for i in staff_members:
img = await i.avatar_url_as(static_format="jpg",size=32).read()
stream = io.BytesIO(img)
img = Image.open(stream).convert('RGB')
open_cv_image = np.array(img)
open_cv_image = open_cv_image[:, :, ::-1].copy()
gray = cv.cvtColor(open_cv_image,cv.COLOR_BGR2GRAY)
staff_pics[i.id] = gray
staff_names = []
for i in staff_members:
if i.nick == None:
staff_names.append(i.name)
else:
staff_names.append(i.nick)
effective_name = unidecode.unidecode(member.name)
for i,staff in enumerate(staff_members):
if staff.id != member.id:
staff_start = staff_names[i].split()[0]
if(staff_start.upper()[:3] in effective_name.upper()):
distance = textdistance.levenshtein(staff_names[i].upper(),effective_name.upper())
if(distance < 5):
member_pic = await member.avatar_url_as(static_format="jpg",size=32).read()
stream = io.BytesIO(member_pic)
img = Image.open(stream).convert('RGB')
#img.show(command='fim')
open_cv_image = np.array(img)
open_cv_image = open_cv_image[:, :, ::-1].copy()
gray = cv.cvtColor(open_cv_image,cv.COLOR_BGR2GRAY)
dim = (32, 32)
gray = cv.resize(gray,dim,interpolation = cv.INTER_AREA)
s = ssim(gray,staff_pics[staff.id])
#print(("Staff: {} Member: {} Name Distance: {} Picture Similairty: {}".format(staff_names[i],member.mention,distance,s)))
if(s > .25 or distance < 3):
await member.send("You have been kicked due to your name being too similar to a staff memebers")
#await ctx.channel.send("Staff: {} Member: {} Name Distance: {} Picture Similairty: {}".format(staff_names[i],member.mention,distance,s))
break
@commands.Cog.listener()
async def on_message(self, ctx):
await self.bot.wait_until_ready()
if(ctx.channel.id in self.info.server_info["scam_channels"] and int(ctx.author.id) != 842892223162089503):
await self.counter_locks[ctx.channel.id].acquire()
count = self.counter[ctx.channel.id]
if( count >= self.info.server_info["scam_channels_counts"][str(ctx.channel.id)] ):
#print(self.info.server_info["scam_channels_counts"][str(ctx.channel.id)])
if "french".upper() in ctx.channel.name.upper():
name = "ATTENTION"
message_dict = self.info.server_info["french_scam_message"]
title = "PREVENTION ANTI-SCAM"
else:
name = "Scam Warning"
admin_message = "Click on the mentions in the message to get in contact with admin.\n <@"
message_dict = self.info.server_info["scam_message"]
title = "Warning"
actualDict = {title : message_dict}
message = makeEmbed(name=name, values=actualDict)
print(ctx.channel.name)
await ctx.channel.send("",embed=message)
self.counter[ctx.channel.id] = 0
else:
self.counter[ctx.channel.id] += 1
self.counter_locks[ctx.channel.id].release()
def is_staff_member(self,user):
user_roles = user.roles
for curr_user_role in user_roles:
if(int(curr_user_role.id) in self.info.server_info["staff_roles"]):
return True
return False
@commands.Cog.listener()
async def on_reaction_add(self,reaction, user):
await self.bot.wait_until_ready()
if reaction.emoji == "🤡" :
DataBaseManagement().update_clown_count(int(user.id))
notify_channel = await self.bot.fetch_channel(844468484036493352)
message_obj = reaction.message
url = "https://discord.com/channels/{}/{}/{}".format(740287152843128944,message_obj.channel.id,message_obj.id)
await notify_channel.send("<@&805576038486769705>" +"Forbiden Reaction: "+str(reaction.emoji)+" "+str(user.mention)+" "+str(user.id) + " " + url)
elif reaction.emoji == "🖕":
DataBaseManagement().update_middlefinger_count(int(user.id))
notify_channel = await self.bot.fetch_channel(844468484036493352)
message_obj = reaction.message
url = "https://discord.com/channels/{}/{}/{}".format(740287152843128944,message_obj.channel.id,message_obj.id)
await notify_channel.send("<@&805576038486769705>" +"Forbiden Reaction: "+str(reaction.emoji)+" "+str(user.mention)+" "+str(user.id) + " " + url)
@commands.command()
async def reaction_count(self,ctx,user_id):
await self.bot.wait_until_ready()
if(not self.is_staff_member(ctx.author)):
await ctx.channel.send("Only staff members can use this command.")
return
else:
df = DataBaseManagement().query_user_reactions(user_id)
if df.empty:
await ctx.channel.send("{} Has Not used any forbidden reactions.".format(user_id))
else:
message = "{} - 🤡: {} and 🖕: {}".format(user_id,df.iloc[0]["clown_count"],df.iloc[0]["middlefinger_count"])
await ctx.channel.send(message)
def makeEmbed(*, name=None, icon=None, colour=0xEB4034, values={}):
'''Creates an embed messasge with specified inputs'''
# Create an embed object with the specified colour
embedObj = discord.Embed(colour=colour)
# Set the author and URL
embedObj.set_author(name=name)
# Create all of the fields
for i in values:
if values[i] == '':
values[i] = 'None'
embedObj.add_field(name=i, value='{}'.format(values[i]))
# Return to user
return embedObj