Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import discord
  2. import time
  3. import asyncio
  4. #import os, use to hide the bot token in the the .env file, retrive it and use it here
  5. #import os (rewrite doesn't allow .env to hide token, or it has changed)
  6. #serverless sql
  7. import sqlite3
  8.  
  9. #id = 204534035299434497
  10.  
  11. #read the token.txt file, get first line with read mode, return first line as it is, save it as token
  12. def read_token():
  13. with open("token.txt", "r") as f:
  14. lines = f.readlines()
  15. return lines[0].strip()
  16.  
  17. token = read_token()
  18.  
  19. messages = joined = 0
  20.  
  21. client = discord.Client()
  22.  
  23. async def update_stats():
  24. await client.wait_until_ready()
  25. global messages, joined
  26.  
  27. #run while the client isn't closed
  28. while not client.is_closed():
  29. try:
  30. #open the stats.txt file and append (add to the end of) it
  31. with open("stats.txt" "a") as f:
  32. f.write(f"Time: {int(time.time())}, Messages: {messages}, Members Joined: {joined}\n")
  33.  
  34. messages = 0
  35. joined = 0
  36. #delay in seconds
  37. await asyncio.sleep(3600)
  38.  
  39. except Exception as e:
  40. print(e)
  41. await asyncio.sleep(3600)
  42.  
  43. @client.event
  44. async def on_ready():
  45. print('We have logged in as {0.user}'.format(client))
  46.  
  47. @client.event
  48. async def on_message(message):
  49. #serveridnumber = client.get_guild(204534035299434497)
  50. #make it so that this will only run in the channel "commands"
  51. #channels = ["commands"]
  52. #this limit commands to name of userid in valid users, useful for admin of server and donors
  53. #valid_users = ["userid"]
  54. #if str(message.channel) in channels and str(message.author) in valid_users:
  55. #increase the messages variable by one
  56. #global messages
  57. #messages += 1
  58. #print all message to console
  59. #print(message.content)
  60. if message.author == client.user:
  61. return
  62. elif message.content.find('!hello') != -1:
  63. await message.channel.send('Hello!')
  64. elif message.content == "!users":
  65. await message.channel.send("""# of Members {serveridnumber.member_count}""")
  66. elif message.content == "!help":
  67. embed = discord.Embed(title="Help on BOT", descrption="Some useful commands")
  68. embed.add_field(name="!hello", value="Greets the user")
  69. embed.add_field(name="!users", value="Prints number of users")
  70. await message.channel.send(content=None, embed=embed)
  71. else:
  72. print(f"""User:{message.author} tried to do command {message.content}, in channel {message.channel}""")
  73.  
  74.  
  75.  
  76.  
  77. @client.event
  78. async def on_member_join(members):
  79. #increase the joined variable by one
  80. #global joined
  81. #joined += 1
  82. for channel in member.server.channels:
  83. if str(channel) == "general":
  84. await client.send_message(f"""Welcome {member.mention}""")
  85.  
  86. @client.event
  87. async def on_message(message):
  88.  
  89. #this will make the update_stats function run as a background loop
  90. client.loop.create_task(update_stats())
  91.  
  92. client.run(token)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement