1
votes

I have made a Discord bot for a game's Discord server. I'm using the discord.py rewrite version and I want to send a private message to the author of the message.

I've tried other codes on the internet which includes some "@bot" code, but it always comes up with the error

"Name 'bot' is not defined"

and if I try send_message it says

"Client object has no attribute 'send_message'"

My code:

#I've tried this...

@bot.command(pass_context=True)
async def poke(ctx, message):
    await client.send_message(ctx.message.author, 'boop')

#but it comes up with the error "Name 'bot' is not defined" and stuff like that

For example, I want to create a command "!messageme", and if a user executes the command I expect the bot to private message the author of the message saying "Just messaged you!".

If Pierce#9255 executes the command in the server, the bot should private message him saying "Just messaged you!".

2
Generally when people post commands here they don't post all of the other code necessary for that command, because it's generally the same between bots. You can find some example of full bots in the discord.py source, or in these answers. - Patrick Haugh

2 Answers

2
votes

Did you define your bot variable? If not then do this:

bot = commands.Bot(command_prefix='!') # Just add your desired prefix there.

# sending dm
@bot.command()
async def poke(ctx):
    await ctx.author.send('boop!')

Also, if you still confused then just give this yt tutorial a try: -NiByO6h7Ck

1
votes

Firstly you have to define the Bot. The you will have to DM a user.

bot = commands.Bot(command_prefix='your_prefix')

@bot.command()
async def hello(ctx):
    user = ctx.author
    await user.send("Hello!")