Discord.py将数据循环到文本文件



嗨,伙计们,我正试图为我的大学写一个函数,记录谁在不和谐的语音通道中。然而,我试图将数据输出到一个文本文件中,这样我的导师就可以有一个在实验室会议期间谁在通道中的文件。

这就是我目前拥有的:

@commands.has_permissions(administrator=True)
@bot.command()
async def test(ctx):
with open('students.txt', "w") as file:
channel = bot.get_channel(my id here)
members = channel.members
for member in members:
file.write(members.display_name)
with open("students.txt", "rb") as file:
await ctx.author.send("Student attendence:", file=discord.File(file,"students.txt"))

但事实上我没有得到任何回应。帮助建议?干杯:(

PS bot=客户端

我试过你的代码,它运行得很好,但唯一的问题是缩进关闭了,而且你在for member in members循环中使用了members.display_name而不是member.display_name

@commands.has_permissions(administrator=True)
@bot.command()
async def test(ctx):
with open('students.txt', "w") as file:
channel = bot.get_channel(id goes here)
members = channel.members
for member in members:
file.write(member.display_name)
with open("students.txt", "rb") as file:
await ctx.author.send("Student attendence:", file=discord.File(file, "students.txt"))

最新更新