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

dxpyg8gm  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(117)

我目前有一个机器人,它会通过一个CSV文件,其中包含username#0000格式的用户名沿着每个用户的关键字。如果机器人检测到消息中的某个关键字,它会发送一条消息,其中包含所有与该消息中提到的关键字匹配的用户。我已经能够让代码做所有事情,但在一条消息中提到所有用户。
这是目前的代码,我有,但它似乎不工作。任何帮助将不胜感激。

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)

我希望能够让机器人发送这样的消息:@user1#0001@user1#0002@user1#0003@user1#0004@user1#0005您现在是超级明星了。
如果我打印(metionMembers),我会得到一个匹配该关键字的所有成员的列表。

yquaqz18

yquaqz181#

要在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: #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"<@{user_id}>") 
await message.channel.send(f"{' '.join(mention_members)} You're now a superstar")

相关问题