如何在一条消息中提及来自列表的多个用户?

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

How to mention multiple users from a list in one message?

问题

await message.channel.send(f"{ ' '.join([member for member in mentionMembers])} You're now a superstar.")
英文:

I currently have a bot that goes through a CSV file with usernames in the format of username#0000 along with keywords for each user. If the bot detects a certain keyword in a message, it will send a message with all users that match that keyword mentioned in that one message. I have been able to get the code to do everything but the mentioning of all the users in one message.

This is the current code that I have but it doesn't seem to be working. Any help would be greatly appreciated.

mentionMembers = []
key1 = "superstar"
with open('csvfile.csv',) as csvfile:
reader = csv.reader(csvfile)
for row in reader:
    if row[2].strip() == key1: #row 2 of the CSV is where the keyword is located
    mentionMembers.append(row[0].strip()) #row 0 of the CSV is where all the usernames are located
await message.channel.send(f"{" ".join([member.mention for member in mentionMembers])} You're now a superstar)

I'm hoping to be able to get the bot to send a message like this
Bot:
@user1#0001 @user1#0002 @user1#0003 @user1#0004 @user1#0005 You're now a superstar.

If I do print (metionMembers) I get a list of all the members that matched that keyword.

答案1

得分: 1

要在Discord上提及用户,您需要他们的ID。提及的语法是:<@user_id>。所以,不要保存用户名,保存用户ID。

假设row[0]对应于用户ID,您可以按如下方式提及他们:

mention_members = []
key1 = "superstar"
with open('csvfile.csv',) as csvfile:
    reader = csv.reader(csvfile)
for row in reader:
    if row[2].strip() == key1: # CSV文件的第2列是关键词所在的位置
        user_id = row[0].strip() # CSV文件的第0列是所有ID所在的位置
        mention_members.append(f"<@{user_id}>") 
await message.channel.send(f"{' '.join(mention_members)} You're now a superstar")
英文:

To mention a user on Discord you need their ID. The mention syntax is: &lt;@user_id&gt;. So, instead of saving the username, save the user ID.

Assuming that row[0] corresponds to the user ID, you can mention them as follows:

mention_members = []
key1 = &quot;superstar&quot;
with open(&#39;csvfile.csv&#39;,) as csvfile:
    reader = csv.reader(csvfile)
for row in reader:
    if row[2].strip() == key1: #row 2 of the CSV is where the keyword is located
        user_id = row[0].strip() #row 0 of the CSV is where all the ids are located
        mention_members.append(f&quot;&lt;@{user_id}&gt;&quot;) 
await message.channel.send(f&quot;{&#39; &#39;.join(mention_members)} You&#39;re now a superstar&quot;)

huangapple
  • 本文由 发表于 2023年4月11日 09:35:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981825.html
匿名

发表评论

匿名网友

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

确定