lunarflu HF staff commited on
Commit
5bfdee1
1 Parent(s): e3f2b9b

cleaning up csv start

Browse files
Files changed (1) hide show
  1. app.py +62 -63
app.py CHANGED
@@ -343,85 +343,84 @@ async def xp_help(ctx):
343
 
344
 
345
  @bot.command()
346
- async def test3(ctx):
347
- if ctx.author.id == 811235357663297546:
348
- worksheet = gspread_bot.open("levelbot").sheet1
349
- values = worksheet.get_all_values()
350
- target_id = '811235357663297546'
351
- variable_xp = 0
352
-
353
- csv_file_path = 'test3.csv'
354
- with open(csv_file_path, 'w', newline='') as csv_file:
355
- csv_writer = csv.writer(csv_file)
356
- csv_writer.writerows(values)
357
 
358
 
359
- # modifying csv data
360
- with open(csv_file_path, 'r') as csv_file:
361
- csv_reader = csv.reader(csv_file)
362
- rows = list(csv_reader)
363
-
364
- for row in rows:
365
- print(row)
366
- if target_id in row:
367
- index_of_target = row.index(target_id)
368
-
369
- # 2 to right
370
- cell_to_increment_index = index_of_target + 2
371
-
372
- # +10
373
- try:
374
- row[cell_to_increment_index] = str(int(row[cell_to_increment_index]) + variable_xp)
375
- print(row[cell_to_increment_index])
376
- except (IndexError, ValueError):
377
- print(f"Error: Unable to increment cell at index {cell_to_increment_index}")
378
-
379
- break
380
 
381
 
382
- # write modified data and send in discord
383
- with open(csv_file_path, 'w', newline='') as csv_file:
384
- csv_writer = csv.writer(csv_file)
385
- csv_writer.writerows(rows)
386
 
387
- csv_data_str = '\n'.join(','.join(row) for row in rows)
388
- csv_file = discord.File(csv_file_path)
389
- target_user = await bot.fetch_user(int(target_id))
390
 
391
- await target_user.send(content="csv", file=csv_file)
 
 
 
392
 
393
 
394
 
395
-
396
 
397
- # do simple operation on file (change one value)
398
 
399
- # post current version to discord
400
 
401
- # recover csv from discord channel, read into memory
402
 
403
 
404
  @bot.command()
405
  async def read_csv(ctx):
406
- if ctx.author.id == 811235357663297546:
407
- async for message in ctx.channel.history(limit=10):
408
- if message.attachments:
409
- attachment = message.attachments[0]
410
- if attachment.filename.endswith('.csv'):
411
- csv_content = await attachment.read()
412
- csv_data = []
413
- csv_lines = csv_content.decode('utf-8').splitlines()
414
- csv_reader = csv.reader(csv_lines)
415
- for row in csv_reader:
416
- csv_data.append(row)
417
- print(csv_data)
418
-
419
- await ctx.send("discord -> read -> updated HF Hub")
420
- break
421
- else:
422
- await ctx.send("The latest attachment is not a CSV file.")
423
- else:
424
- await ctx.send("No attachments found in the latest message.")
425
 
426
 
427
  """
 
343
 
344
 
345
  @bot.command()
346
+ async def write_csv(ctx):
347
+ worksheet = gspread_bot.open("levelbot").sheet1
348
+ values = worksheet.get_all_values()
349
+ target_id = '811235357663297546'
350
+ variable_xp = 0
351
+
352
+ csv_file_path = 'test.csv'
353
+ with open(csv_file_path, 'w', newline='') as csv_file:
354
+ csv_writer = csv.writer(csv_file)
355
+ csv_writer.writerows(values)
 
356
 
357
 
358
+ # modifying csv data
359
+ with open(csv_file_path, 'r') as csv_file:
360
+ csv_reader = csv.reader(csv_file)
361
+ rows = list(csv_reader)
362
+
363
+ for row in rows:
364
+ print(row)
365
+ if target_id in row:
366
+ index_of_target = row.index(target_id)
367
+
368
+ # 2 to right
369
+ cell_to_increment_index = index_of_target + 2
370
+
371
+ # +10
372
+ try:
373
+ row[cell_to_increment_index] = str(int(row[cell_to_increment_index]) + variable_xp)
374
+ print(row[cell_to_increment_index])
375
+ except (IndexError, ValueError):
376
+ print(f"Error: Unable to increment cell at index {cell_to_increment_index}")
377
+
378
+ break
379
 
380
 
381
+ # write modified data and send in discord
382
+ with open(csv_file_path, 'w', newline='') as csv_file:
383
+ csv_writer = csv.writer(csv_file)
384
+ csv_writer.writerows(rows)
385
 
386
+ csv_data_str = '\n'.join(','.join(row) for row in rows)
387
+ csv_file = discord.File(csv_file_path)
 
388
 
389
+ await ctx.channel.send(content="csv", file=csv_file)
390
+
391
+ #target_user = await bot.fetch_user(int(target_id))
392
+ #await target_user.send(content="csv", file=csv_file)
393
 
394
 
395
 
396
+
397
 
398
+ # do simple operation on file (change one value)
399
 
400
+ # post current version to discord
401
 
402
+ # recover csv from discord channel, read into memory
403
 
404
 
405
  @bot.command()
406
  async def read_csv(ctx):
407
+ async for message in ctx.channel.history(limit=10):
408
+ if message.attachments:
409
+ attachment = message.attachments[0]
410
+ if attachment.filename.endswith('.csv'):
411
+ csv_content = await attachment.read()
412
+ csv_data = []
413
+ csv_lines = csv_content.decode('utf-8').splitlines()
414
+ csv_reader = csv.reader(csv_lines)
415
+ for row in csv_reader:
416
+ csv_data.append(row)
417
+ print(csv_data)
418
+
419
+ await ctx.send("discord -> read -> updated HF Hub")
420
+ # react to the given message, helps with explainability
421
+ await message.add_reaction('✅')
422
+ break
423
+
 
 
424
 
425
 
426
  """