Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.78 KB | None | 0 0
  1. # import pip
  2. # pip.main(['install', "discord.py"])
  3. import discord
  4. import asyncio
  5. import json
  6.  
  7. client = discord.Client()
  8.  
  9.  
  10. def setup():
  11.     global gl_userArray
  12.     global gl_receiver_list
  13.     global gl_places
  14.     global gl_place_name
  15.     gl_place_name = ""
  16.  
  17.     def user_load():  # Load userdata
  18.         global gl_userArray
  19.         try:
  20.             with open("user.json", "r") as file:
  21.                 gl_userArray = json.loads(file.read())
  22.         except:
  23.             print("Keine Benutzerdatenbank gefunden. Benutzerdatenbank wird erstellt.")
  24.             with open("user.json", "w") as file:
  25.                 gl_userArray = {}
  26.  
  27.     user_load()
  28.  
  29.     def world_load():  # load worlddata
  30.         global gl_places
  31.         try:
  32.             with open("world.json", "r") as file:
  33.                 gl_places = json.loads(file.read())
  34.         except:
  35.             print("Keine Weltdatenbank gefunden. Weltdatenbank wird erstellt.")
  36.             with open("world.json", "w") as file:
  37.                 gl_places = {}
  38.  
  39.     world_load()
  40.     global gl_receiver
  41.     global gl_content
  42.     gl_receiver = client.user
  43.     gl_content = ""
  44.  
  45.  
  46. @client.event
  47. @asyncio.coroutine
  48. def on_ready():
  49.     print('Logged in as')
  50.     print(client.user.name)
  51.     print(client.user.id)
  52.     print('------')
  53.     setup()
  54.  
  55.  
  56. @client.event
  57. @asyncio.coroutine
  58. def on_message(message):
  59.     global gl_receiver
  60.     global gl_content
  61.     global gl_content_list
  62.     global gl_places
  63.     global gl_userArray
  64.     global gl_place_name
  65.     print(str(message.author) + " : " + message.content)
  66.     if str(message.author.id) != client.user.id:  # if author is not the Bot
  67.         if commands(message.author, message.channel, message.content, message.timestamp, "d"):  # If command is executed
  68.             if gl_place_name != "":  # if new place is added
  69.                 try:  # create new channel and dumps the worlddata to file
  70.                     channel = yield from client.create_channel(message.server, gl_place_name)
  71.                     gl_places[gl_place_name][0]["channel_id"] = channel.id
  72.                     gl_places[gl_place_name][0]["user"].append(gl_places[gl_place_name][0]["channel_id"])
  73.                     dump_array("world.json", gl_places)
  74.                 except:  # if the user is not on a server
  75.                     gl_receiver = message.author
  76.                     gl_content = gl_receiver.mention + "Please execute this command on a server!"
  77.                     try:  # remove place out of worlddata
  78.                         gl_places.pop(gl_place_name)
  79.                     except:
  80.                         print("")
  81.                 gl_place_name = ""
  82.             # print(gl_content)
  83.             yield from client.send_message(gl_receiver, gl_content)  # Return only to the user
  84.             gl_content = "sdjfkl öasdljal"
  85.         else:  # if command isnt executed
  86.             # to which user should the message be sent?
  87.             user_place = gl_userArray[message.author.id][0]["place"]  # present users
  88.             gl_content_list = gl_places[user_place][0]["user"]  # list with user ids
  89.             # print(gl_content_list)
  90.             for receiver in gl_content_list:
  91.                 receiver = discord.Object(receiver)  # user id to user object
  92.                 # print(receiver)
  93.                 content = gl_userArray[message.author.id][0]["nickname"] + ": " + message.content
  94.                 try:
  95.                     if not message.content.startswith("%"):  # if message starts with % it will not send
  96.                         if message.author.id != receiver.id:
  97.                             yield from client.send_message(receiver, content)  # send message to current receiver
  98.                             yield from client.add_reaction(message, "📲")  # send reaction to sender
  99.                 except:  # i dont know what this does
  100.                     user = yield from client.get_user_info(receiver.id)
  101.                     yield from client.send_message(user, content)
  102.  
  103.  
  104. def commands(author, channel, content, timestamp, app):
  105.     global gl_userArray
  106.     global gl_receiver
  107.     global gl_content
  108.     global gl_place_name
  109.     # Commands
  110.     if content.startswith(".register"):
  111.         register(author, channel, app)
  112.         return True
  113.     if content.startswith(".addPlace") or content.startswith(".addplace"):
  114.         arguments = content.split()
  115.         gl_place_name = arguments[1]
  116.         add_place(author, channel, arguments[1], arguments[2], arguments[3], arguments[4], arguments[5])
  117.         return True
  118.     if content.startswith(".changeLocation") or content.startswith(".changelocation"):
  119.         arguments = content.split()
  120.         change_location(arguments, author)
  121.         return True
  122.     if content.startswith(".info") or content.startswith(".i"):
  123.         info(author, channel)
  124.         return True
  125.     # if no command is executed
  126.     return False
  127.  
  128.  
  129. def add_place(author, answer_channel, place_name, travel_locations, hear_locations, command_list, emoji):
  130.     global gl_places
  131.     global gl_content
  132.     global gl_receiver
  133.     global gl_place_name
  134.     user = []
  135.     for i in gl_places:  # is place already existing?
  136.         if place_name == i:
  137.             gl_content = author.mention + " This place already exists!"
  138.             gl_receiver = answer_channel
  139.             gl_place_name = ""
  140.             return
  141.     gl_places[place_name] = [
  142.         {"name": place_name, "travel_locations": json.loads(travel_locations),
  143.          "hear_locations": json.loads(hear_locations), "commands": json.loads(command_list),
  144.          "user": user, "channel_id": "", "emoji": emoji}]
  145.  
  146.     gl_receiver = answer_channel
  147.     gl_content = author.mention + " " + place_name + " added successfully."
  148.     return
  149.  
  150.  
  151. def change_location(arguments, user):
  152.     global gl_places
  153.     global gl_content
  154.     global gl_receiver
  155.     global gl_userArray
  156.     user_place = gl_userArray[user.id][0]["place"]
  157.     destination = arguments[1]
  158.  
  159.     for place in gl_places[user_place][0]["travel_locations"]:  # Can user travel to his destination?
  160.         # print(place)
  161.         if place == destination or gl_places[place][0]["emoji"] == destination:  # place name or emoji
  162.             try:
  163.                 gl_places[user_place][0]["user"].remove(user.id)  # remove user from current location
  164.             except:
  165.                 print("")
  166.             gl_places[place][0]["user"].append(user.id)  # add user to new location
  167.             gl_userArray[user.id][0]["place"] = place  # change place in userdata
  168.             gl_content = "```Welcome to " + place + "! Type \".info\" for info```"
  169.             gl_receiver = user
  170.             dump_array("world.json", gl_places)
  171.             dump_array("user.json", gl_userArray)
  172.             return
  173.     # if user cant travel:
  174.     gl_content = "You can not travel to " + destination + ". Type .info to get information about your current location."
  175.     gl_receiver = user
  176.  
  177.  
  178. def info(user, channel):
  179.     global gl_userArray
  180.     global gl_receiver
  181.     global gl_places
  182.     global gl_content
  183.     gl_receiver = channel  # destination channel (mostly a user)
  184.     user_place = gl_userArray[user.id][0]["place"]
  185.     gl_content = "You are in **" + user_place + "**. \n From here you can travel to these places:\n"
  186.     # print(gl_places[user_place])
  187.     for location in gl_places[user_place][0]["travel_locations"]:
  188.         emoji = gl_places[location][0]["emoji"]
  189.         gl_content = gl_content + "-" + emoji + location + "\n"
  190.     gl_content = gl_content + "\n From here you can execute this commands:\n"
  191.     for command in gl_places[user_place][0]["commands"]:
  192.         gl_content = gl_content + "-" + command + "\n"
  193.  
  194.  
  195. def register(user, channel, app):
  196.     global gl_userArray
  197.     global gl_receiver
  198.     global gl_content
  199.     global gl_places
  200.     bol_register = True
  201.     for i in gl_userArray:  # Userdatenbank durchsuchen
  202.         if user.id == i:
  203.             if gl_userArray[i][0]["app"] == app:
  204.                 gl_content = user.mention + " You have already registered. Look in your direct messages!"
  205.                 gl_receiver = channel
  206.                 return bol_register
  207.     gl_userArray[user.id] = [{"name": user.name, "discriminator": user.discriminator, "app": app,
  208.                               "nickname": nickname(user), "place": "the-market"}]
  209.     # print(type(gl_places["Markt"][0]["user"]))
  210.     gl_places["the-market"][0]["user"].append(user.id)
  211.     dump_array("world.json", gl_places)
  212.     bol_register = False
  213.     gl_receiver = channel
  214.     dump_array("user.json", gl_userArray)
  215.     gl_content = user.mention + " You signed in successfully!"
  216.     return bol_register
  217.  
  218.  
  219. def nickname(user):
  220.     return user.name
  221.  
  222.  
  223. def dump_array(file, array):
  224.     with open(file, "w") as file:
  225.         file.write(json.dumps(array))
  226.  
  227.  
  228. client.run("Mzk4NTk4MjYyNTI2ODM2NzM3.DTA3Hg.gDBbb042WpxPe5fTCveSX7bL8X8")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement