【问题标题】:Discord.py bot: how would I make my discord bot send me responses to a command that users use in DMs e.g. for a survey?Discord.py 机器人:如何让我的不和谐机器人向我发送对用户在 DM 中使用的命令的响应,例如进行调查?
【发布时间】:2019-03-09 02:54:53
【问题描述】:

所以,我想让我的不和谐机器人有一个调查命令,如果用户说“#survey”之类的话,那么机器人会向他们发送问题。然后我想这样做,以便机器人通过 DM 将响应(用户使用 DM 中的命令进行响应)发送给我?这可能吗?

我知道当机器人在 discord 服务器中使用命令时如何让他们成为用户,但它发送给我的响应部分我无法理解。

我是 discord.py 的新手,但在询问是否能找到任何相关内容之前,我已经浏览了文档。

这也是我在这个网站上的第一个问题,我刚注册,如果写得不好,请原谅。

提前致谢!

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    我认为您遇到问题的部分是捕获响应。 discord.py overloads the function(args, *, kwargs) 命令语法,因此* 之后的单个参数是消息其余部分的文本。

    from discord.ext.commands import Bot
    
    bot = Bot('#')
    
    my_user_id = "<Your ID>"  
    # You can get your id through the discord client, after activating developer mode.
    
    @bot.command(pass_context=True)
    async def survey(ctx):
        await bot.send_message(ctx.message.author, "What's your name?")
    
    @bot.command(pass_context=True)
    async def respond(ctx, *, response):
        owner = await bot.get_user_info(my_user_id)
        await bot.send_message(owner, "{} responded: {}".format(ctx.message.author.name, response))
    
    bot.run("token")
    

    【讨论】:

    • 谢谢!我现在明白了。
    【解决方案2】:
    @client.event
    async def on_message(message):
        if message.channel.is_private: 
             #If any of the users DM the bot, the bot will send you the message in your DMS
             owner = client.get_member("id_to_send")
             await client.send_message(owner,f"{message.author}": {message.content})
        await client.process_commands(message)
    @client.command(pass_context=True)
    async def survey(ctx):
        await client.send_message(ctx.message.author,"The question you want to ask.")
    

    这可以正常工作:-)

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 2020-07-28
      • 2022-10-13
      • 2021-06-13
      • 2021-05-08
      • 2018-07-21
      • 2021-11-15
      • 2021-12-23
      相关资源
      最近更新 更多