Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
testing run_in_executor for add_exp (to try unblocking)
Browse files
app.py
CHANGED
@@ -56,7 +56,7 @@ def calculate_xp(level):
|
|
56 |
|
57 |
|
58 |
|
59 |
-
|
60 |
async def search_csv(file_path, column_index, search_value):
|
61 |
with open(file_path, 'r', newline='') as csvfile:
|
62 |
reader = csv.reader(csvfile)
|
@@ -88,18 +88,57 @@ def is_csv_empty(file_path):
|
|
88 |
|
89 |
return True
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
-
|
93 |
async def add_exp(member_id):
|
94 |
try:
|
95 |
-
#worksheet = gspread_bot.open("levelbot").sheet1
|
96 |
-
|
97 |
-
# Can only add exp to users who are discord users, whether or not they have a hub account linked
|
98 |
|
99 |
-
|
|
|
|
|
100 |
guild = bot.get_guild(879548962464493619)
|
101 |
member = guild.get_member(member_id)
|
102 |
-
print(f" {member}")
|
103 |
lvl1 = guild.get_role(1171861537699397733)
|
104 |
lvl2 = guild.get_role(1171861595115245699)
|
105 |
lvl3 = guild.get_role(1171861626715115591)
|
@@ -123,73 +162,25 @@ async def add_exp(member_id):
|
|
123 |
lvls = {
|
124 |
1: lvl1, 2: lvl2, 3: lvl3, 4: lvl4, 5: lvl5, 6: lvl6, 7: lvl7, 8: lvl8, 9: lvl9, 10: lvl10,
|
125 |
11: lvl11, 12: lvl12, 13: lvl13, 14: lvl14, 15: lvl15, 16: lvl16, 17: lvl17, 18: lvl18, 19: lvl19, 20: lvl20,
|
126 |
-
}
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
#await search_csv(csv_file_path, 0, member.id)
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
"""
|
153 |
-
length = len(worksheet.col_values(1))
|
154 |
-
print(f"3")
|
155 |
-
if cell is None:
|
156 |
-
print(f"creating new record for {member}")
|
157 |
-
# if not, create new record
|
158 |
-
string_member_id = str(member.id)
|
159 |
-
xp = 10
|
160 |
-
current_level = calculate_level(xp)
|
161 |
-
member_name = member.name
|
162 |
-
worksheet.update(values=[[string_member_id, member_name, xp, current_level]], range_name=f'A{length+1}:D{length+1}')
|
163 |
-
# initial role assignment
|
164 |
-
if current_level == 1:
|
165 |
-
if lvl1 not in member.roles:
|
166 |
-
await member.add_roles(lvl1)
|
167 |
-
print(f"Gave {member} {lvl1}")
|
168 |
-
else:
|
169 |
-
print(f"4")
|
170 |
-
if cell:
|
171 |
-
# if so, update that row...
|
172 |
-
xp = worksheet.cell(cell.row, cell.col+2).value
|
173 |
-
xp = int(xp) + XP_PER_MESSAGE
|
174 |
-
current_level = calculate_level(xp)
|
175 |
-
print(f"updating record for {member}: {xp} xp")
|
176 |
-
# write with added xp
|
177 |
-
worksheet.update(values=[[xp, current_level]], range_name=f'C{cell.row}:D{cell.row}')
|
178 |
-
# level up
|
179 |
-
if current_level >= 2 and current_level <=20:
|
180 |
-
current_role = lvls[current_level]
|
181 |
-
if current_role not in member.roles:
|
182 |
-
await member.add_roles(current_role)
|
183 |
-
print(f"Giving {member} {current_role}")
|
184 |
-
await member.remove_roles(lvls[current_level-1])
|
185 |
-
print(f"Removed {lvls[current_level-1]} from {member}")
|
186 |
-
#print(f"{member} Level up! {current_level-1} -> {current_level}!")
|
187 |
-
if current_role in member.roles:
|
188 |
-
await member.send(f"Level up! {current_level-1} -> {current_level}!")
|
189 |
-
|
190 |
-
"""
|
191 |
-
|
192 |
-
|
193 |
except Exception as e:
|
194 |
print(f"add_exp Error: {e}")
|
195 |
|
|
|
56 |
|
57 |
|
58 |
|
59 |
+
"""
|
60 |
async def search_csv(file_path, column_index, search_value):
|
61 |
with open(file_path, 'r', newline='') as csvfile:
|
62 |
reader = csv.reader(csvfile)
|
|
|
88 |
|
89 |
return True
|
90 |
|
91 |
+
"""
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
+
def blocking_io(member_id):
|
96 |
+
worksheet = gspread_bot.open("levelbot").sheet1
|
97 |
+
|
98 |
+
guild = bot.get_guild(879548962464493619)
|
99 |
+
member = guild.get_member(member_id)
|
100 |
+
print(f" {member}")
|
101 |
+
print(f"2")
|
102 |
+
|
103 |
+
length = len(worksheet.col_values(1))
|
104 |
+
print(f"3")
|
105 |
+
if cell is None:
|
106 |
+
print(f"creating new record for {member}")
|
107 |
+
# if not, create new record
|
108 |
+
string_member_id = str(member.id)
|
109 |
+
xp = 10
|
110 |
+
current_level = calculate_level(xp)
|
111 |
+
member_name = member.name
|
112 |
+
worksheet.update(values=[[string_member_id, member_name, xp, current_level]], range_name=f'A{length+1}:D{length+1}')
|
113 |
+
# initial role assignment
|
114 |
+
if current_level == 1:
|
115 |
+
if lvl1 not in member.roles:
|
116 |
+
return(1)
|
117 |
+
|
118 |
+
else:
|
119 |
+
print(f"4")
|
120 |
+
if cell:
|
121 |
+
# if so, update that row...
|
122 |
+
xp = worksheet.cell(cell.row, cell.col+2).value
|
123 |
+
xp = int(xp) + XP_PER_MESSAGE
|
124 |
+
current_level = calculate_level(xp)
|
125 |
+
print(f"updating record for {member}: {xp} xp")
|
126 |
+
# write with added xp
|
127 |
+
worksheet.update(values=[[xp, current_level]], range_name=f'C{cell.row}:D{cell.row}')
|
128 |
+
# level up
|
129 |
+
if current_level >= 2 and current_level <=20:
|
130 |
+
# pass current_level as int?
|
131 |
+
return(current_level)
|
132 |
|
133 |
+
|
134 |
async def add_exp(member_id):
|
135 |
try:
|
|
|
|
|
|
|
136 |
|
137 |
+
loop = asyncio.get_running_loop()
|
138 |
+
result = await loop.run_in_executor(None, blocking_io, member_id)
|
139 |
+
|
140 |
guild = bot.get_guild(879548962464493619)
|
141 |
member = guild.get_member(member_id)
|
|
|
142 |
lvl1 = guild.get_role(1171861537699397733)
|
143 |
lvl2 = guild.get_role(1171861595115245699)
|
144 |
lvl3 = guild.get_role(1171861626715115591)
|
|
|
162 |
lvls = {
|
163 |
1: lvl1, 2: lvl2, 3: lvl3, 4: lvl4, 5: lvl5, 6: lvl6, 7: lvl7, 8: lvl8, 9: lvl9, 10: lvl10,
|
164 |
11: lvl11, 12: lvl12, 13: lvl13, 14: lvl14, 15: lvl15, 16: lvl16, 17: lvl17, 18: lvl18, 19: lvl19, 20: lvl20,
|
165 |
+
}
|
166 |
+
|
167 |
+
if result == 1:
|
168 |
+
await member.add_roles(lvl1)
|
169 |
+
print(f"Gave {member} {lvl1}")
|
170 |
+
|
171 |
+
if result >= 2:
|
172 |
+
current_level = result
|
173 |
+
current_role = lvls[current_level]
|
174 |
+
if current_role not in member.roles:
|
175 |
+
await member.add_roles(current_role)
|
176 |
+
print(f"Giving {member} {current_role}")
|
177 |
+
await member.remove_roles(lvls[current_level-1])
|
178 |
+
print(f"Removed {lvls[current_level-1]} from {member}")
|
179 |
+
#print(f"{member} Level up! {current_level-1} -> {current_level}!")
|
180 |
+
if current_role in member.roles:
|
181 |
+
await member.send(f"Level up! {current_level-1} -> {current_level}!")
|
182 |
+
|
183 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
except Exception as e:
|
185 |
print(f"add_exp Error: {e}")
|
186 |
|