【问题标题】:Discord Rewrite Bot crashed after uploading to herokuDiscord Rewrite Bot 上传到 heroku 后崩溃
【发布时间】:2021-05-12 02:01:34
【问题描述】:

到目前为止,它在 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

我的 request.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

我知道它说“日期时间”未定义,但这是我的主要 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)

【问题讨论】:

  • NameError: name 'datetime' is not defined: 你的系统上是否为 python-3.9 安装了 datetime?

标签: python heroku discord.py


【解决方案1】:

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 下载。

【讨论】:

    【解决方案2】:

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

    编辑:

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

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

    【讨论】:

      猜你喜欢
      • 2020-08-13
      • 2020-09-27
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 2020-08-15
      • 2014-02-28
      相关资源
      最近更新 更多