简体   繁体   English

尝试发送 discord 消息时出现语法错误

[英]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.

相关问题 我的 discord 机器人在我尝试发送消息时出错 - My discord bot gives an error when I try send a message Python/Discord 当我尝试让 python 获取不和谐用户的用户 ID 时,它什么也不做,没有错误消息,也没有输出 - Python/Discord When I try to make python get the userID of a discord user it just does nothing, no error message and no output AttributeError: 'NoneType' object 没有属性 'send',当我尝试使用机器人向特定 discord 通道发送消息时 - AttributeError: 'NoneType' object has no attribute 'send' ,when I try to send a message with a bot to a specific discord channel 当我尝试使用“”字符进行打印时,出现语法错误 - When I try to use the “”" thing for print, i get a syntax error 在 Discord 中发送消息时,我无法收到响应消息? - I am not able to receive a response message when send a message in Discord? discord.py - 当我尝试更改用户名时出现这样的错误,有人知道如何解决吗? - discord.py - when i try to change username i get such an error does anyone know how to fix it? 当我尝试运行我的机器人时,为什么会出现这个奇怪的错误? (discord.py) - Why do i get this weird error when i try to run my bot? (discord.py) 我无法运行 ipython,当我尝试运行它时出现语法错误 - I can't run ipython and when i try to run it i get syntax error 我尝试adblocker build时出现错误消息 - Error message when I try adblocker build discord.py 如何让机器人在特定用户类型时发送消息? - discord.py how do I get a bot to send a message when a specific user types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM