Telepathic_Squid
Telepathic_Squid

Reputation: 3

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. I am new to this, and I am not very good.

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

Upvotes: 0

Views: 334

Answers (3)

THEMEGACODERS
THEMEGACODERS

Reputation: 93

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 

Upvotes: 0

bguerra
bguerra

Reputation: 41

In order to use "await", you have to use it inside of an asynchronous function. So you could so something like this:

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().

Upvotes: 0

Aaj Kaal
Aaj Kaal

Reputation: 1304

await has to be called from a function. If you are not calling await from within a function you will get error as below:

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

Upvotes: 1

Related Questions