Khaotic Codes
Khaotic Codes

Reputation: 17

Discord.py dm command

@bot.command()
async def dm(ctx, member: discord.Member):
  id = member.id
  for a in range(5):
    await bot.send_message(id, "")

ive done this my error is :

raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Bot' object has no attribute 'message'

Upvotes: 0

Views: 1261

Answers (3)

Aditya Tomar
Aditya Tomar

Reputation: 1639

Just add user = await bot.fetch_user(id) and then do await user.send("Hello").

@bot.command()
async def dm(ctx, member: discord.Member):
  id = member.id
  user = await bot.fetch_user(id)
  for a in range(5):
    await user.send("Hello")

Upvotes: 0

Cohen
Cohen

Reputation: 2415

This is the important part of your error, and it basically shows already what it is about; The bot has no attribute of message, in which it doesn't and should be member.send in your case. You also don't need to get the id of the member.

AttributeError: 'Bot' object has no attribute 'message'

By re-writing your command, here are those errors fixed

@bot.command()
async def dm(ctx, member: discord.Member):
  for a in range(5):
    await member.send("text")

You can also add more specific detail to the command, in this example if you don't include a member it won't cause an error, or if the member was not reachable, it would handle the error.

@bot.command()
async def dm(ctx, member: discord.Member=None):
  if member == None:
    await ctx.send('Mention a member')
    return
  try:
    for a in range(5):
      await member.send("text")

  except commands.CommandInvokeError:
      await ctx.send("Couldn't send message to user")

Upvotes: 1

elf
elf

Reputation: 46

Try using member.send instead of bot.send_message.

Upvotes: 0

Related Questions