Discord机器人具有签到和签退功能,但顺序不正确。

huangapple go评论101阅读模式
英文:

Discord bot with Clock in and clock out functions, but order are not correct

问题

Here's the translated text:

  1. 我有一个机器人我希望首先是按钮然后是文本我尝试使用chatgpt来更改顺序但不确定为什么无法让它首先出现
  2. 这基本上是一个打卡系统应该首先是文本但我得到的是文本然后是按钮行为有点奇怪

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

  1. import discord
  2. from discord.ext import commands
  3. from datetime import datetime
  4. import settings
  5. texto1 = []
  6. texto2 = []
  7. bot = discord.Client(intents=discord.Intents.all())
  8. class MyView(discord.ui.View):
  9. @discord.ui.button(label="Entrar", style=discord.ButtonStyle.green, custom_id="entrar")
  10. async def entrar_button_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
  11. if interaction.user.display_name not in texto1:
  12. view = MyView()
  13. texto1.append(interaction.user.display_name)
  14. texto2.append(f"{interaction.user.display_name} entrou às {datetime.now().strftime('%H:%M:%S')}")
  15. await interaction.message.edit(content=get_text(), view=view)
  16. await interaction.response.defer()
  17. @discord.ui.button(label="Sair", style=discord.ButtonStyle.red, custom_id="sair")
  18. async def sair_button_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
  19. if interaction.user.display_name in texto1:
  20. view = MyView()
  21. texto1.remove(interaction.user.display_name)
  22. texto2.append(f"{interaction.user.display_name} saiu às {datetime.now().strftime('%H:%M:%S')}")
  23. await interaction.message.edit(content=get_text(), view=view)
  24. await interaction.response.defer()
  25. async def update_message(channel):
  26. # create a MyView object with the buttons
  27. view = MyView()
  28. # create an initial message
  29. text = get_text()
  30. message = await channel.send(text, view=view)
  31. # update the message with the actual data
  32. text = get_text()
  33. await message.edit(view=view, content=text)
  34. def get_text():
  35. text1 = '\n'.join(texto1) if texto1 else 'Ninguém'
  36. text2 = '\n'.join(texto2) if texto2 else 'Nenhum registro ainda'
  37. text = f"**Quem está trabalhando:**\n{text1}\n\n**Log de chegada e saída:**\n{text2}"
  38. return text
  39. @bot.event
  40. async def on_ready():
  41. print('Bot está online')
  42. channel = bot.get_channel(1105561415038812311) # Insira aqui o ID do canal em que o bot deve ficar
  43. await update_message(channel)
  44. 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

您说得对,我分开发送了:

  1. view = MyView()
  2. # 创建初始消息
  3. 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

  1. view = MyView()
  2. # create an initial message
  3. message = await channel.send(view=view)

and solved the problem
thanks

huangapple
  • 本文由 发表于 2023年5月14日 03:10:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定