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行中的代码以将消息发送给“作者”?
答案 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)