首页 > 解决方案 > 不和谐机器人的语法无效(python)

问题描述

在左下角,它说我正在使用 Python 3.6 64 位 我正在尝试创建一个不和谐的机器人并从示例中复制并粘贴了此代码,但由于某种原因它似乎不起作用。任何帮助将不胜感激

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

标签: python-3.xdiscord.py

解决方案


你用过

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

所以要让它工作,你需要使用

@bot.command()

否则它将无法正常工作。

还有

Client = discord.Client()

这里的部分没用


推荐阅读