Home > Net >  Sending qr codes to discord user
Sending qr codes to discord user

Time:11-15

My discord bot creates qr-codes after a certain command. However I am unable to send this qr-code to the user as message:

import qrcode

def create_qr_code(string : str):

    qr = qrcode.make(string)
    return qr


# sending qr to user
qr_code = create_qr_code('some text')
# check if qr_code is None
print(qr_code)
await ctx.send(file=discord.File(fp=qr_code))

My print statement returns something like

<qrcode.image.pil.PilImage object at 0x000001BD735FCF28>,

which is fine and shows me that the creation of the qr code was successful. I wonder why sending it doesn't seem to work.

CodePudding user response:

You can use a package called qrcode and then use this code:

async def qrcode(self, ctx, *, url):
        qr = qrcode.QRCode(
            version=1,
            error_correction=qrcode.constants.ERROR_CORRECT_H,
            box_size=10,
            border=4,
        )
        qr.add_data(str(url))
        qr.make(fit=True)
        img = qr.make_image(fill_color="black",
                            back_color="white").convert('RGB')
        img.save('qrcode.png')
        await ctx.send(file=discord.File('qrcode.png'))

btw if you want to continue using PyQRCode, looking at the pypi documentation it looks like you can do:

qr_code.png('code.png', scale=6, module_color=[0, 0, 0, 128], background=[0xff, 0xff, 0xcc])

To save it.

CodePudding user response:

Actually I found a working solution myself using this solution:

First of all I create a qr code and return this object

import qrcode

def create_qr_code(string : str):

    qr_code = qrcode.make(string)
    return qr_code

I can now use BytesIO() to send this qr code as binary file to discord:

import io

def some_other_function():

    qr_code = create_qr_code('my string')

    with io.BytesIO() as image_binary:
        qr_code.save(image_binary, 'PNG')
        image_binary.seek(0)
        await ctx.send(file=discord.File(fp=image_binary, filename='qr.png'))
  • Related