Python-DM用户不和谐机器人 [英] Python - DM a User Discord Bot

查看:157
本文介绍了Python-DM用户不和谐机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python处理User Discord Bot.如果漫游器所有者键入!DM @user,则漫游器将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 来获取目标用户,以及仅关键字参数作为发送给他们的可选消息:

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")


对于discord.py的1.0+较新版本,应使用send而不是send_message


For the newer 1.0+ versions of discord.py, you should use send instead of 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")

这篇关于Python-DM用户不和谐机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆