Discord Bot 如何获取发件人
Discord Bot how get get sender
我正在制作一个与 Python 不和谐的机器人,我想要一个命令,例如有人打字
User: !hello
Bot: Hello User#1234
我在网上找遍了都没有找到。我尝试了很多方法,其中 none 有效
如果您正在使用 discord.py
库,则消息有一个作者变量,您可以使用 message.author
访问此 returns User
或 Member
对象。
您可以使用 message.author.name
(这将是 return 用户)获取这些的显示名称,而 message.author
将是 return 用户#1234
discord.py
库也有很好的文档 here.
如果您使用的是 discord.py
,请查看此示例脚本:
https://github.com/Rapptz/discord.py/blob/async/examples/reply.py
@client.event
async def on_message(message):
if message.content.startswith('!hello'):
msg = 'Hello {0.author.mention}'.format(message)
await client.send_message(message.channel, msg)
如果您想添加更多命令,最好使用 @*.command()
装饰器,请参阅 the other examples。基本上上面例子的装饰器版本应该看起来像这样:
bot = commands.Bot(command_prefix='!')
@bot.command(pass_context=True)
async def hello(ctx):
"""Answers a !hello command"""
msg = 'Hello {0.author.mention}'.format(ctx.message)
await bot.say(msg)
我正在制作一个与 Python 不和谐的机器人,我想要一个命令,例如有人打字
User: !hello
Bot: Hello User#1234
我在网上找遍了都没有找到。我尝试了很多方法,其中 none 有效
如果您正在使用 discord.py
库,则消息有一个作者变量,您可以使用 message.author
访问此 returns User
或 Member
对象。
您可以使用 message.author.name
(这将是 return 用户)获取这些的显示名称,而 message.author
将是 return 用户#1234
discord.py
库也有很好的文档 here.
如果您使用的是 discord.py
,请查看此示例脚本:
https://github.com/Rapptz/discord.py/blob/async/examples/reply.py
@client.event
async def on_message(message):
if message.content.startswith('!hello'):
msg = 'Hello {0.author.mention}'.format(message)
await client.send_message(message.channel, msg)
如果您想添加更多命令,最好使用 @*.command()
装饰器,请参阅 the other examples。基本上上面例子的装饰器版本应该看起来像这样:
bot = commands.Bot(command_prefix='!')
@bot.command(pass_context=True)
async def hello(ctx):
"""Answers a !hello command"""
msg = 'Hello {0.author.mention}'.format(ctx.message)
await bot.say(msg)