我正在 Python 中开发一个 User Discord 机器人。如果机器人所有者输入!DM @user
然后机器人会私信所有者提到的用户。
@client.event
async def on_message(message):
if message.content.startswith('!DM'):
msg = 'This Message is send in DM'
await client.send_message(message.author, msg)
最简单的方法是使用discord.ext.commands
扩大。这里我们使用一个转换器 https://discordpy.readthedocs.io/en/rewrite/ext/commands/commands.html#converters获取目标用户,以及仅关键字参数 https://discordpy.readthedocs.io/en/rewrite/ext/commands/commands.html#keyword-only-arguments作为发送给他们的可选消息:
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix='!')
@bot.command(pass_context=True)
async def DM(ctx, user: discord.User, *, message=None):
message = message or "This Message is sent via DM"
await bot.send_message(user, message)
bot.run("TOKEN")
对于较新的 1.0+ 版本的discord.py,您应该使用send
代替send_message
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix='!')
@bot.command()
async def DM(ctx, user: discord.User, *, message=None):
message = message or "This Message is sent via DM"
await user.send(message)
bot.run("TOKEN")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)