【问题标题】:Pythonping not working with a discord botPythonping 无法与不和谐的机器人一起使用
【发布时间】:2021-09-16 10:26:20
【问题描述】:

我想让这个 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)```

【问题讨论】:

  • 你能告诉我这个错误在我的代码中的什么地方吗?

标签: python discord discord.py ip ping


【解决方案1】:

我可以引导您完成代码中的错误:

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

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

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

async def ping_ip(ip) -> str:  # The function returns a string
    ... # Do pinging here and return a str

@client.command()
async def ping(ctx, ip: str):
    data = await ping_ip(ip)
    return await ctx.send(data)

【讨论】:

  • 我改变了它现在它给了我这个消息“”它没有给出任何语法错误。这是我的代码:async def ping_ip(ip) -> str: # The function returns a string ping return str # Do pinging here and return a str @client.command() async def ping(ctx, ip: str): data = await ping_ip(ip) return await ctx.send(data) client.run(token)
【解决方案2】:

我使用此代码查找机器人 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 2021-03-25
    • 2021-05-23
    • 2019-04-09
    • 2023-04-04
    • 2018-03-20
    • 2021-09-12
    相关资源
    最近更新 更多