[英]Discord.py buttons
所以我想发送一个带有“我加入”的按钮的嵌入,用户可以点击它,这样一个邀请就会在 JSON 文件中下降 go
这是我的代码:
@client.command()
async def post(ctx, *, content="!"):
await ctx.channel.purge(limit=1)
with open("bot-info.json", "r") as f:
bot_data = json.load(f)
DISCORD_API_LINK = "https://discordapp.com/api/invite/"
DISCORD_GET_IMAGE_LINK = "https://cdn.discordapp.com/icons/"
channel = client.get_channel(bot_data["nft_projects_channel"])
if len(content.split(";")) == 3:
LINK, DESCRIPTION, INVITES = content.split(";")
invite_code = LINK.split("/")[-1]
async with aiohttp.ClientSession() as session:
async with session.get(DISCORD_API_LINK + invite_code) as response:
data = await response.text()
json_data = json.loads(data)
try:
SERVER_NAME = json_data["guild"]["name"]
SERVER_ID = json_data["guild"]["id"]
if json_data["expires_at"] != None:
msg = await ctx.send("Warning! invitation has an expiration date, snd an invitation without an expiration date.")
await asyncio.sleep(6)
await msg.delete()
return None
try:
json_data["inviter"]
except:
embed = discord.Embed(title="Error", description="Invite Link is not unique", color=0xF54245)
msg = await ctx.send(embed=embed)
await asyncio.sleep(6)
await msg.delete()
return None
SERVER_ICON = str(DISCORD_GET_IMAGE_LINK + SERVER_ID + "/" + json_data["guild"]["icon"] + ".png")
except:
embed = discord.Embed(title="Error", description="Check invite link", color=0xF54245)
msg = await ctx.send(embed=embed)
await asyncio.sleep(6)
await msg.delete()
return None
with open("all-servers.json", "r") as read_file:
data = json.load(read_file)
if not SERVER_ID in data:
##################################################################################################
embed = discord.Embed(title = SERVER_NAME, url=LINK, description = DESCRIPTION, color=0xE659ff) #This embed should have the button
embed.add_field(name="Link:", value=LINK, inline=False)
embed.add_field(name="Invites Requird:", value=INVITES, inline=False)
embed.set_thumbnail(url=SERVER_ICON)
await channel.send(embed=embed) #here I am sending it
##################################################################################################
data[SERVER_ID] = {}
data[SERVER_ID]["SERVER ID"] = SERVER_ID
data[SERVER_ID]["INVIRES NEEDED"] = int(INVITES)
with open("all-servers.json", "w") as write_file:
json.dump(data, write_file, indent=4)
else:
msg = await ctx.send("The server has already been sent.")
await asyncio.sleep(6)
await msg.delete()
else:
embed = discord.Embed(title="Error", description=f"Post command requires 3 arguments.\nUseage: `{get_prefix()}post <link>; <description>; <invites required>`\nto post a server on the nft-projects channel", color=0xF54245)
msg = await ctx.send(embed=embed)
await asyncio.sleep(6)
await msg.delete()
在此之后,嵌入应删除自身并从 JSON 文件中删除自身,但我可以自己执行此操作,我只需要交互帮助来触发 function。
谢谢,里奥
首先,您需要使用 discord.py 版本 2.0.0 才能正常工作。
要将按钮添加到消息中,您首先必须创建一个视图,然后将此视图添加到您要发送的消息中:
class DeleteEmbedView(discord.ui.View):
@discord.ui.button(label='I joined', style=discord.ButtonStyle.green)
async def delete(self, button: discord.ui.Button, interaction: discord.Interaction):
# This is called once the button is clicked
await interaction.message.delete() #delete the message with the embed
# delete it from the JSON file here
然后每当您想发送带有消息的按钮时,请执行以下操作:
await channel.send(embed=embed, view=DeleteEmbedView())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.