【问题标题】:How do I convert a mention variable, to a normal variable and print it to my terminal如何将提及变量转换为普通变量并将其打印到我的终端
【发布时间】:2021-04-30 09:25:06
【问题描述】:

我的代码中有一个看起来像这样的变量,唯一的问题是它在我的终端中看起来很丑。这是一张图片。

我希望它显示:{获胜者姓名}赢得了:{prize},而不是显示他们的 Discord ID 并让我的终端看起来很难看。

这是我的代码,它使这个终端成为怪物。

winners = random.sample([user for user in users if not user.bot], k=winerscount)

winnerstosend = "\n".join([winner.mention for winner in winners])




win = await msg.edit(embed = discord.Embed(title = "WINNER" , description = f"Congratulations {winnerstosend}, you have won **{msg4.content}**!" , color = discord.Color.blue()))

print(Fore.GREEN + winnerstosend , Fore.WHITE + "has won: " , Fore.GREEN + msg4.content)
print(Style.RESET_ALL)

现在,如果您能帮助我,并将我的错误转化为漂亮的确认信息,我会很高兴的。

【问题讨论】:

    标签: python python-3.x discord bots discord.py


    【解决方案1】:

    嗯,试试winner.display_name 而不是winner.mention

    【讨论】:

      【解决方案2】:

      用户是你的对象吗? 如果是这样,您可以定义一个str dunder-method 并以您想要的方式打印字符串。

      请看下面的例子。

      user@Inspiron:~/code/advanced_python$ cat str_dunder_example.py 
      class A:
          def __init__(self, name, age):
              self.name = name
              self.age = age
      
          def __str__(self):
              return f'User {self.name} aged {self.age} has won'
      
      if __name__ == '__main__':
          a = A('FakeBlob', 20)
          print(a)
      user@Inspiron:~/code/advanced_python$ python str_dunder_example.py 
      User FakeBlob aged 20 has won
      user@Inspiron:~/code/advanced_python$ 
      

      【讨论】:

      • 我不明白你想问什么。 @ll
      • winerstosend 对象的类是你定义的?
      • 您可以转换为一个类,并通过定义 str 以您喜欢的方式打印它,如上图所示。
      猜你喜欢
      • 1970-01-01
      • 2018-04-12
      • 2020-09-11
      • 1970-01-01
      • 2012-06-27
      • 2018-10-12
      • 2019-05-24
      • 2015-05-08
      • 1970-01-01
      相关资源
      最近更新 更多