首页 > 解决方案 > discord.py,RuntimeWarning:启用 tracemalloc 以在发送消息时获取对象分配回溯

问题描述

好的,所以我遇到了这样的问题:我编写了一个代码,该代码旨在每隔 10 秒(只是现在,用于测试)在 discord 服务器上发送消息,并且当我尝试在 discord 上执行命令时:

import os
import sched
import time

from discord.ext import commands
from dotenv import load_dotenv

import Library

load_dotenv()
TOKEN = os.getenv('discordToken')

bot = commands.Bot(command_prefix='Question')
lastEmbed = None
s = sched.scheduler(time.time, time.sleep)


async def do_something(sc):
    channel = bot.get_channel(416238248445214720)
    await channel.send("And what?")
    s.enter(10, 1, do_something, (sc,))


@bot.event
async def on_ready():
    print(f'{bot.user.name} has connected to Discord!')
    s.enter(10, 1, do_something, (s,))
    s.run()

每次我收到这样的错误:

C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\sched.py:151: RuntimeWarning: coroutine 'do_something' was never awaited
  action(*argument, **kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

标签: pythondiscorddiscord.py

解决方案


好的,谢谢@FrozenAra 和@derw 的帮助,尽管我找到了一种完全不同的方法来解决我使用 discord.Client 库和任务的问题,所以现在看起来像这样:

@tasks.loop(minutes=30)
async def reminder():
    channel = client.get_channel(692724253237313576)
    await channel.send("So what?")
    await channel.send(preembed)

一切都很好。感谢您的帮助伙计们:)


推荐阅读