Guest User

Untitled

a guest
Dec 11th, 2017
7,638
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """A bot to list all members of a server."""
  2. import csv
  3. import time
  4.  
  5. from discord.ext import commands
  6. from discord.ext.commands import Bot
  7.  
  8. # constants
  9. PREFIX = "~"
  10. TOKEN = "PUT YOUR BOTs TOKEN HERE"
  11.  
  12. bot = Bot(command_prefix=PREFIX)
  13.  
  14.  
  15. @bot.event
  16. async def on_ready():
  17.     print('Logged in as')
  18.     print(bot.user.name)
  19.     print(bot.user.id)
  20.     print('------')
  21.  
  22.  
  23. @bot.event
  24. async def on_command_error(error, ctx):
  25.     if isinstance(error, commands.CommandNotFound):
  26.         return
  27.     else:
  28.         print(error)
  29.  
  30.  
  31. @bot.command(pass_context=True)
  32. async def stat(ctx):
  33.     """Returns a CSV file of all users on the server."""
  34.     await bot.request_offline_members(ctx.message.server)
  35.     before = time.time()
  36.     nicknames = [m.display_name for m in ctx.message.server.members]
  37.     with open('temp.csv', mode='w', encoding='utf-8', newline='') as f:
  38.         writer = csv.writer(f, dialect='excel')
  39.         for v in nicknames:
  40.             writer.writerow([v])
  41.     after = time.time()
  42.     await bot.send_file(ctx.message.author, 'temp.csv', filename='stats.csv',
  43.                         content="Here you go! Check your PM's. Generated in {:.4}ms.".format((after - before)*1000))
  44.  
  45.  
  46. if __name__ == '__main__':
  47.     bot.run(TOKEN)
RAW Paste Data