python discord bot事件不工作

python discord bot事件不工作,python,discord.py,Python,Discord.py,“我的距离计算器”事件不起作用。 它应该(如果有效的话)计算两个位置之间的距离,并给出一条消息,说明玩家应该等待多长时间才能到达那里(口袋妖怪围棋相关) 没有给我错误,它什么也没做。 我尝试过不同的事情,但似乎都不管用。 有人能帮我吗?那太好了 from discord.ext import commands import discord import gpxpy.geo import math import asyncio client = discord.Client(command_pr

“我的距离计算器”事件不起作用。 它应该(如果有效的话)计算两个位置之间的距离,并给出一条消息,说明玩家应该等待多长时间才能到达那里(口袋妖怪围棋相关) 没有给我错误,它什么也没做。 我尝试过不同的事情,但似乎都不管用。 有人能帮我吗?那太好了

from discord.ext import commands
import discord
import gpxpy.geo
import math
import asyncio

client = discord.Client(command_prefix='kaikai*')

@client.async_event
async def on_ready():
    print("Logged in as:")
    print(client.user.name)
    print("ID:")
    print(client.user.id)
    print("Ready to use!")

@client.async_event
async def on_message(message) :
    if message.author == client.user:
        return
    elif message.content.startswith("kaikai*test") :
        await client.send_message(message.channel, "BOT READY!")

@client.async_event
class Distance:
    """Calculate the distance between two coordinates."""
    def __init__(self, client):
        self.client = client

    @commands.command(pass_context=True)
    async def distance(self, ctx, *, message):
        """Calculate the distance between two coordinates and also provide you with a cooldown time."""
        # Your code will go here

        def error_embed_1(self):
            embed=discord.Embed(
                title="Error:",
                description="Not enough data given. Did you give 4 different coordinates, seperated by spaces?",
                color=0x207cee)

            return embed
        def error_embed_2(self):
            embed=discord.Embed(
                title="Error:",
                description="Your message is invalid. Please use this format\n``!distance <num1> <num2> <num3> <num4>``\nWhich would look like this: ``!distance 51.301597 -0.598019 51.270664 -0.594132``",
                color=0x207cee)
            return embed

        def calc_embed(msg, msg2):
            embed=discord.Embed(
                title="",
                description=" ",
                color=0x207cee)
            embed.add_field(
                name="**Calculated Distance:**",
                value="{} Kilometers".format(msg),
                inline=False)
            embed.add_field(
                name="**Cooldown Timer:**",
                value="{} ".format(msg2),
                inline=False)
            return embed

        def calculate(lon1, lat1, lon2, lat2):
            dist = gpxpy.geo.haversine_distance(lat1, lon1, lat2, lon2)
            dist = dist/1000
            dist = round(dist, 2)

            return dist

        def cooldown(dist):
            time = " "


            if dist >= 1500:
                time = "120 minutes"

            elif dist >= 1403 and dist <= 1500:
                time = "120 minutes"

            elif dist >= 1344 and dist <= 1403:
                time = "119 minutes"

            elif dist >= 1300 and dist <= 1344:
                time = "117 minutes"

            elif dist >= 1221 and dist <= 1300:
                time = "112 minutes"

            elif dist >= 1180 and dist <= 1221:
                time = "109 minutes"

            elif dist >= 1020 and dist <= 1180:
                time = "101 minutes"

            elif dist >= 1007 and dist <= 1020:
                time = "97 minutes"

            elif dist >= 948 and dist <= 1007:
                time = "94 minutes"

            elif dist >= 897 and dist <= 948:
                time = "90 minutes"

            elif dist >= 839 and dist <= 897:
                time = "88 minutes"

            elif dist >= 802 and dist <= 839:
                time = "83 minutes"

            elif dist >= 751 and dist <= 802:
                time = "81 minutes"

            elif dist >= 700 and dist <= 751:
                time = "76 minutes"

            elif dist >= 650 and dist <= 700:
                time = "73 minutes"

            elif dist >= 600 and dist <= 650:
                time = "69 minutes"

            elif dist >= 550 and dist <= 600:
                time = "65 minutes"

            elif dist >= 500 and dist <= 550:
                time = "61 minutes"

            elif dist >= 450 and dist <= 500:
                time = "58 minutes"

            elif dist >= 400 and dist <= 450:
                time = "54 minutes"

            elif dist >= 350 and dist <= 400:
                time = "49 minutes"

            elif dist >= 328 and dist <= 350:
                time = "48 minutes"

            elif dist >= 300 and dist <= 328:
                time = "46 minutes"

            elif dist >= 250 and dist <= 300:
                time = "41 minutes"

            elif dist >= 201 and dist <= 250:
                time = "36 minutes"

            elif dist >= 175 and dist <= 201:
                time = "33 minutes"

            elif dist >= 150 and dist <= 175:
                time = "31 minutes"

            elif dist >= 125 and dist <= 150:
                time = "28 minutes"

            elif dist >= 100 and dist <= 125:
                time = "26 minutes"

            elif dist >= 90 and dist <= 100:
                time = "24 minutes"

            elif dist >= 80 and dist <= 90:
                time = "23 minutes"

            elif dist >= 70 and dist <= 80:
                time = "22 minutes"

            elif dist >= 60 and dist <= 70:
                time = "21 minutes"

            elif dist >= 50 and dist <= 60:
                time = "20 minutes"

            elif dist >= 45 and dist <= 50:
                time = "19 minutes"

            elif dist >= 40 and dist <= 45:
                time = "18 minutes"

            elif dist >= 35 and dist <= 40:
                time = "17 minutes"

            elif dist >= 30 and dist <= 35:
                time = "16 minutes"

            elif dist >= 25 and dist <= 30:
                time = "14 minutes"

            elif dist >= 20 and dist <= 25:
                time = "11 minutes"

            elif dist >= 15 and dist <= 20:
                time = "8 minutes"

            elif dist >= 10 and dist <= 15:
                time = "6 minutes"

            elif dist >= 8 and dist <= 10:
                time = "4 minutes"

            elif dist >= 5 and dist <= 8:
                time = "3 minutes"

            elif dist >= 4 and dist <= 5:
                time = "2 minutes"

            elif dist >= 3 and dist <= 4:
                time = "2 minutes"

            elif dist >= 2 and dist <= 3:
                time = "1 minutes"

            elif dist and dist <= 1:
                time = "48 seconds"

            return time

        bool = True
        List = str(message)
        var = List.split(" ")
        try:
            lat1 = float(var[0])
            long1 = float(var[1])

            lat2 = float(var[2])
            long2 = float(var[3])

        except IndexError:
            msg = error_embed_1(self)
            bool = False
        except ValueError:
            msg = error_embed_2(self)
            bool = False

        if bool == True:
            calc = calculate(long1, lat1, long2, lat2)
            cooldown = cooldown(calc)
            msg = calc_embed(calc, cooldown)
        await client.send(embed=msg)


client.run("mytokenhere")
来自discord.ext导入命令
进口不和
导入gpxpy.geo
输入数学
导入异步
client=discord.client(命令前缀=kaikai*)
@client.async\u事件
_ready()上的异步定义:
打印(“登录身份:”)
打印(client.user.name)
打印(“ID:)
打印(client.user.id)
打印(“准备使用!”)
@client.async\u事件
异步def on_消息(消息):
如果message.author==client.user:
返回
elif message.content.startswith(“kaikai*test”):
等待客户端。发送消息(message.channel,“BOT READY!”)
@client.async\u事件
班级距离:
“”“计算两个坐标之间的距离。”“”
定义初始化(自我,客户机):
self.client=client
@commands.command(pass\u context=True)
异步定义距离(自身、ctx、*、消息):
“”“计算两个坐标之间的距离,并为您提供冷却时间。”“”
#你的代码会在这里
def错误_嵌入_1(自身):
嵌入=discord.embed(
title=“错误:”,
description=“提供的数据不够。是否提供了4个不同的坐标,用空格分隔?”,
颜色=0x207cee)
返回嵌入
def错误_嵌入_2(自身):
嵌入=discord.embed(
title=“错误:”,
description=“您的邮件无效。请使用此格式\n`!distance``\n如下所示:`!distance 51.301597-0.598019 51.270664-0.594132`”,
颜色=0x207cee)
返回嵌入
def calc_嵌入(msg、msg2):
嵌入=discord.embed(
title=“”,
description=“”,
颜色=0x207cee)
embed.add_字段(
name=“**计算距离:*”,
value=“{}公里”。格式(msg),
内联=假)
embed.add_字段(
name=“**冷却计时器:*”,
value=“{}”格式(msg2),
内联=假)
返回嵌入
def计算(lon1、lat1、lon2、lat2):
距离=gpxpy.geo.haversine_距离(lat1、lon1、lat2、lon2)
距离=距离/1000
距离=圆形(距离,2)
返回区
def冷却(距离):
time=“”
如果距离>=1500:
时间=“120分钟”

elif dist>=1403,dist=1344,dist=1300,dist=1221,dist=1180,dist=1020,dist=1007,dist=948,dist=897,dist=839,dist=802,dist=751,dist=700,dist=650,dist=600,dist=550,dist=450,dist=400,dist=350,dist=328,dist=300,dist=250,dist=201,dist=175,dist=150,dist=125,dist=100,dist=90,dist=80,dist=70,dist=60,dist=50,dist=45,dist=40,dist=35,dist=30,dist=25,dist=20,dist=15,dist=8,dist=5,dist=4,dist=3,dist=2,dist将所有
@client.async_事件
更改为
@client.event
。您可以在示例中看到,这是正确的用法

由于您有一个
on_message
事件,因此必须指定必须处理的命令。这是通过在
on\u message
事件中调用
process\u命令(message)
来完成的。默认情况下不会这样做。异步
分支的文档是,而重写分支的文档是

您创建的
Distance
类也未加载(据我所知,如果我错了,请纠正我)。我建议将其移动到单独的文件中,然后将其加载到主bot文件中。下面是如何实现这一点的示例,其中
bot.py
是运行bot的主文件,
cog.py
包含一个带有
add
命令的示例类
bot.py
还包括带有
process\u命令(message)
on\u message
事件,以说明其用法

bot.py

from discord.ext import commands

client = commands.Bot(command_prefix='!')

client.load_extension('cog')

@client.event
async def on_ready():
    print('client ready')

@client.command()
async def ping():
    await client.say('Pong')

@client.event
async def on_message(message):
    if message.content == 'Hello'
        await client.send_message(message.channel, 'Hello')

    await client.process_commands(message)

client.run('TOKEN')
from discord.ext import commands

class TestCog:

    def __init__(self, bot):
        self.bot = bot
        self.counter = 0

    @commands.command()
    async def add(self):
        self.counter += 1
        await self.bot.say('Counter is now %d' % self.counter)


def setup(bot):
    bot.add_cog(TestCog(bot))
cog.py

from discord.ext import commands

client = commands.Bot(command_prefix='!')

client.load_extension('cog')

@client.event
async def on_ready():
    print('client ready')

@client.command()
async def ping():
    await client.say('Pong')

@client.event
async def on_message(message):
    if message.content == 'Hello'
        await client.send_message(message.channel, 'Hello')

    await client.process_commands(message)

client.run('TOKEN')
from discord.ext import commands

class TestCog:

    def __init__(self, bot):
        self.bot = bot
        self.counter = 0

    @commands.command()
    async def add(self):
        self.counter += 1
        await self.bot.say('Counter is now %d' % self.counter)


def setup(bot):
    bot.add_cog(TestCog(bot))