Python (discord.py)Client.send_message()不';不要发信息

Python (discord.py)Client.send_message()不';不要发信息,python,python-3.x,Python,Python 3.x,我正在使用discord.py创建聊天机器人。到目前为止,这只是一个测试,所有代码都在一个文件中 bot连接到服务器并侦听以感叹号“!”开头的消息 然后根据命令调用两个函数中的一个。到目前为止,bot按预期工作 client = discord.Client() @client.async_event def on_message(message): author = message.author if message.content.startswith('!test'):

我正在使用discord.py创建聊天机器人。到目前为止,这只是一个测试,所有代码都在一个文件中

bot连接到服务器并侦听以感叹号“!”开头的消息

然后根据命令调用两个函数中的一个。到目前为止,bot按预期工作

client = discord.Client()

@client.async_event
def on_message(message):
    author = message.author
    if message.content.startswith('!test'):
        print('on_message !test')
        test(author, message)
    if message.content.startswith('!quit'):
        print('on_message !quit')
        quit(author, message)
这就是它变得奇怪的地方。当调用quit函数时,程序终止。当调用test函数时,它什么也不做。它甚至不打印字符串

def test(author, message):
    print('in test function')
    yield from client.send_message(message.channel, 'Hi %s, i heard you.' % author)

def quit(author, message):
    sys.exit()

我错过了什么?非常感谢您的帮助。

我遇到了这个问题,这似乎解决了它。如果您使用的是python 3.5:

@client.async_event
def on_message(message):
应改为:

@client.event
async def on_message(message):

而的
收益率应更改为
wait
。如果您没有使用Python3.5,我建议升级到它。希望这能起作用。

我遇到了这个确切的问题,这似乎解决了它。如果您使用的是python 3.5:

@client.async_event
def on_message(message):
应改为:

@client.event
async def on_message(message):

收益率应更改为
wait
。如果您没有使用Python3.5,我建议升级到它。希望这能起作用。

我通过使一些函数异步并将send_消息作为一个共同例程,使您的脚本正常工作。当然,我使用的是Python3.5,所以如果您使用的是Python3.4,您可能需要做一些不同的事情

认为,您发送的消息没有被发送的原因是,您的程序中没有任何程序阻塞各种功能(不使用wait),而这些功能可能会导致您的机器人冻结。您可以在discord.py文档的一节中了解更多信息

client = discord.Client()

@client.async_event
async def on_message(message):
    author = message.author
    if message.content.startswith('!test'):
        print('on_message !test')
        await test(author, message)
    if message.content.startswith('!quit'):
        print('on_message !quit')
        quit(author, message)
async def test(author, message):
    print('in test function')
    await client.send_message(message.channel, 'Hi %s, i heard you.' % author)

def quit(author, message):
    sys.exit()

我通过使一些函数异步并将send_消息作为一个共同例程来实现脚本的功能。当然,我使用的是Python3.5,所以如果您使用的是Python3.4,您可能需要做一些不同的事情

认为,您发送的消息没有被发送的原因是,您的程序中没有任何程序阻塞各种功能(不使用wait),而这些功能可能会导致您的机器人冻结。您可以在discord.py文档的一节中了解更多信息

client = discord.Client()

@client.async_event
async def on_message(message):
    author = message.author
    if message.content.startswith('!test'):
        print('on_message !test')
        await test(author, message)
    if message.content.startswith('!quit'):
        print('on_message !quit')
        quit(author, message)
async def test(author, message):
    print('in test function')
    await client.send_message(message.channel, 'Hi %s, i heard you.' % author)

def quit(author, message):
    sys.exit()

天哪,向你问好。我不熟悉python,但我尝试使用在discord.py重写之前编写的较旧github bot。这修复了我的命令行错误,并且能够使bot处于活动状态。再次非常感谢你!!!天哪,向你问好。我不熟悉python,但我尝试使用在discord.py重写之前编写的较旧github bot。这修复了我的命令行错误,并且能够使bot处于活动状态。再次非常感谢你!!!