Python - DM用户不和谐机器人

问题描述 投票:0回答:1

我正在使用Python中的用户Discord机器人。如果机器人所有者键入!DM @user,那么机器人将DM所有者提到的用户DM。

@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)
python-3.x bots discord discord.py dm
1个回答
0
投票

最简单的方法是使用discord.ext.commands扩展。在这里,我们使用converter来获取目标用户,并使用keyword-only argument作为可选消息来发送它们:

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")
© www.soinside.com 2019 - 2024. All rights reserved.