首页 > 解决方案 > Discord.py int 没有属性 send

问题描述

嘿,所以我得到了这个代码

import discord
import asyncio
import random
import os
import time
from discord.ext import tasks, commands


client = discord.Client()

@client.event
async def on_ready():
 print('we have logged in as {0.user}'.format (client))

async def background_loop():
  await client.wait_until_ready()
  while not client.is_closed():
    channel =(814588208091365426)
    messages = ["Test1", "Test2", "Test3"]
    await channel.send(random.choice(messages))
    await asyncio.sleep(60)
    

client.loop.create_task(background_loop())
client.run(os.getenv('token'))

我收到了这个错误:

future: <Task finished name='Task-1' coro=<background_loop() done, defined at main.py:15> exception=AttributeError("'int' object has no attribute 'send'")>
Traceback (most recent call last):
  File "main.py", line 20, in background_loop
    await channel.send(random.choice(messages))
AttributeError: 'int' object has no attribute 'send'

我也尝试添加

return messages

我没有收到错误,但机器人也没有做任何事情。提前致谢

标签: pythondiscorddiscord.py

解决方案


您正在尝试调用sendnumber 上的函数814588208091365426,这是频道 ID,您需要将频道 ID 转换为具有以下内容的频道:

channel = client.get_channel(814588208091365426)

推荐阅读