Trying to send message to a channel every hour
You are doing the following wrong:
channel = bot.get_channel = xxx
The thing is that bot.get_channel is a function. This means that you actually need to do the following:
channel = bot.get_channel(xxx)
Why it goes wrong is that you are not correctly executing the bot.get_channel() function. Thus the value of channel becomes xxx. But in order to send a message to a channel you need the channel object. You can only get this by executing the function correctly.
So if you did:
channel = bot.get_channel(id)
await channel.send('Your message')
Then bot.get_channel(id) returned a channel object which you can assign to the variable channel. You can later use this to send a message to that channel.
Another thing to note is that bot.channel is not the same as the channel variable. So if you had a channel object in channel. You cannot send something using bot.channel.send(). You need to do channel.send().
It is very usefull to read the documentation: https://discordpy.readthedocs.io/en/latest/api.html?highlight=get_channel#discord.Client.get_channel
primitiveProgrammer
Updated on September 14, 2022Comments
-
primitiveProgrammer over 1 year
I'm trying to create a simple Discord bot where it send a channel a message every hour, when I test it in the terminal it works fine, prints 'test' every 2 seconds. But when i want to add the line 'await bot.channel.send('here')' it only prints 'test' once in terminal and nothing in the discord channel
import discord,asyncio,os from discord.ext import commands, tasks token = 'xxx' bot = commands.Bot(command_prefix='.') @bot.event async def on_ready(): change_status.start() print('bot in active') @tasks.loop(seconds=2) async def change_status(): channel = bot.get_channel = xxx await bot.change_presence(activity=discord.Game('online')) print('test') await bot.channel.send('here') bot.run(token)
-
RafalS over 4 yearsHave you tried running
change_status
just once outside@tasks.loop
? -
primitiveProgrammer over 4 yearsYes it would just print 'test' once, I need it due to repeat this function every hour.
-
RafalS over 4 yearshmm, maybe you're missing
change_status.start()
? -
primitiveProgrammer over 4 yearsWhere would I add that?
-
Patrick Haugh over 4 yearsAssuming
channel
refers to the channel you want to send the message to, you should use that object:await channel.send(...)
-
primitiveProgrammer over 4 yearsI've tried that already, for some reason this now stops the loop from working.
-
-
primitiveProgrammer over 4 yearsThank you for the in depth explanation, I made those changes but yet it still wont work. I've even added a print statement in above the channel = bot.get_channel(id) to see if the function is even being called, it is being called but just wont send the message to the discord server.
-
Deru over 4 yearsSorry I missed the word 'await' in the last one. If you dont add the word 'await' in front of channel.send() it will fail. As channel.send() is a coroutine. If you still have problems then it could be that you are giving an incorrect id.
-
primitiveProgrammer over 4 yearsYeah I did that, I also printed channel to see what it would return, it returns None. I'm not sure why because I've copied the ID from Discord. I clicked on the channel >> server settings>> widgets then I copied the channel ID stright from there. Am I doing something wrong?
-
Deru over 4 yearsNone means that it doesnt have a value. The id that you find in server settings is the id of the server not the channel. This is the reason why the value of channel is None. As the id is incorrect. To get the channel id. First enable developer settings in discord by doing the following: Settings > Appearance > Check developer mode. After that right click the channel and copy id.
-
primitiveProgrammer over 4 yearsWow thank you so much, it worked. I was banging my head trying to figure out what was wrong, I need to pay more attention to detail. Thanks again.