-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcodebot.py
More file actions
184 lines (156 loc) · 5.86 KB
/
leetcodebot.py
File metadata and controls
184 lines (156 loc) · 5.86 KB
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
import discord
from discord.ext import commands
import asyncio
import Classmate
import time
import pickle
from datetime import datetime
#TOKEN = open("leetcodebottoken.txt", "rt")
TOKEN = "TOKEN HERE"
#Changes time between saves in seconds
SAVEINTERVAL = 5
bot = commands.Bot(command_prefix='.')
try:
classmates = pickle.load(open("save.p", "rb"))
except:
classmates = []
#Saving files
async def save():
pickle.dump(classmates, open("save.p", "wb"))
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Save Created at", current_time)
@bot.event
async def on_ready():
await bot.change_presence(activity=discord.Game(name="type .info for info"))
print("LeetCodeBot is ready.")
print("Save interval set to", SAVEINTERVAL, "seconds")
print("Logged in as {0.user}".format(bot))
print(discord.__version__)
while True:
await asyncio.sleep(SAVEINTERVAL)
await save()
#used for testing
@bot.command()
async def echo(ctx, *, message):
await ctx.send(message)
@bot.command()
async def add_topics(ctx, *, in_text):
topics = in_text.split(", ")
user = ctx.message.author.display_name
id = ctx.message.author.id
for person in classmates:
if person.name == user:
for topic in topics:
person.topics.append(topic)
return
else:
newClassMate = Classmate.ClassMate(user, id)
for topic in topics:
newClassMate.topics.append(topic)
classmates.append(newClassMate)
return
@bot.command()
async def users_topics(ctx, *, in_text):
user = in_text.replace("@!", "") #to remove the @ and ! sign from user pings
out_string = ""
for person in classmates:
if person.name == user or "<" + str(person.id) + ">" == user: #id is returned in pings rather than the name dispite appearing as the name
if len(person.topics) <= 0:
await ctx.send("User has any topics currently")
for topic in person.topics:
out_string = out_string + topic + ", "
out_string = out_string[:-2]
await ctx.send(out_string)
return
await ctx.send("User has not added any topics or does not exist")
@bot.command()
async def my_topics(ctx):
out_string = ""
for person in classmates:
if person.id == ctx.message.author.id:
if len(person.topics) <= 0:
await ctx.send("You currently do not have any Leetcode Topics Set")
return
for topic in person.topics:
out_string = out_string + topic + ", "
out_string = out_string[:-2]
await ctx.send(out_string)
return
await ctx.send("You do not have any LeetCode Topics set yet")
@bot.command()
async def clear_topics(ctx):
for person in classmates:
if person.id == ctx.message.author.id:
person.topics = []
await ctx.send("Topics cleared")
@bot.command()
async def remove_topics(ctx, *, in_text):
topicsToRemove = in_text.split(", ")
for person in classmates:
if person.id == ctx.message.author.id:
for toRemove in topicsToRemove:
person.topics.remove(toRemove)
@bot.command()
async def find_topics(ctx, *, in_text):
matches = []
out_string = ""
for person in classmates:
if in_text in person.topics:
matches.append(person.name)
if len(matches) <= 0:
await ctx.send("No matches found.")
return
for match in matches:
out_string = out_string + match + ", "
out_string = out_string[:-2]
await ctx.send(out_string)
@bot.command()
async def shared_topics(ctx):
matches = []
out_string = ""
for person in classmates:
if person.id == ctx.message.author.id:
user = person
for person in classmates:
if user.id != person.id and not set(person.topics).isdisjoint(user.topics):
matches.append(person.name)
if len(matches) <= 0:
await ctx.send("No matches found.")
return
for match in matches:
out_string = out_string + match + ", "
out_string = out_string[:-2]
await ctx.send(out_string)
@bot.command()
async def whos_leetcoding(ctx):
currentlyLeetcoding = []
out_string = ""
members = ctx.message.guild.members
for member in members:
for activity in member.activities:
if activity.type == discord.ActivityType.custom and "leetcoding now" in activity.name.casefold() and member not in currentlyLeetcoding: #casefold() allowes caseinsensitve match
currentlyLeetcoding.append(member.name)
if len(currentlyLeetcoding) <= 0:
await ctx.send("Noone is currently Leetcoding")
return
for person in currentlyLeetcoding:
out_string = out_string + person + ", "
out_string = out_string[:-2]
await ctx.send(out_string)
@bot.command()
async def info(ctx):
multiline = """ This is a bot for people to find others to leetcode/study with add a custom status including the words leetcoding now to show that you are available
Commands include:
```.whos_leetcoding: Shows all people currently working on leetcode.
.add_topics: Type topics you want to leetcode about if multiple use a comma and space seperator ,
.remove_topics: Type topics you wish to remove from you list of topics
.clear_topics: Clear your list of topics
.my_topics: Lists all of your topics
.users_topics: Lists all of a users topics either by name or by @ ping
.shared_topics: Find all users who share a topic with you.
.find_topic: Find all users with a particular topic```
"""
await ctx.send(multiline)
bot.run(TOKEN)
#bot.run(TOKEN.read())