首页 > 解决方案 > 试图制作一个随机发送随机消息的不和谐机器人,但我不断收到此错误

问题描述

这是我的代码:

import random
import discord
import asyncio

from discord.ext import commands

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

msg = ['msg1','msg2','msg3']

@bot.command()
async def test(message):
  while True:
    chance = random.randint(1,4)
    print(chance)
    if chance == 1:
      msj = random.choice(msg)
      await message.send(msj)
    time = random.randint(60,3600)
    print(time)
    await asyncio.sleep(time)
bot.loop.create_task(test("!test"))

还有我的错误:

await message.send(msj)
AttributeError: 'str' object has no attribute 'send'

我尝试使用 bot.event 来做这件事await bot.send(channel, msj),但我得到了一个类似的错误:

await bot.send(channel, msj)
AttributeError: 'Bot' object has no attribute 'send'

有任何想法吗?谢谢

标签: pythondiscord.py

解决方案


尝试添加 ctx 而不是 message 然后执行:await ctx.message.channel.send(msg)


推荐阅读