Discord.py 个按钮(计数)
Discord.py Buttons (count)
所以我想提出一个建议,使用按钮而不是反应来投票。到目前为止,除了两件事之外,一切都有效。这是代码:
count = 0
count +=1
await interaction.response.edit_message(embed=embed2)
print(f"{count}") #check if count has gone up
button.callback = button_callback
每次单击按钮时,它不会显示数字,而是显示“”,每次 'count' 都是 1。
完整代码如下:
@client.command()
async def suggest(ctx, *, reason):
button = Button(label="Good Suggestion", style=discord.ButtonStyle.green,)
button2 = Button(label="Bad Suggestion", style=discord.ButtonStyle.red,)
async def button_callback(interaction):
count = 0
count +=1
print(f"{count}")
await interaction.response.edit_message(embed=embed2)
button.callback = button_callback
#count 2 is the same code
async def button_callback(interaction):
count2 = 0
count2 +=1
print(f"{count2}")
await interaction.response.edit_message(embed=embed2)
button2.callback = button_callback
embed = discord.Embed(color=ctx.message.author.color, timestamp=ctx.message.created_at)
embed.set_author(name=f"{ctx.message.author}({ctx.message.author.id})", icon_url= ctx.message.author.avatar)
embed.add_field(name='Suggestion:', value=f"{reason}")
embed.set_footer(text=f"Likes: 0 | Dislikes: 0")
embed2 = discord.Embed(color=ctx.message.author.color, timestamp=ctx.message.created_at)
embed2.set_author(name=f"{ctx.message.author}({ctx.message.author.id})", icon_url= ctx.message.author.avatar)
embed2.add_field(name='Suggestion:', value=f"{reason}")
embed2.set_footer(text=f"Likes: {count} | Dislikes: {count2}")
view = View()
view.add_item(button)
view.add_item(button2)
await ctx.send(embed=embed,view=view)
我不知道该怎么做,因为我已经做了我知道该怎么做的事情。有人可以帮我解释一下该怎么做吗?
# Globally
count1 = 0
async def suggest(ctx, *, reason):
async def button_callback(interaction):
global count1 # Let the scope know that we are using count1 from the global scope
count1 += 1
# ... everything else
或者我们可以选择将计数引用传递给函数
async def suggest(ctx, *, reason, count)
或者我们可以选择从 embed
中获取值并递增。这将避免服务器因任何原因停止 运行 而我们无法跟踪当前计数的问题。我想我们也可以将它写入一个文件,但是如果有多个 buttons/embeds 等等,这会变得很混乱。
您可以使用 TheLazyScripter 的方法,但是这有一个问题,一旦 suggest
命令被多次调用,count
变量就会变得混乱,这里有一种方法可以防止那:
async def suggest(ctx, *, reason):
count = 0
async def button_callback(interaction):
nonlocal count
count += 1
所以我想提出一个建议,使用按钮而不是反应来投票。到目前为止,除了两件事之外,一切都有效。这是代码:
count = 0
count +=1
await interaction.response.edit_message(embed=embed2)
print(f"{count}") #check if count has gone up
button.callback = button_callback
每次单击按钮时,它不会显示数字,而是显示“
完整代码如下:
@client.command()
async def suggest(ctx, *, reason):
button = Button(label="Good Suggestion", style=discord.ButtonStyle.green,)
button2 = Button(label="Bad Suggestion", style=discord.ButtonStyle.red,)
async def button_callback(interaction):
count = 0
count +=1
print(f"{count}")
await interaction.response.edit_message(embed=embed2)
button.callback = button_callback
#count 2 is the same code
async def button_callback(interaction):
count2 = 0
count2 +=1
print(f"{count2}")
await interaction.response.edit_message(embed=embed2)
button2.callback = button_callback
embed = discord.Embed(color=ctx.message.author.color, timestamp=ctx.message.created_at)
embed.set_author(name=f"{ctx.message.author}({ctx.message.author.id})", icon_url= ctx.message.author.avatar)
embed.add_field(name='Suggestion:', value=f"{reason}")
embed.set_footer(text=f"Likes: 0 | Dislikes: 0")
embed2 = discord.Embed(color=ctx.message.author.color, timestamp=ctx.message.created_at)
embed2.set_author(name=f"{ctx.message.author}({ctx.message.author.id})", icon_url= ctx.message.author.avatar)
embed2.add_field(name='Suggestion:', value=f"{reason}")
embed2.set_footer(text=f"Likes: {count} | Dislikes: {count2}")
view = View()
view.add_item(button)
view.add_item(button2)
await ctx.send(embed=embed,view=view)
我不知道该怎么做,因为我已经做了我知道该怎么做的事情。有人可以帮我解释一下该怎么做吗?
# Globally
count1 = 0
async def suggest(ctx, *, reason):
async def button_callback(interaction):
global count1 # Let the scope know that we are using count1 from the global scope
count1 += 1
# ... everything else
或者我们可以选择将计数引用传递给函数
async def suggest(ctx, *, reason, count)
或者我们可以选择从 embed
中获取值并递增。这将避免服务器因任何原因停止 运行 而我们无法跟踪当前计数的问题。我想我们也可以将它写入一个文件,但是如果有多个 buttons/embeds 等等,这会变得很混乱。
您可以使用 TheLazyScripter 的方法,但是这有一个问题,一旦 suggest
命令被多次调用,count
变量就会变得混乱,这里有一种方法可以防止那:
async def suggest(ctx, *, reason):
count = 0
async def button_callback(interaction):
nonlocal count
count += 1