[英]I get a syntax error when I try to send a discord message
I cannot run the program and I just get a syntax error.我无法运行该程序,只是出现语法错误。 I am trying to send a message to a specific channel, but the ch variable for some reason lights up red.
我正在尝试向特定频道发送消息,但 ch 变量由于某种原因亮起红色。 I am new to this, and I am not very good.
我是新手,我不是很好。
ch = client.get_channel(12345642256905728)
await ch.send('hello')
await has to be called from a function. If you are not calling await from within a function you will get error as below: await 必须从 function 中调用。如果您不是从 function 中调用 await,您将收到如下错误消息:
>>> import asyncio
>>> await asyncio.sleep(1)
File "<stdin>", line 1
await asyncio.sleep(1)
^
SyntaxError: invalid syntax
In order to use "await", you have to use it inside of an asynchronous function. So you could so something like this:为了使用“await”,你必须在异步 function 中使用它。所以你可以这样:
async def sendMessage(message):
ch = client.get_channel(12345642256905728)
await ch.send(str(message))
The "str(message)" is just a safety precaution since you need to send a string when using ch.send(). “str(message)”只是一种安全预防措施,因为在使用 ch.send() 时需要发送一个字符串。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.