Discord Bot(Python)的语法无效

时间:2018-11-06 12:16:54

标签: python-3.x discord.py

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代码中运行此代码

1 个答案:

答案 0 :(得分:0)

您使用

bot = commands.Bot(commands_prefix='#')

因此,要使其正常工作,您将需要使用

@bot.command()

否则它将无法正常工作。

也是

Client = discord.Client()

这里的部分没用