英文:
Discord bot with Clock in and clock out functions, but order are not correct
问题
Here's the translated text:
我有一个机器人,我希望首先是按钮,然后是文本。我尝试使用chatgpt来更改顺序,但不确定为什么无法让它首先出现。
这基本上是一个打卡系统,应该首先是文本,但我得到的是文本,然后是按钮,行为有点奇怪。
Please note that code elements like variable names, class names, and method names remain unchanged in the translation.
英文:
I have this bot where I want to have Buttons first and text bellow. I tried chatgpt to fix the order but not sure why I can't make it come first
import discord
from discord.ext import commands
from datetime import datetime
import settings
texto1 = []
texto2 = []
bot = discord.Client(intents=discord.Intents.all())
class MyView(discord.ui.View):
@discord.ui.button(label="Entrar", style=discord.ButtonStyle.green, custom_id="entrar")
async def entrar_button_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user.display_name not in texto1:
view = MyView()
texto1.append(interaction.user.display_name)
texto2.append(f"{interaction.user.display_name} entrou às {datetime.now().strftime('%H:%M:%S')}")
await interaction.message.edit(content=get_text(), view=view)
await interaction.response.defer()
@discord.ui.button(label="Sair", style=discord.ButtonStyle.red, custom_id="sair")
async def sair_button_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user.display_name in texto1:
view = MyView()
texto1.remove(interaction.user.display_name)
texto2.append(f"{interaction.user.display_name} saiu às {datetime.now().strftime('%H:%M:%S')}")
await interaction.message.edit(content=get_text(), view=view)
await interaction.response.defer()
async def update_message(channel):
# create a MyView object with the buttons
view = MyView()
# create an initial message
text = get_text()
message = await channel.send(text, view=view)
# update the message with the actual data
text = get_text()
await message.edit(view=view, content=text)
def get_text():
text1 = '\n'.join(texto1) if texto1 else 'Ninguém'
text2 = '\n'.join(texto2) if texto2 else 'Nenhum registro ainda'
text = f"**Quem está trabalhando:**\n{text1}\n\n**Log de chegada e saída:**\n{text2}"
return text
@bot.event
async def on_ready():
print('Bot está online')
channel = bot.get_channel(1105561415038812311) # Insira aqui o ID do canal em que o bot deve ficar
await update_message(channel)
bot.run(settings.DISCORD_API_SECRET)
Its basically a clock in and out suppose to be text first but I got
text then buttons. kinda weird behave
答案1
得分: 0
"如果您想首先显示按钮,您需要将它们发送到一个单独的消息中,然后再发送另一条带有文本的消息。 - Hazzu
您说得对,我分开发送了:
view = MyView()
# 创建初始消息
message = await channel.send(view=view)
并解决了问题
谢谢"
英文:
> If you want to display buttons first, you'll need to send them in a
> separate message, and then another message with the text. – Hazzu
You were right, I did separate
view = MyView()
# create an initial message
message = await channel.send(view=view)
and solved the problem
thanks
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论