python - 试图制作一个随机发送随机消息的不和谐机器人,但我不断收到此错误
问题描述
这是我的代码:
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'
有任何想法吗?谢谢
解决方案
尝试添加 ctx 而不是 message 然后执行:await ctx.message.channel.send(msg)
推荐阅读
- python - Display Django Templates after sucessfuly posting data
- java - 为什么在 Java 中使用扫描器类和分隔符获取双精度时会得到错误的数学结果?
- python - 如何在以另一个列表的一个元素开头的行列表中查找特定行?
- c - 创建后序二叉树数组的函数
- discord - 会员计数频道 discord.py
- pyspark - 我可以在 pyspark forEach 中使用协程函数吗?
- c - 如何在 dsPIC33F 上正确设置 DMA
- python - 'function' 对象不可下标 - 检查其他类似错误但不属于任何其他错误?
- python-3.x - 如果我想将 Google 表格下载到本地 CSV,是否需要为 Google API v4 定义“redirect_uris”?
- java - 是否可以使用 Kafka 将多个任务组合成一个数据条目?