Python - DM 一个用户 Discord 机器人
问题描述
我正在使用 Python 开发 User Discord Bot.如果 bot 所有者键入 !DM @user
,那么 bot 将 DM 所有者提到的用户.
I'm working on a User Discord Bot in Python .If the bot owner types !DM @user
then the bot will DM the user that was mentioned by the owner.
@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
扩展.这里我们使用 converter 来获取目标用户,和一个 keyword-only 参数发送给他们的可选消息:
The easiest way to do this is with the discord.ext.commands
extension. Here we use a converter to get the target user, and a keyword-only argument as an optional message to send them:
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")
相关文章