Search code examples
discorddiscord.pynextcord

How to send DM to all server members (nextcord)


So, I'm planning to make my bot dm all members in the server to remind them to enter the giveaway

@client.command()
@commands.is_owner()
async def dm_all(ctx, *, args=None):
    if args != None:
        members = ctx.guild.members
        for member in members:
            try:
                await member.send(args)
                print("'" + args + "' sent to: " + member.name)

            except:
                print("Couldn't send '" + args + "' to " + member.name)

    else:
        await ctx.channel.send("You didn't provide a arguments.")

this code doesn't work

in terminal it appears like this. it's only try to dm the bot (Hu Tao) and no dm's coming from other members


Solution

  • You do not have intents enabled:

    intents = nextcord.Intents.default()
    

    Now enable members for the intents:

    intents.members = True
    

    Update your client to use the intents:

    client = commands.Bot(prefix, intents=intents)
    

    Also, I'm not forcing you but DMing them all is a bad idea.