尝试发送不和谐消息时收到语法错误

时间:2020-10-08 00:38:57

标签: python discord discord.py

我无法运行该程序,而只是出现语法错误。我正在尝试向特定频道发送消息,但是ch变量由于某种原因呈红色亮起。我对此并不陌生,我也不是很好。

ch = client.get_channel(12345642256905728)
await ch.send('hello')

3 个答案:

答案 0 :(得分:1)

必须从函数中调用

await。如果您不是从函数中调用await,则会出现以下错误:

>>> import asyncio
>>> await asyncio.sleep(1)
File "<stdin>", line 1
    await asyncio.sleep(1)
                ^
SyntaxError: invalid syntax

答案 1 :(得分:0)

为了使用“等待”,您必须在异步函数内部使用它。所以你可以这样:

async def sendMessage(message):
    ch = client.get_channel(12345642256905728)
    await ch.send(str(message))

“ str(message)”只是一项安全预防措施,因为使用ch.send()时需要发送一个字符串。

答案 2 :(得分:0)

import discord 
from discord.ext import commands
from discord.utils import get

bot = commands.Bot(command_prefix='Your perfix', description="Your description")

# You need to do @bot.command() because you have the bot = commands.Bot if you don't like it you can change it to client
@bot.command()
async def test(ctx):
    # Ctx stands for context and where the message came from
    ctx.channel.send("What you want the bot to send after you did your prefix command")
    # For example if your bots prefix is ! you need to do !test for it