2017-03-03 7 views
0

ボットに一連の定義済みメッセージから数秒ごとにメッセージを送信しようとしています。discord.pyでDiscordボットを作成しようとしたときにエラーが発生しました

import discord 
import asyncio 
import random 

client = discord.Client() 

async def background_loop(): 
    await client.wait_until_ready() 
    while not client.is_closed: 
     channel = client.get_channel("channel id here") 
     messages = ["Hello!", "How are you doing?", "Howdy!"] 
     await client.send_message(channel, random.choice(messages)) 
     await asyncio.sleep(120) 

client.loop.create_task(background_loop()) 
client.run("discord token here") 

私はあまりにもそれを実行しようとすると、私はコンソールでこのエラーを取得し、チャットにメッセージが送信されません。

/usr/bin/python3.5 /root/PycharmProjects/untitled/Loop.py 
Task exception was never retrieved 
future: <Task finished coro=<background_loop() done, defined at /root/PycharmProjects/untitled/Loop.py:8> exception=InvalidArgument('Destination must be Channel, PrivateChannel, User, or Object. Received NoneType',)> 
Traceback (most recent call last): 
    File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step 
    result = coro.send(None) 
    File "/root/PycharmProjects/untitled/Loop.py", line 13, in background_loop 
    await client.send_message(channel, random.choice(messages)) 
    File "/usr/local/lib/python3.5/dist-packages/discord/client.py", line 1145, in send_message 
    channel_id, guild_id = yield from self._resolve_destination(destination) 
    File "/usr/local/lib/python3.5/dist-packages/discord/client.py", line 289, in _resolve_destination 
    raise InvalidArgument(fmt.format(destination)) 
discord.errors.InvalidArgument: Destination must be Channel, PrivateChannel, User, or Object. Received NoneType 
+0

'discord.errors.InvalidArgumentをChannelオブジェクトを取得するヘルパー関数を記述することによって、これを修正:デスティネーションチャンネル、PrivateChannel、ユーザー、またはオブジェクトでなければなりません。 Received NoneType' リクエストを送信する場所を確認します。正しいチャンネルIDを持っていることを確認しますか? – CynePhoba12

+0

あなたは正しいと思われる、問題は私がチャンネルのIDは、チャネルの名前を参照していたと思った。すべて確定しました。 – Museman

答えて

-1

私はクライアントから

def get_channel(channels, channel_name): 
    for channel in client.get_all_channels(): 
     print(channel) 
     if channel.name == channel_name: 
      return channel 
    return None 

client = discord.Client() 
general_channel = get_channel(client.get_all_channels(), 'general') 
await client.send_message(general_channel, 'test msg') 
+0

まだNoneTypeを返す – mast3rd3mon

関連する問題