【问题标题】:How to send private message to member in on_member_join() discord.py?如何在 on_member_join() discord.py 中向成员发送私人消息?
【发布时间】:2020-06-16 22:36:12
【问题描述】:
这就是我所拥有的:
@client.command(pass_context=True)
@client.event
async def on_member_join(ctx, member):
print(f'{member} has joined a server.')
await ctx.send(f"Hello {member}!")
await ctx.member.send(f"Welcome to the server!")
我需要机器人在他加入时发送一条包含规则和命令列表的私人消息。
请帮忙!
【问题讨论】:
标签:
python
python-3.x
discord.py
discord.py-rewrite
【解决方案1】:
我不知道发生了什么,从一天到下一天,机器人停止向新成员发送欢迎消息。但我终于能够解决它。
我只需要添加这两行代码。 intents = discord.Intents()intents.members = TrueRead
import discord
from discord.ext import commands
#try add this
intents=intents=discord.Intents.all()
#if the above don't work, try with this
#intents = discord.Intents()
#intents.members = True
TOKEN = 'your token'
bot=commands.Bot(command_prefix='!',intents=intents)
#Events
@bot.event
async def on_member_join(member):
await member.send('Private message')
@bot.event
async def on_ready():
print('My bot is ready')
bot.run(TOKEN)
【解决方案2】:
事件on_member_join() 仅接受成员作为有效参数(请参阅doc)。因此,您尝试执行的操作:on_member_join(ctx, member),将无法正常工作。你需要改用这个:on_member_join(member)。
如果你按如下方式使用事件:
@client.event
async def on_member_join(member):
await member.send('Private message')
您可以直接向加入服务器的成员发送消息。因为您使用此事件获得了member 对象。