How to make my discord.py rewrite bot send a Private Message to the message author

会有一股神秘感。 提交于 2020-01-30 11:56:10

问题


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!".


回答1:


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




回答2:


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!")


来源:https://stackoverflow.com/questions/55935047/how-to-make-my-discord-py-rewrite-bot-send-a-private-message-to-the-message-auth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!