Copy string to clipboard after button click
My Discord bot should copy a string to my clipboard after a button is clicked. The interaction fails everytime:
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)
My bot sends me a message with a button when using command
. Clicking that button prints try to copy string
into my console, but 'my string' is not copied. There is no exception beeing raised.
pip install clipboard
was successful.
Same for pip install discord-components
I don't see any issues here other than that there might potentially be some deep misunderstanding here: your code interacts with clipboard of the machine that is running the bot, not the client that clicks the button - discord does not provide any such capability.
You should be able to easily tell if it worked properly by changing the call to send
in the try
block to
await ctx.send(clipboard.paste())