【发布时间】:2019-04-09 20:04:52
【问题描述】:
On the bottom left it says that I'm using Python 3.6 64-bit 我试图创建一个不和谐的机器人并从示例中复制并粘贴此代码,但由于某种原因它似乎不起作用。任何帮助将不胜感激
#Python 3.6
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(commands_prefix='#')
client = discord.Client()
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hello'):
msg = 'Hello {0.author.mention}'.format(message)
await client.send_message(message.channel, msg)
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run('token')
我得到的错误是:
[Running] python -u "/Users/brady/Documents/tempCodeRunnerFile.py"
File "/Users/brady/Documents/tempCodeRunnerFile.py", line 12
async def on_message(message):
^
SyntaxError: invalid syntax
[Done] exited with code=1 in 0.092 seconds
我正在尝试在 Visual Studio Code 中运行此代码
【问题讨论】:
-
完整的错误信息是什么?这里的缩进是否与您尝试运行的代码的缩进相同(特别是
client.run行)?与您的问题无关,但您为什么同时使用Bot和Client? -
我已编辑帖子以显示错误消息。此外,client.run 行上的缩进已修复以匹配实际代码。我不确定机器人和客户端,因为我已经复制并粘贴了这段代码,如果你能帮助我弄清楚使用哪个会有所帮助。再次感谢@PatrickHaugh
-
你确定你使用的是 python 3.6 吗?试试
python -V -
我添加了一张代码图片,显示我的版本是 3.6 @PatrickHaugh
-
我已经测试了你的代码,它在我的机器上运行良好,除了将
commands_prefix更改为command_prefix。您是否尝试过保存文件并直接从命令提示符/终端运行它?
标签: python-3.x discord.py