Rifat Azad commited on
Commit
818b408
·
1 Parent(s): 702d428

update fix

Browse files
Files changed (1) hide show
  1. pydvpl_bot.py +30 -26
pydvpl_bot.py CHANGED
@@ -422,43 +422,47 @@ async def ping(ctx):
422
  # Calculate shared latency
423
  shared_latency = round((bot.latency * 1000) - client_latency)
424
 
425
- await msg.edit(content=f"Pong! Client latency is {client_latency:.2f}ms. - Shared latency is {shared_latency}ms.")
426
 
427
  await ctx.reply(f"The command `ping` was executed by - {ctx.author.mention}")
428
 
429
 
430
  # FUNCTION ------------------------------------------------------ SPLIT
431
 
 
 
432
 
433
  @bot.command()
434
- async def pinger(ctx, address):
435
  try:
436
-
437
- msg = await ctx.reply("Pinging...")
438
-
439
- # Pinging the given address
440
- process = subprocess.Popen(['ping', '-c', '4', address], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
441
- stdout, stderr = process.communicate()
442
-
 
 
 
 
443
 
444
- # Extracting latency from the output
445
- output = stdout.decode('utf-8')
446
- lines = output.split('\n')
447
- latency_line = lines[-2] # assuming the second last line contains latency information
448
- latency_values = latency_line.split('=')[-1].split('/')
449
- min_latency = latency_values[0]
450
- avg_latency = latency_values[1]
451
- max_latency = latency_values[2]
452
- mdev_latency = latency_values[3].split(' ')[0] # remove 'ms' from the end
453
-
454
- # Sending latency to the channel
455
- await msg.edit(content=f'Latency for {address}: Min={min_latency} ms, Avg={avg_latency} ms, Max={max_latency} ms, StdDev={mdev_latency} ms')
 
 
 
456
 
457
- except Exception as e:
458
- await msg.edit(content=f'Error occurred: {e}')
459
-
460
- # Send the author information
461
- await ctx.reply(f"The command `ping` was executed by - {ctx.author.mention}")
462
 
463
 
464
 
 
422
  # Calculate shared latency
423
  shared_latency = round((bot.latency * 1000) - client_latency)
424
 
425
+ await msg.edit(content=f"Pong! 🏓 Client latency is {client_latency:.2f}ms. - Shared latency is {shared_latency}ms.")
426
 
427
  await ctx.reply(f"The command `ping` was executed by - {ctx.author.mention}")
428
 
429
 
430
  # FUNCTION ------------------------------------------------------ SPLIT
431
 
432
+ import socket
433
+ import time
434
 
435
  @bot.command()
436
+ async def pinger(ctx, target):
437
  try:
438
+ # Attempt to resolve the target to an IP address
439
+ ip_address = socket.gethostbyname(target)
440
+
441
+ # Create a socket object
442
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
443
+
444
+ # Set a timeout for the connection attempt
445
+ s.settimeout(5)
446
+
447
+ # Record the start time
448
+ start_time = time.time()
449
 
450
+ # Connect to the target IP address on port 80 (HTTP)
451
+ s.connect((ip_address, 80))
452
+
453
+ # Calculate the round trip time (RTT)
454
+ rtt = (time.time() - start_time) * 1000 # Convert to milliseconds
455
+
456
+ # Send a message indicating success with latency
457
+ await ctx.send(f'Ping to {target} successful! 🟢 Latency: {rtt:.2f}ms')
458
+
459
+ # Close the socket
460
+ s.close()
461
+ except socket.gaierror:
462
+ await ctx.send(f'Error: Unable to resolve {target} to an IP address. 🔴')
463
+ except socket.error:
464
+ await ctx.send(f'Error: {target} is unreachable. 🔴')
465
 
 
 
 
 
 
466
 
467
 
468