shayypy / guilded.py

Asynchronous Guilded API wrapper for Python
https://guildedpy.rtfd.io
Other
133 stars 25 forks source link

`message.author.send('some string')` not working #31

Closed kintama48 closed 2 years ago

kintama48 commented 2 years ago

Describe the bug Hi, I am trying to migrate a bot from discord to guilded. The bot generates a QR code and sends it to a member of the guild. The member types $qr in a guild channel and the bot then DM's the QR code to the member. However, the bot fails to DM the member.

To Reproduce This is the on_message event listener I am using.

@client.event
# Listen for an incoming message
async def on_message(message):
    # If the author is the robot itself, then do nothing!
    if message.author == client.user:
        return
    # If the user writes $qr
    if message.content == "$qr":
        current_time = now.strftime("%H:%M:%S")
        print('\n')
        # This for loop check for all the user's DiscordID in the Database
        if str(message.author.id) in ScholarsDict:
            print("This user received his QR Code : " + message.author.name)
            print("Discord ID : " + str(message.author.id))
            print("Current time : ", current_time)
            # value with discordID
            botPlaceHolders = ScholarsDict[str(message.author.id)]
            # discordID's privateKey from the database
            accountPrivateKey = botPlaceHolders[2]
            # discordID's EthWalletAddress from the database
            accountAddress = botPlaceHolders[1]
            # Get a message from AxieInfinty
            rawMessage = getRawMessage()
            # Sign that message with accountPrivateKey
            signedMessage = getSignMessage(rawMessage, accountPrivateKey)
            # Get an accessToken by submitting the signature to AxieInfinty
            accessToken = submitSignature(signedMessage, rawMessage, accountAddress)
            # Create a QrCode with that accessToken
            qrCodePath = f"QRCode_{message.author.id}_{str(uuid.uuid4())[0:8]}.png"
            generate_qr(accessToken, qrCodePath)

            # Send the QrCode the the user who asked for
            await message.author.send(
                "Tips: If the qr code wont work, please try this steps. Delete the old qr on your device, and get a "
                "new one again, "
                "if that doesn't work then scan the qr code with another device."

                + message.author.name + "\nHere is your new QR Code to login : "
            )
            await message.author.send(file=guilded.File(qrCodePath))
            os.remove(qrCodePath)
            return ["bot_token"]
        else:
            print("This user didn't receive a QR Code because not added in SecretStorage: " + message.author.name)
            print("Guilded ID: " + str(message.author.id))
            print("Current time: ", current_time)
            return

Expected behavior I expect the bot to DM the member the generated QR code

Actual behavior I get this error instead

Ignoring exception in on_message:
Traceback (most recent call last):
  File "/home/kintama/Downloads/Cryptic-QR-Code-for-Guilded/venv/lib/python3.8/site-packages/guilded/client.py", line 353, in _run_event
    await coro(*args, **kwargs)
  File "Cryptic-Scholars-QR-Code-Bot.py", line 67, in on_message
    await message.author.send(
  File "/home/kintama/Downloads/Cryptic-QR-Code-for-Guilded/venv/lib/python3.8/site-packages/guilded/user.py", line 213, in general
    return await getattr(self._user, x)(*args, **kwargs)
  File "/home/kintama/Downloads/Cryptic-QR-Code-for-Guilded/venv/lib/python3.8/site-packages/guilded/abc.py", line 616, in send
    await self.create_dm()
  File "/home/kintama/Downloads/Cryptic-QR-Code-for-Guilded/venv/lib/python3.8/site-packages/guilded/abc.py", line 591, in create_dm
    data = await self._state.create_dm_channel([self.id])
AttributeError: 'HTTPClient' object has no attribute 'create_dm_channel'

Environment

kintama48 commented 2 years ago

I have changed the code to use @bot.command() event listener however I still get the same error when I use the command on guilded. Here's the updated code.

@bot.command(name="qr", description="Sends QR to a user for login")
async def qr(ctx):
    current_time = now.strftime("%H:%M:%S")
    if str(ctx.author.id) in ScholarsDict:
        botPlaceHolders = ScholarsDict[str(ctx.author.id)]
        accountPrivateKey = botPlaceHolders[2]
        accountAddress = botPlaceHolders[1]
        rawMessage = getRawMessage()
        signedMessage = getSignMessage(rawMessage, accountPrivateKey)
        accessToken = submitSignature(signedMessage, rawMessage, accountAddress)

        qrCodePath = f"QRCode_{ctx.author.id}_{str(uuid.uuid4())[0:8]}.png"
        generate_qr(accessToken, qrCodePath)

        await ctx.author.send(
            "Tips: If the qr code wont work, please try this steps. Delete the old qr on your device, and get a "
            "new one again, "
            "if that doesn't work then scan the qr code with another device."

            + ctx.author.name + "\nHere is your new QR Code to login : "
        )
        await ctx.author.send(file=guilded.File(qrCodePath))
        os.remove(qrCodePath)
        return ["bot_token"]
    else:
        print("This user didn't receive a QR Code because not added in SecretStorage: " + message.author.name)
        print("Guilded ID: " + str(ctx.author.id))
        print("Current time: ", current_time)
        return

The Error I get

Ignoring exception in command qr:
Traceback (most recent call last):
  File "/home/kintama/Downloads/Cryptic-QR-Code-for-Guilded/venv/lib/python3.8/site-packages/guilded/ext/commands/core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "Cryptic-Scholars-QR-Code-Bot.py", line 62, in qr
    "Tips: If the qr code wont work, please try this steps. Delete the old qr on your device, and get a "
  File "/home/kintama/Downloads/Cryptic-QR-Code-for-Guilded/venv/lib/python3.8/site-packages/guilded/user.py", line 213, in general
    return await getattr(self._user, x)(*args, **kwargs)
  File "/home/kintama/Downloads/Cryptic-QR-Code-for-Guilded/venv/lib/python3.8/site-packages/guilded/abc.py", line 616, in send
    await self.create_dm()
  File "/home/kintama/Downloads/Cryptic-QR-Code-for-Guilded/venv/lib/python3.8/site-packages/guilded/abc.py", line 591, in create_dm
    data = await self._state.create_dm_channel([self.id])
AttributeError: 'HTTPClient' object has no attribute 'create_dm_channel'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/kintama/Downloads/Cryptic-QR-Code-for-Guilded/venv/lib/python3.8/site-packages/guilded/ext/commands/bot.py", line 421, in invoke
    await ctx.command.invoke(ctx)
  File "/home/kintama/Downloads/Cryptic-QR-Code-for-Guilded/venv/lib/python3.8/site-packages/guilded/ext/commands/core.py", line 619, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/home/kintama/Downloads/Cryptic-QR-Code-for-Guilded/venv/lib/python3.8/site-packages/guilded/ext/commands/core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
guilded.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'HTTPClient' object has no attribute 'create_dm_channel'
shayypy commented 2 years ago

This is not so much a bug but moreso just confusing. Only users can create DMs, thus the create_dm_channel method is only available if your HTTP Client was created by a UserbotClient or UserbotBot.

User.create_dm should have the |onlyuserbot| substitution but that's about it.