Python discord bot send message in a channel

Viewed 119

I'm trying to run a very simple program: it should check for a string in a link, and in case of a true or false result it should send a different message to a discord channel. The part that verifies the link works, the problem is the part that sends the message, I tried to do it again in different ways but I can't get it to work, this is the code:

@client.command()
async def check_if_live():
    global live
    contents = requests.get('https://www.twitch.tv/im_marcotv').content.decode('utf-8')
    channel = client.get_channel(1005529902528860222)
    if 'isLiveBroadcast' in contents: 
        print('live')
        live = True
        await channel.send('live')
    else:
        print('not live')
        live = False
        await channel.send('not live')

In other parts of the code where I send a message in response to a command using await message.channel.send("message")everything works as expected.

I have done a lot of research but can't find anything useful.

*edit: With the code above the error is 'NoneType' object has no attribute 'send' on the line await channel.send('not live'), if i try to do as suggested by Artie Vandelay inserting await client.fetch_channel(1005529902528860222) before channel = client.get_channel(1005529902528860222) on the inserted line I have an error 'NoneType' object has no attribute 'request'.

Since I think that at this point it may have a certain value, I also insert the code sections about the client variable.

client = discord.Client()
load_dotenv()
TOKEN = os.getenv('TOKEN')
#all bot code
client.run(TOKEN)

I also tried this:

bot = commands.Bot(command_prefix='!')
load_dotenv()
TOKEN = os.getenv('TOKEN')
@bot.command()
#all bot code
bot.run(TOKEN)

Both methods return errors

2 Answers

Double check if your channel id is correct.

Also try using

.text

instead of

.content.decode('utf-8')

if it still does not work, please send your error message, so we can see what is wrong.

I answer my question because I found a solution, instead of using @bot.command I used @bot.event async def on_ready(), and now it works, I hope this answer can be useful to someone else who has this same problem.

Related