简体   繁体   English

如何将提及变量转换为普通变量并将其打印到我的终端

[英]How do I convert a mention variable, to a normal variable and print it to my terminal

I have a variable in my code that looks like this, only problem is it looks very ugly in my terminal.我的代码中有一个看起来像这样的变量,唯一的问题是它在我的终端中看起来非常难看。 Here's a picture.这是一张图片。

我的终端

I would like it to display: {Name of the winner} has won: {prize} , instead of showing their Discord ID and making my terminal look horrible to look at.我希望它显示: {Name of the winner} has won: {prize} ,而不是显示他们的 Discord ID 并让我的终端看起来很糟糕。

Here is my code, which makes this monstrosity of a terminal.这是我的代码,它使终端变得如此怪异。

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)

Now I would love it if you could help me out, and convert my mistake into a beautiful confirmation message.现在,如果您能帮助我,并将我的错误转化为漂亮的确认信息,我会很高兴的。

Is user your object?用户是您的 object 吗? If so, you can define a str dunder-method and print the string in the way you want.如果是这样,您可以定义一个str dunder 方法并以您想要的方式打印字符串。

Please see example below.请看下面的例子。

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$ 

hmm, try winner.display_name instead of winner.mention嗯,试试winner.display_name而不是winner.mention

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM