Discord.py – Send message under different name or user

Question:

I’ve been working on a bot and there was a small request, which I have no idea how to do.

When user types their message, the bot checks whether the message contains a predetermined keyword (to put everything into perspective, I’m using examples from an already working BOT), replace this part (for example an emoji) and post the message again, under the name of the user, which posted the message. (I do not want to do exactly this, I just want to know how to "post as another user" like Animated Emojis bot).

It works like this:

Example of image

Also somehow, the user created is not a valid user, since they don’t have any roles or can be messaged. This is what happens if you click on their profile (in chat):

Invalid profile

As I’ve said before, this is somehow possible (the animated emojis bot is one of the examples) and I wonder, how can I do something like this.

Thanks in advance and I hope you have lovely day!

Asked By: TargetDummy

||

Answers:

I have found a simple way, and seems like what you are talking about, simply creating a webhook with the user’s name or another user you use the command to "act" as, will create a Discord webhook with those details, then deletes the webhook, but keeps the message. Try this.

@client.command()
async def act(ctx, member: discord.Member, *, message=None):

        if message == None:
                await ctx.send(
                    f'Please provide a message with that!')
                return

        webhook = await ctx.channel.create_webhook(name=member.name)
        await webhook.send(
            str(message), username=member.name, avatar_url=member.avatar_url)

        webhooks = await ctx.channel.webhooks()
        for webhook in webhooks:
                await webhook.delete()

EDIT: A webhook is a communication type that can be used to access & automate your messages to send data updates to your Discord text channels.

In this case, when sending the message from the command, the user appears as a bot, but everything else is from the user. NQN bot uses this exact method

Answered By: Cohen

You can get a user’s avatar using the user.avatar attribute & the name using the user.name attribute.
You then use await bot.user.edit(avatar=b, name="name") to change your bots avatar. Where b is a bytes type object.

Answered By: Kelo

Discord gives cooldown on rapid pfp change and the pfp will be changed for the bot completely not just that message which is disco party

Answered By: Joel
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.