peppewarrior1
peppewarrior1

Reputation: 55

how to make a webhook to send a message with the avatar and nickname of the user execute command discord.py

I would like that when I run the ordinarie command the avatar and the nickname of the bot change by adding both the avatar and the nickname to get this result

Edit 1: I realized that I have to use webhooks for this to happen and I did a couple of searches but never touched webhooks. How could I do? I leave you the code below to know if I have to work inside or "outside" this command.

Edit 2: I have tried doing webhook.send but I have no idea how I could activate a webhook! advice?

Edit 3: I finally created a webhook but when I want the bot to add the reactions it gives me discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'add_reaction'. I tried to see if I found the id of the message by doing print (message.id) but it gives me the same error so the problem is that despite me before using webhook.send I use message = it does not count it as I could do with channel. send first. How do I resolve? Below I have updated the code.

code:

@client.command(aliases=["Ordinarie","ORDINARIE","ord","Ord","ORD"])
@commands.has_any_role(690951634183782461, 690954867346243624, 690956686147453048, 690960692051705896)
async def ordinarie(ctx, *, proposta):
    await ctx.channel.purge(limit=1)
    channel = client.get_channel(637761152654508062)
    if ctx.channel.id == channel.id:
        diocane = ctx.author
        favore = get_emoji(ctx.guild, "Favorevole")
        contro = get_emoji(ctx.guild, "Contrario")
        flore = get_emoji(ctx.guild, "Astenuto")
        WEBHOOK_URL = "https://discordapp.com/api/webhooks/765735283831603201/x2i974bNeGwUq13I5p0WmGaviVrkkhsX8IIcQTgnTSr4F9vOFzEi9bK19pD6Bi_6ogos"
        async with ClientSession() as session:
            webhook = discord.Webhook.from_url(WEBHOOK_URL, adapter=discord.AsyncWebhookAdapter(session))
            message = await webhook.send(content=f"**Proposta Ordinaria di {diocane.mention}**\n\n{proposta}\n\nVOTA A FAVORE [{favore}]\nVOTA CONTRO [{contro}]\nASTIENITI [{flore}]", username=ctx.author.name, avatar_url=ctx.author.avatar_url)
        await message.add_reaction(favore)
        await message.add_reaction(contro)
        await message.add_reaction(flore)
        await asyncio.sleep(10)
        message = await webhook.fetch_message(message.id)
        await message.remove_reaction(favore, client.user)
        await message.remove_reaction(contro, client.user)
        await message.remove_reaction(flore, client.user)
        thumbsup = get(message.reactions, emoji = favore)
        thumbsdown = get(message.reactions, emoji = contro)
        neutral = get(message.reactions, emoji = flore)
        if thumbsup.count > thumbsdown.count:
            await webhook.send(content=f"**Risultati della proposta di {diocane.mention}**:\n{thumbsup.count - 1} a favore, {thumbsdown.count - 1} contro e {neutral.count - 1} astenuti.\nLa proposta di {diocane.mention} è stata approvata e l'organo incaricato dovrà pertanto applicarla.", username=ctx.author.name, avatar_url=ctx.author.avatar_url)
            WEBHOOK_URL = "https://discordapp.com/api/webhooks/765738529803468820/QyQ_zXzEF_q2EzH1ijTVBZY5L-hxru7lR1EsFoL7L1b5IdbpQe7uiQjVjcBET77C0wjS"
            async with ClientSession() as session:
                webhook = discord.Webhook.from_url(WEBHOOK_URL, adapter=discord.AsyncWebhookAdapter(session))
                await webhook.send(content=proposta, username=ctx.author.name, avatar_url=ctx.author.avatar_url)
                return
        if thumbsup.count < thumbsdown.count:
            await webhook.send(content=f"**Risultati della proposta di {diocane.mention}**:\n{thumbsup.count - 1} a favore, {thumbsdown.count - 1} contro e {neutral.count - 1} astenuti.\nLa proposta di {diocane.mention} è stata rifiutata e pertanto non potranno essere presenti in altre proposte i contenuti di questa per i prossimi 30 giorni.", username=ctx.author.name, avatar_url=ctx.author.avatar_url)
            return
        if thumbsup.count == thumbsdown.count:
            await webhook.send(content=f"**Risultati della proposta di {diocane.mention}**:\n{thumbsup.count - 1} a favore, {thumbsdown.count - 1} contro e {neutral.count - 1} astenuti.\nLa proposta di {diocane.mention} è terminata in parità e pertanto spetterà al Triumvirato decidere a riguardo tra le opzioni finite in pareggio.", username=ctx.author.name, avatar_url=ctx.author.avatar_url)
            return

Upvotes: 0

Views: 9324

Answers (1)

unex
unex

Reputation: 1356

Here is an example for how to use a webhook with the name and avatar changed to the invoked user, you should be able to modify it for your needs:

WEBHOOK_URL = "<redacted>"

from aiohttp import ClientSession

@client.command()
async def test(ctx, *, message: str):
    async with ClientSession() as session:
        webhook = discord.Webhook.from_url(WEBHOOK_URL, adapter=discord.AsyncWebhookAdapter(session))

        await webhook.send(content=message, username=ctx.author.name, avatar_url=ctx.author.avatar_url)

Note that webhooks are channel specific, so if you want this to work in more than one channel it's gonna get messy.

For adding reactions, things get more complicated. A webhook object created by URL does not have all the required data, so we have to pull it from the Channel object:

# keep track of each webhook per channel perhaps?
WEBHOOK_ID = 748726283772624896

@client.command()
async def test(ctx, *, message: str):
    webhook = discord.utils.get(await ctx.channel.webhooks(), id=WEBHOOK_ID)

    if not webhook:
        # webhook does not exist, create and store the id possibly?
        webhook = await ctx.channel.create_webhook(...)
        # store webhook.id here?

    m = await webhook.send(content=message, username=ctx.author.name, avatar_url=ctx.author.avatar_url, wait=True) # wait=True will make this return the 'Message' object
    await m.add_reaction('\N{OK HAND SIGN}')

Upvotes: 7

Related Questions