Discord Rewrite Bot 在上传到 heroku 后崩溃

Discord Rewrite Bot crashed after uploading to heroku

到目前为止,它在 heroku 和本地都运行良好。现在它只能在本地工作,但在部署到 heroku 后就不能工作了。这是来自 Heroku 的日志:

2021-05-11T14:24:06.000000+00:00 app[api]: Build succeeded
2021-05-11T14:24:06.163275+00:00 heroku[worker.1]: State changed from starting to up
2021-05-11T14:24:15.306244+00:00 app[worker.1]: Traceback (most recent call last):
2021-05-11T14:24:15.306768+00:00 app[worker.1]: File "/app/dbot.py", line 2, in <module>
2021-05-11T14:24:15.307297+00:00 app[worker.1]: import discord
2021-05-11T14:24:15.307369+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/discord/__init__.py", line 23, in <module>
2021-05-11T14:24:15.307970+00:00 app[worker.1]: from .client import *
2021-05-11T14:24:15.307996+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/discord/client.py", line 36, in <module>
2021-05-11T14:24:15.308464+00:00 app[worker.1]: from .user import User
2021-05-11T14:24:15.308533+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/discord/user.py", line 26, in <module>
2021-05-11T14:24:15.309148+00:00 app[worker.1]: import discord.abc
2021-05-11T14:24:15.309214+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/discord/abc.py", line 101, in <module>
2021-05-11T14:24:15.309876+00:00 app[worker.1]: SnowflakeTime = Union[Snowflake, datetime]
2021-05-11T14:24:15.310045+00:00 app[worker.1]: NameError: name 'datetime' is not defined
2021-05-11T14:24:15.685219+00:00 heroku[worker.1]: Process exited with status 1
2021-05-11T14:24:15.995755+00:00 heroku[worker.1]: State changed from up to crashed

我的 requirments.txt 文件包含以下依赖项:

git+https://github.com/Rapptz/discord.py
youtube_dl==2020.03.08
pynacl == 1.3.0
colorlog == 4.1.0

我的 procfile 中有这个:

worker: python dbot.py

我知道它说 'datetime' 未定义,但这是我的主 python 文件的前几行:

import datetime
import discord
from discord.ext import commands
from discord import FFmpegPCMAudio
import random
import youtube_dl
import os
from discord.utils import get
import ctypes
import ctypes.util

据我所知,这一切都是在我将以下事件添加到我的代码后开始的:

@client.event
async def on_message(ctx):
    try:
        if ctx.channel.name == "memes" and "https://" not in str(ctx.content) and not ctx.attachments:
            await ctx.channel.purge(limit=1)
        else:
            await client.process_commands(ctx)
    except:
            await client.process_commands(ctx)

git+https://github.com/Rapptz/discord.py是discord.py的开发分支,不要在生产中使用。

git+https://github.com/Rapptz/discord.py@1.7.2

1.7.2 为最新版本。

或来自 PyPi

discord.py>=1.7.2

discord.py 在 PyPI 中可用,因此可以直接从 PyPI 下载。

前一个答案是导致该错误的正确答案,但您为 on_message 事件显示的内容也不正确。我建议阅读 the documentation for on_message 以了解它应该如何工作。

编辑:

它目前正在运行,因为您没有使用任何使用 Context 对象的东西。 on_message 采用 discord.Message 参数,而不是 commands.Context 参数。这意味着,如果您尝试使用来自 Context 的方法或属性,它将不起作用,因为 Message 是传递给 on_message.

的参数

例如,如果我尝试在您的代码中执行 ctx.bot,它会给我一个错误,因为 discord.Message 没有 bot 属性。现在你可能正在查看文档并想知道为什么它说 ctx 有一个 bot 属性,但是当你尝试它时却出错了。出于这个原因,我建议将您的 on_message 参数更改为 message 并将 ctx 的所有实例更改为 message 以避免以后混淆。