【问题标题】:discord.py Sending DM through python consolediscord.py 通过 python 控制台发送 DM
【发布时间】:2021-01-05 12:15:56
【问题描述】:

我正在尝试通过 python 控制台发送消息。现在,当我将其设置为机器人命令时,一切正常(当我执行命令!dm 时,我在 python 控制台中收到提示用户 ID 和我想要 DM 的消息。这一切都有效。但是,我我想做的是让程序在启动后立即提示我。

我当前的代码:

@bot.command(name='dm')
async def messageinput(ctx):
    member = input('Enter Member ID: ')
    message = input('Enter Message: ')
    print(member)
    print(message)
    user = bot.get_user(int(member))
    await user.send(message)

@bot.event
async def on_ready():
    #await verifyloop()
    print(Fore.CYAN + '|------------------------------------------|')
    print(Fore.CYAN + '|--------------Bot is online!--------------|')
    print(Fore.CYAN + '|------------------------------------------|')
    
    #await messageinput()

我该怎么做?

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    要在 on_ready() 函数中使用他们的 ID 获取不和谐成员,请执行以下操作:

    await client.fetch_user(USER_ID)
    

    其中USER_ID 是用户的ID(一个整数值)。


    因此,您的活动将如下所示:

    @bot.event
    async def on_ready():
        print(Fore.CYAN + '|------------------------------------------|')
        print(Fore.CYAN + '|--------------Bot is online!--------------|')
        print(Fore.CYAN + '|------------------------------------------|')
    
        member = int(input('Enter Member ID: '))
        message = input('Enter Message: ')
    
        user = await bot.fetch_user(member)
        await user.send(message)
    

    【讨论】:

    • 就像我说的,我已经让它作为一个机器人命令工作。但是,我希望它在我启动程序时提示我(所以我不必打开不和谐。
    • @ENORMOUZ 给我一点时间来编辑我的答案。
    • @ENORMOUZ 你能试试这个吗?
    【解决方案2】:

    不要使用 print,print 将消息发送到控制台,改用 ctx.send,有关详细信息,请查看 discord.py documentation

    一个例子:

    ctx.send("Sending this to the designated channel on Discord")
    

    【讨论】:

    • 就像我说的,我已经让它作为一个机器人命令工作。但是,我希望它在我启动程序时提示我(所以我不必打开不和谐。
    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 2021-04-16
    • 2021-11-23
    • 1970-01-01
    • 2014-10-08
    • 2021-08-07
    • 1970-01-01
    • 2013-09-10
    相关资源
    最近更新 更多