我想通过Discord机器人将DM发送给“ on_message”函数中收到的消息的作者。我怎样才能做到这一点?

时间:2020-03-01 10:04:34

标签: python python-3.x discord discord.py

from discord.ext import commands
load_dotenv("token.env")
token = os.getenv("Token")
bot = commans.Bot(command_prefix="!")

@bot.event
async def on_message(message):
     if message.author == bot.user:
          return
     if message.content.startswith("!"):
          await bot.process_commands(message)
          return
     else:
          author = message.author
          await message.channel.send(message.content)

如何更改第15行中的代码以将消息发送给“作者”?

2 个答案:

答案 0 :(得分:0)

您可以为此使用Member.create_dm()。您可以按以下方式修改代码:

from discord.ext import commands
load_dotenv("token.env")
token = os.getenv("Token")
bot = commands.Bot(command_prefix="!")

@bot.event
async def on_message(message):
     if message.author == bot.user:
          return
     if message.content.startswith("!"):
          await bot.process_commands(message)
          return
     else:
          c = await message.author.create_dm()
          await c.send(message.content)

您还误记了commands中的bot = commands.Bot(...:)

答案 1 :(得分:0)

您可以使用await ctx.author.send(),以便谁运行该命令都可以将其发送到dms中

from discord.ext import commands
load_dotenv("token.env")
token = os.getenv("Token")
bot = commands.Bot(command_prefix="!")

@bot.event
async def on_message(message):
     if message.author == bot.user:
          return
     if message.content.startswith("!"):
          await bot.process_commands(message)
          return
     else:
          author = message.author
          await ctx.author.send(message.content)