Reputation: 399
I'm writing a discord bot and in my background task, client.private_channels
and member.dm_channel
are showing up as None
. Here is my code where I create a background task.
# run this code when client is ready
def my_run_task():
client.loop.create_task(BackgroundManager())
client.run(_token)
Here is where I defined BackgroundManager()
:
async def BackgroundManager():
await client.wait_until_ready()
await SendDM(ID)
and finally here is SendDM()
:
async def SendDM(ID):
member = client.guilds[0].get_member(int(ID))
DM = member.dm_channel
PC = client.private_channels
await member.dm_channel.send('DM has been sent.')
Please note that ID
is a hard coded string which I switched out for privacy sake. It is a valid ID and returns the correct member object.
DM
shows up as None
and PC
is an empty list. How come I can't access the user's dm_channel?
Upvotes: 0
Views: 391
Reputation: 61052
The most likely explanation is that your bot either hasn't exchanged DMs with the user, or the DM channel was not retrieved when the bot started. Either way, you can send
to the member directly, which will handle creating or retrieving the DM channel
await member.send('DM has been sent.')
Upvotes: 2