运行client.fetch_channel时发生AttributeError

时间:2019-07-20 08:48:55

标签: python python-3.x discord.py

我正在尝试使用client.fetch_channel来获取频道,但它以AttributeError: 'NoneType' object has no attribute 'request'的形式给出

我尝试了很多不同的事情,例如使用int(channel_id)

    for submission in reddit.subreddit('all').stream.submissions():
        if os.path.exists('streams/' + str(submission.subreddit) + '.json'):
            with open('streams/' + str(submission.subreddit) + '.json') as sub_data:
                sub_info = json.load(sub_data)
                for channel_id in sub_info['channels']:
                #The error is on this line:
                    channel = await client.fetch_channel(601904205627654146)
                    await channel.send('https://www.reddit.com/r/memes/comments/' + submission)


asyncio.run(restartallstreams())
client.login("nope")

跟踪:

  File "/home/bwac/Desktop/Reddit/versions/1.2.4/stream.py", line 33, in <module>
    asyncio.run(restartallstreams())
  File "/usr/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "/home/bwac/Desktop/Reddit/versions/1.2.4/stream.py", line 27, in restartallstreams
    channel = await client.fetch_channel(601904205627654146)
  File "/home/bwac/.local/lib/python3.7/site-packages/discord/client.py", line 1231, in fetch_channel
    data = await self.http.get_channel(channel_id)
  File "/home/bwac/.local/lib/python3.7/site-packages/discord/http.py", line 158, in request
    async with self.__session.request(method, url, **kwargs) as r:
AttributeError: 'NoneType' object has no attribute 'request'

这应该创建一个频道类并向其发送消息。

1 个答案:

答案 0 :(得分:0)

好吧,如果要获取频道对象,可以使用以下代码:

channel = discord.utils.get(ctx.guild.channels, id=channel_id)

这将为您提供一个通道对象,并允许向该通道发送消息,但是该命令必须在ID为该通道的服务器中使用。