为在Discord bot中创建的csv文件创建命令(python)

时间:2020-09-03 07:58:47

标签: python discord bots

codepython

codepython

codepython

嘿,我正在尝试创建一个机器人,以添加一个用于创建有关服务器中的成员名称,角色等的csv文件的命令。我也想添加一个从该文件中搜索的功能。就像如果成员以不同的名称再次加入服务器时,他们可以说出类似!user我是这个人,并且漫游器将从列表中搜索并可能更改名称,如果该人不在列表中,则需要@服务器的管理员。我想获得有关创建此机器人的信息的帮助。到目前为止,我设法找到了一些确实创建了成员列表的东西,并思考了如何从中创建命令。非常感谢。

class Bot(discord.Client):
    async def on_ready(self):
        print('Logged in as {0}'.format(self.user))

    async def on_message(self, message):
        # !users: Gathers the names of all users on the server and prints them to a CSV file
        if "!users" in message.content.lower():
            await message.channel.send("Getting user list...")
            users = self.get_all_members()

            with open('log.csv', 'w') as output:
                writer = csv.writer(output)
                writer.writerow(['Name'])
                for user in users:
                    writer.writerow([user.display_name])
                    
---------------------------------------------------------
@bot.command()
async def users(ctx):
# !users: Gathers the names of all users on the server and prints them to a CSV$
await ctx.send("Getting user list...")
await ctx.get_all_members()

with open('log.csv', 'w') as output:
        writer = csv.writer(output)
        writer.writerow(['Name'])
        for user in users:
            writer.writerow([user.display_name])

------------------------------------------------------------------

async def on_message(self, message):
# !users: Gathers the names of all users on the server and prints them to CSVS
        if "!users" in message.content.lower():
                await message.channel.send("Getting user list...")
                users = self.get_all_members()


                with open('log.csv', 'w') as output:
                writer = csv.writer(output)
                writer.writerow(['Name'])
                for user in users:
                writer.writerow([user.display_name])

1 个答案:

答案 0 :(得分:0)

设法从Youtube找到这个

import csv

data=[]
with open("details.csv") as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
            data.append(row)
print (data)

name = input("Enter a first name: ")
#gives us a lis tof everuthing in col 0
col = [x[0] for x in data]

if name in col:
        for x in range(0, ien(data)):
            if name == data[x] [0]:
                print(data[x])
               
else:
    print("name doesn't exist")