有没有办法编辑不和谐按钮的大小?

问题描述 投票:0回答:2

我目前正在启动一个 connect 4 Discord 机器人,并使用按钮让人们放置他们的作品。问题的出现是因为使用 :white_large_square: 表情符号制作的板比按钮小。此外,按钮占用两行,因为它们有六个。有没有办法让按钮的大小与表情符号相似?

@bot.command(brief=" Begins your Connect 4 Game",
  description=
  " Displays the board and buttons, which will place your piece in the desired lane",
  Arguments="None")
async def Connect4(ctx):
  open_file = open(ctx.author.name,"w")
  open_file.write(":white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:\n:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:\n:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:\n:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:\n:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:\n:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:")
  open_file.close()
  open_file = open(ctx.author.name,"r")
  board = []
  for _ in range(6):
    value = open_file.readline()
    board.append(value.strip("\n").split(","))
  open_file.close()
  L1 = "".join(board[0])
  L2 = "".join(board[1])
  L3 = "".join(board[2])
  L4 = "".join(board[3])
  L5 = "".join(board[4])
  L6 = "".join(board[5])
  button1 = Button(label="", emoji="1️⃣", style=discord.ButtonStyle.gray)
  button2 = Button(label="", emoji="2️⃣", style=discord.ButtonStyle.gray)
  button3 = Button(label="", emoji="3️⃣", style=discord.ButtonStyle.gray)
  button4 = Button(label="", emoji="4️⃣", style=discord.ButtonStyle.gray)
  button5 = Button(label="", emoji="5️⃣", style=discord.ButtonStyle.gray)
  button6 = Button(label="", emoji="6️⃣", style=discord.ButtonStyle.gray)
  view1 = View()
  view1.add_item(button1)
  view1.add_item(button2)
  view1.add_item(button3)
  view1.add_item(button4)
  view1.add_item(button5)
  view1.add_item(button6)
  message = L1 + "\n" +L2 + "\n" +L3+ "\n" +L4+ "\n" +L5+ "\n" +L6
  await ctx.send(message,view=view1)

示例图片:

python button discord discord.py discord-buttons
2个回答
0
投票

按钮尺寸不是

Changeable
!

但是,您可以更改

Maximum Size Of Each Row/Column 

要得到这样的东西:

1 2 3
4 5 6


0
投票

由于这是搜索“不和谐按钮宽度”之类的内容时出现的第一个结果之一,因此我想更详细地解决这个问题。

首先,为了回答您的问题,每个操作行最多只能有五个按钮,每条消息最多只能有五个操作行(每条消息总共最多有 25 个按钮)。请参阅ActionRow 文档。因此,您将无法将所有六个按钮排成一排。然而,正如 ArDaVaN81 所说,您可以有两行,每行三个按钮。对于你的第二个问题,我无法找到任何相关文档,但根据我的理解和经验,你不能将按钮的宽度减小到超过某个最小点。如果您在按钮中仅使用表情符号,那么您将无法控制按钮的宽度。

控制带有文本标签的按钮宽度

这让我想到了我想要解决的问题,它不能回答您的问题,但如果您选择使用文本标签而不是表情符号,可能对您有用。对于那些在按钮中使用文本标签的用户,您可以使用与 Discord 嵌入中使用的相同技巧来增加按钮的宽度 - 将空格字符封装在零宽度空格 [\u200b] (或其他非空白)内人物。您还可以使用各种不同类型的空间来精细控制这些按钮的宽度。例如,我喜欢使用这个网站。一些示例代码:

let myActionRowBuilder = Discord.ActionRowBuilder()
  .addComponents(
    .setCustomId('!helloworld')
    .setLabel('\u200b    Hello World!     \u200b')  //you can insert different types of whitespace around "Hello World!" to finely control the button width
    .setEmoji(helloWorldEmoji) //optional- the emoji will appear on the far left
    .setStyle(Discord.ButtonStyle.Primary)
  );

动态控制按钮宽度

此外,如果您需要动态控制按钮宽度,可以使用这个库。它内部定义了 Discord 字体的字符宽度,然后使用各种空格填充字符将按钮宽度控制在字符串宽度的 0.5 个单位以内。它的工作并不完美,但当您的按钮标签字符串事先未知时,它肯定很有用。下面是一些示例代码:

import { padStringToWidth } from 'discord-button-width';

let myActionRowBuilder = Discord.ActionRowBuilder()
    .addComponents(
        .setCustomId('!helloworld')
        .setLabel(`\u200b${padStringToWidth('Hello World!', 80, "center")}\u200b`)  //the zero-width space unicode characters are still needed
        .setEmoji(helloWorldEmoji) //optional- the emoji will appear on the far left
        .setStyle(Discord.ButtonStyle.Primary)
    );

我知道很多人会因为帖子的标题而来到这里,所以我希望这对那些希望使用文本标签控制 Discord 按钮宽度的人有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.