【问题标题】:Copy string to clipboard after button click单击按钮后将字符串复制到剪贴板
【发布时间】:2021-11-24 14:07:41
【问题描述】:

单击按钮后,我的 Discord 机器人应将字符串复制到我的剪贴板。交互每次都失败:

import clipboard
from discord_components import Button, DiscordComponents

@client.command()
async def command(ctx, *args):
    # do some stuff
    await ctx.send('message', components=[Button(label='Copy to clipboard', custom_id="copy_btn")])

@client.event
async def on_button_click(interaction):
    if interaction.custom_id == 'copy_btn':
        try:
            print('try to copy string')
            clipboard.copy('my string')
            await ctx.send('string copied')
        except Exception as e:
            print('failed to copy string to clipboard\n' + e)

当我使用command 时,我的机器人会向我发送带有按钮的消息。单击该按钮会将try to copy string 打印到我的控制台中,但不会复制“我的字符串”。养蜂也不例外。

pip install clipboard 成功。 pip install discord-components也一样

【问题讨论】:

    标签: python discord discord.py clipboard


    【解决方案1】:

    我在这里没有看到任何问题,除了这里可能存在一些深刻的误解:您的代码与运行机器人的机器的剪贴板交互,而不是与单击按钮的客户端交互 - discord 不提供任何这样的能力。

    您应该能够通过将try 块中对send 的调用更改为

    ,轻松判断它是否正常工作
    await ctx.send(clipboard.paste())
    

    【讨论】:

    • 啊,是的,这是有道理的。它被复制到运行机器人的主机上
    猜你喜欢
    • 2014-04-30
    • 2012-09-28
    • 2021-12-10
    • 2013-11-21
    • 2010-10-09
    • 1970-01-01
    • 2017-09-17
    相关资源
    最近更新 更多