-
Notifications
You must be signed in to change notification settings - Fork 11
/
bot.py
163 lines (156 loc) · 7.99 KB
/
bot.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
import discord
from contest_reminder import reminder
from _handle_verification_ import handle_verification
import gitgud
from paginator import table
import asyncio
import clist_api
import db
import stalk
import contest_info
import challenge
from graphs import rating_vs_problems, problem_vs_time,performance
from help import help as help_command
import os
from dotenv import load_dotenv
load_dotenv('process.env')
async def reconnect():
await asyncio.sleep(10)
await client.close()
await client.connect()
client = None
async def run_discord_bot():
global client
TOKEN=os.environ.get('TOKEN') #bot id
print(TOKEN)
print('hello')
client = discord.Client(intents=discord.Intents.all()) #giving permissions and intents to the bot
# guild_id=1048212913539784805 #guild id
# guild=client.get_guild(guild_id)
@client.event
async def on_ready(): #logged in successfully
print('hello')
await reminder(client)
@client.event
async def on_disconnect():
await reconnect()
@client.event
async def on_connection_error(error):
print(error)
await reconnect()
@client.event
async def on_message(ctx):
if ctx.author == client.user: #will keep messaging itself without this
return
user_message=str(ctx.content)
if (user_message.startswith(';challenge')):
await challenge.challenge_question_cf(ctx,client)
if (user_message.startswith(';identify')):
await handle_verification(ctx,client)
# if user_message[0]=='?': #checking if message is private
# user_message=user_message[1:]
# await send_message(ctx,user_message,is_private=True) #message is private
elif user_message.split()[0]==";gitlog":
mydict, msg = await gitgud.gitlog(ctx)
if msg=="error":
return
if(len(mydict)==0):
await msg.edit(content=f"{ctx.author.mention} You have not solved any problem yet. use ;gitgud to get a problem")
else:
await table(ctx,client,['Problem Name','Problem Rating','Points'], mydict, isEmbed=True, current_message=msg)
elif user_message.split()[0]==";next":
await clist_api.nextcontests(ctx)
elif user_message.split()[0]==";leaderboard":
if len(user_message.split())==2:
mylist,res = await db.Leaderboard_list(ctx,user_message.split()[1])
if(len(mylist)==0):
await res.edit(content=f"{ctx.author.mention} No one is there on leaderboard yet")
if(user_message.split()[1]=="cf"):
await table(ctx,client,['Discord Name','Score','Codeforces Handle'], mylist,current_message=res)
elif(user_message.split()[1]=="ac"):
await table(ctx,client,['Discord Name','Score','Atcoder Handle'], mylist,current_message=res)
elif(user_message.split()[1]=="both"):
await table(ctx,client,['Discord Name','Total Score'], mylist,current_message=res)
else:
await res.edit(content=f"{ctx.author.mention} Please enter a valid platform")
else:
await ctx.channel.send(f"{ctx.author.mention} Please enter a valid platform")
elif user_message.split()[0]==";stalk":
if len(user_message.split())==2:
temp = await stalk.stalk_user(ctx,user_message.split()[1])
if(temp==None):
return
header,mylist,msg = temp[0],temp[1],temp[2]
if(len(mylist)==0):
await msg.edit(content=f"{ctx.author.mention} No user found")
else:
await table(ctx,client,header,mylist,current_message=msg)
elif len(user_message.split())==3:
if user_message.split()[2]=="hardest":
header,mylist,msg = await stalk.stalk_user(ctx,user_message.split()[1],hardest=True)
if(len(mylist)==0):
await msg.edit(content=f"{ctx.author.mention} No user found")
else:
await table(ctx,client,header,mylist,current_message=msg)
elif user_message.split()[2].isdigit():
header,mylist,msg = await stalk.stalk_user(ctx,user_message.split()[1],R=int(user_message.split()[2]))
if(len(mylist)==0):
await msg.edit(content=f"{ctx.author.mention} No user found")
else:
await table(ctx,client,header,mylist,current_message=msg)
else:
await ctx.channel.send(f"{ctx.author.mention} Please follow the message format")
return
else:
await ctx.channel.send(f"{ctx.author.mention} Please follow the message format")
elif user_message.split()[0]==";ratingchange":
if len(user_message.split())==3:
if(user_message.split()[1]=="cf"):
mylist,header,msg=await contest_info.codeforces_rating_changes(str(user_message.split()[2]),ctx)
if header=="error":
await msg.edit(content=f"{ctx.author.mention} Some error occurred 🧐")
elif(len(mylist)==0):
await msg.edit(content=f"{ctx.author.mention} Either no user gave the contest or the contest id is invalid 😲")
else:
await table(ctx,client,header,mylist,current_message=msg)
elif(user_message.split()[1]=="ac"):
mylist,header,msg=await contest_info.atcoder_rating_changes(str(user_message.split()[2]),ctx)
if header=="error":
await msg.edit(content=f"{ctx.author.mention} Perhaps the contest id is invalid 🧐")
elif(len(mylist)==0):
await msg.edit(content=f"{ctx.author.mention} No user gave this contest or contest is unrated 😲")
else:
await table(ctx,client,header,mylist,current_message=msg)
else:
await ctx.channel.send(f"{ctx.author.mention} Please specify a valid platform")
else:
await ctx.channel.send(f"{ctx.author.mention} Please follow the message format")
elif user_message.split()[0]==";performance":
if len(user_message.split())==2:
await performance(ctx,user_message.split()[1])
else:
await ctx.channel.send(f"{ctx.author.mention} Please follow the message format")
elif user_message.split()[0]==";graph":
if(user_message.split()[1]=="rvp"):
await rating_vs_problems(ctx)
elif(user_message.split()[1]=="pvt"):
await problem_vs_time(ctx)
else:
await ctx.channel.send(f"{ctx.author.mention} Please follow the message format")
elif user_message.split()[0]==";help":
content=await help_command()
await ctx.channel.send(embed=content)
elif user_message.split()[0]==";gitgud":
return await gitgud.gitgud(ctx)
elif user_message.split()[0]==";gotgud":
return await gitgud.gotgud(ctx)
elif user_message.split()[0]==";nogud":
if len(user_message.split())==2:
if user_message.split()[1]=="cf":
return await gitgud.nogud_cf(ctx)
elif user_message[1]=="ac":
return await gitgud.nogud_atcoder(ctx)
elif user_message.split()[0]==";gimme":
return await gitgud.gimme(ctx)
return "Sorry, I didn't understand that :smiling_face_with_tear: Try ;help for more info"
client.run(TOKEN)