【问题标题】:Invalid syntax for discord bot (python)不和谐机器人的语法无效(python)
【发布时间】: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 行)?与您的问题无关,但您为什么同时使用BotClient
  • 我已编辑帖子以显示错误消息。此外,client.run 行上的缩进已修复以匹配实际代码。我不确定机器人和客户端,因为我已经复制并粘贴了这段代码,如果你能帮助我弄清楚使用哪个会有所帮助。再次感谢@PatrickHaugh
  • 你确定你使用的是 python 3.6 吗?试试python -V
  • 我添加了一张代码图片,显示我的版本是 3.6 @PatrickHaugh
  • 我已经测试了你的代码,它在我的机器上运行良好,除了将commands_prefix 更改为command_prefix。您是否尝试过保存文件并直接从命令提示符/终端运行它?

标签: python-3.x discord.py


【解决方案1】:

你用过

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

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

@bot.command()

否则它不会工作。

还有

Client = discord.Client()

这里的部分没用

【讨论】:

    猜你喜欢
    • 2021-10-30
    • 2021-05-27
    • 2021-07-15
    • 2021-03-29
    • 2021-07-11
    • 2021-03-25
    • 2018-08-13
    • 1970-01-01
    • 2021-02-28
    相关资源
    最近更新 更多