Python-DM用户不和谐机器人

发布于 2021-01-29 15:01:28

我正在使用Python处理User Discord Bot。如果机器人所有者键入内容,!DM @user则该机器人将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)
关注者
0
被浏览
85
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    最简单的方法是使用discord.ext.commands扩展程序。在这里,我们使用转换器来获取目标用户,并使用仅关键字参数作为可选消息来发送目标用户:

    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

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看