Search code examples
pythonfilediscordbotsqr-code

Sending qr codes to discord user


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.


Solution

  • 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'))