0
votes

I have an example code block below that I am trying to run to send a message to a specific channel. When I run it I get this error: AttributeError: 'NoneType' object has no attribute 'send'. I know the channel id is good. This is pretty straightforward but I am knew to discord.py so I may be overlooking something

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

intents = discord.Intents(guild_messages=True)
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    await client.get_channel(811074101505425418).send("bot is online")

client.run(TOKEN)
3
try client.channel.send("bot is online") - Goion
@Goion AttributeError: 'Client' object has no attribute 'channel' is what I'm getting - Brenden
Recheck your token and check if you have correct permissions. Seems like it cannot authenticate or something - Goion
@Goion I kicked the bot, redid the permissions on the developer page and I'm still not getting any luck... Seems really weird for something so straight forward - Brenden
Does print(client.user.name) print anything? - Goion

3 Answers

0
votes

You can try this

Channel=‘#channelID’
Await channel(‘#your message’)
0
votes

You can give this a try:

@client.event
async def on_ready():
    # get channel by ID and save it in the variable "channel"
    channel = await client.get_channel(811074101505425418)
    # send your message to the channel
    await channel.send('bot is ready')
0
votes

I think your problem is that you define the channel as you use it. This should work:

from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

intents = discord.Intents(guild_messages=True)
client = discord.Client(intents=intents)

@client.event
async def on_ready():
#make a channel variable first
    channel = client.get_channel(811074101505425418)
#if you want to use the message later eg. to delete it you can make a variable too
    message = await channel.send('Bot is online!')#only channel.send because the channel 
#is a variable

client.run(TOKEN)