我希望当我运行ordinarie
命令时,通过同时添加头像和昵称来更改机器人的头像和昵称
编辑1: 我意识到我必须使用webhooks才能做到这一点,我做了几次搜索,但从未碰过webhooks。 我该怎么办?我将下面的代码留给您,以了解是否需要在此命令内部还是外部工作。
编辑2:
我尝试做webhook.send
,但不知道该如何激活Webhook!建议?
编辑3:
我终于创建了一个Webhook,但是当我希望机器人添加响应时,它会给我discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'add_reaction'.
我尝试查看是否通过执行print (message.id)
找到了消息的ID,但是它给了我同样的错误,所以问题是,尽管我在使用webhook.send
之前仍使用了message =它不将其计为我可以先使用channel. send
。我该如何解决?
下面我更新了代码。
代码:
@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
答案 0 :(得分:1)
这里是一个示例,说明如何使用webhook的名称和头像更改为被调用用户,您应该能够根据需要对其进行修改:
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)
请注意,webhooks是特定于渠道的,因此,如果您希望它在多个渠道中工作,则会变得混乱。
要增加反应,事情就变得更加复杂。由URL创建的webhook对象没有所有必需的数据,因此我们必须将其从Channel
对象中拉出:
# 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}')