Pythonping 不能与不和谐机器人一起使用

时间:2021-07-05 16:43:17

标签: python discord discord.py ip ping

我想让这个 discord bot 可以 ping、portscan 和 whois 服务器和 IP。所有的命令都工作得很好,但是当我尝试使用 pythonping 时,我得到一个我看不到的错误。我得到此消息不一致,<coroutine object Command.call at 0x04E55368> 和 Visual Studio Code 终端中的此错误我收到此错误 C:\Users\swevl\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py:85: RuntimeWarning: coroutine 'Command.__call__' was never awaited ret = await coro(*args, **kwargs) RuntimeWarning: Enable tracemalloc to get the object allocation traceback

这是我的代码:

from discord.ext import commands
import json
import os
import random
import string
import time
import colorama
from colorama import Fore, Back, Style
import string
from os import system, name
import aiohttp
from pythonping import ping
import requests

# # # # # # # # # # Bot Config and Token # # # # # # # # # #
def clear():
  
    if name == 'nt':
        _ = system('cls')
  
    else:
        _ = system('clear')

clear()
os.system("cls && title Gen Bot")
token = "Censored Bot Token"

client = commands.Bot(command_prefix='a!', case_insensitive=True)
client.remove_command('help')
print(f"                 {Fore.RED}╔═╗╦  ╔═╗╦ ╦╔═╗  ╔╗ ╔═╗╔╦╗")
print(f"                 {Fore.RED}╠═╣║  ╠═╝╠═╣╠═╣  ╠╩╗║ ║ ║ ")
print(f"                 {Fore.RED}╩ ╩╩═╝╩  ╩ ╩╩ ╩  ╚═╝╚═╝ ╩ ")
print(f"{Fore.YELLOW}I-----------------------------------------------------I")
@client.event
async def on_ready():
    # login of client
    await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='a!help'))
    print(f"{Fore.RED}[{Fore.YELLOW}STATUS{Fore.RED}] The bot just went online!".format(client))
#####Utiity Commands#####
@client.command()
async def clear(ctx, amount=100):
    await ctx.channel.purge(limit=amount)
    await ctx.send("Messages have been removed!", delete_after=4)
    print (f'{Fore.RED}[{Fore.YELLOW}LOGS{Fore.RED}] {ctx.author} cleared {amount} messages')



#########################################################################################################################################





@client.command(name='whois')
async def whois(ctx, arg1):
   if arg1 == "":
       await ctx.send("Invalid IP!")
   else:
       async with aiohttp.ClientSession() as session:
                async with session.get(f"http://ip-api.com/json/{arg1}?fields=66846719") as r:
                    js = await r.json()
                    myip = ('')
                    if myip == (js["query"]):
                        await ctx.send('Invalid Ip!')
                    else:
                        cont = (js["continent"])
                        country = (js["country"])
                        region = (js["regionName"])
                        city = (js["city"])
                        zipcode = (js["zip"])
                        iso = (js["isp"])
                        org = (js["org"])
                        reverse = (js["reverse"])
                        mobile = (js["mobile"])
                        proxy = (js["proxy"])
                        hosting = (js["hosting"])
                        embed1 = discord.Embed(title=(js["query"]), color = discord.Color.red())
                        embed1.add_field(name="info", value=(f"{ctx.author.mention}\n"
                                                                         f"Continent: {cont} \n"
                                                                         f"country: {country} \n"
                                                                         f"Region: {region}\n"
                                                                         f"City: {city} \n"
                                                                         f"Zip: {zipcode} \n"
                                                                         f"ISP: {iso} \n"
                                                                         f"Org: {org} \n"
                                                                         f"Reverse: {reverse} \n"
                                                                         f"Mobile: {mobile} \n"
                                                                         f"Proxy: {proxy} \n"
                                                                         f"Hosting: {hosting}"), inline=False)
                        await ctx.send(embed=embed1)
                        print (f'{Fore.RED}[{Fore.YELLOW}LOGS{Fore.RED}] {ctx.author} used the whois comand on {arg1}')




@client.command()
async def portscan(ctx, arg1):
    if arg1 == '':
     await ctx.send("Invalid IP!")
    else:
       print (f'{Fore.RED}[{Fore.YELLOW}LOGS{Fore.RED}] {ctx.author} portscanned {arg1}')
       async with aiohttp.ClientSession() as session:
                async with session.get(f"https://api.hackertarget.com/nmap/?q={arg1}") as r:
                       if r.status == 200:
                        text = await r.text()
                        embed1 = discord.Embed(title=(f'Results from {arg1}'), description=(text), color = discord.Color.red())
                        await ctx.send(embed=embed1)
                       else:
                               await ctx.send("API is offline, try again later...")


@client.command()
async def ping(ctx, arg1):
        if arg1 == '':
                await ctx.send("Invalid IP!")
        else:
                await ctx.send(ping(arg1))





client.run(token)```

2 个答案:

答案 0 :(得分:0)

我使用此代码来查找bot ping

@client.command()
@commands.guild_only()
async def ping(ctx):
    embed = discord.Embed(title="Bot ping",
                          description=f"{round(client.latency * 1000)}ms",
                          color=discord.Colour.purple())
    await ctx.send(embed=embed)

答案 1 :(得分:0)

我可以引导您解决代码中的错误:

        Set<Integer> unique = new HashSet<Integer>();
        for (int i=0; i<100; i++)
        {
            int number = rand.nextInt(upperbound);
            while (unique.contains(number)) // `number` is a duplicate
                number = rand.nextInt(upperbound);
            arr[i] = number;
            unique.add(number); // adding to the set
        }

当您点击 else 语句时,您将再次调用 ping 函数。您从未等待 ping 功能(它是一个 coro),这就是引发错误的原因。从您在此处提供的内容来看,此命令似乎只会使您的不和谐机器人崩溃。它会一遍又一遍地调用自己(无限递归)。

我认为您正在尝试使用一个命令来 ping ip 并返回响应。你需要使用不同的函数来ping,你不能一遍又一遍地调用同一个。

@client.command()
async def ping(ctx, arg1):
        if arg1 == '':
                await ctx.send("Invalid IP!")
        else:
                await ctx.send(ping(arg1))