试图制作一个随机发送随机消息的不和谐机器人,但我不断收到此错误

时间:2021-03-28 14:27:25

标签: python discord.py

这是我的代码:

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'

有什么想法吗?谢谢

1 个答案:

答案 0 :(得分:0)

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