Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import pip
- # pip.main(['install', "discord.py"])
- import discord
- import asyncio
- import json
- client = discord.Client()
- def setup():
- global gl_userArray
- global gl_receiver_list
- global gl_places
- global gl_place_name
- gl_place_name = ""
- def user_load(): # Load userdata
- global gl_userArray
- try:
- with open("user.json", "r") as file:
- gl_userArray = json.loads(file.read())
- except:
- print("Keine Benutzerdatenbank gefunden. Benutzerdatenbank wird erstellt.")
- with open("user.json", "w") as file:
- gl_userArray = {}
- user_load()
- def world_load(): # load worlddata
- global gl_places
- try:
- with open("world.json", "r") as file:
- gl_places = json.loads(file.read())
- except:
- print("Keine Weltdatenbank gefunden. Weltdatenbank wird erstellt.")
- with open("world.json", "w") as file:
- gl_places = {}
- world_load()
- global gl_receiver
- global gl_content
- gl_receiver = client.user
- gl_content = ""
- @client.event
- @asyncio.coroutine
- def on_ready():
- print('Logged in as')
- print(client.user.name)
- print(client.user.id)
- print('------')
- setup()
- @client.event
- @asyncio.coroutine
- def on_message(message):
- global gl_receiver
- global gl_content
- global gl_content_list
- global gl_places
- global gl_userArray
- global gl_place_name
- print(str(message.author) + " : " + message.content)
- if str(message.author.id) != client.user.id: # if author is not the Bot
- if commands(message.author, message.channel, message.content, message.timestamp, "d"): # If command is executed
- if gl_place_name != "": # if new place is added
- try: # create new channel and dumps the worlddata to file
- channel = yield from client.create_channel(message.server, gl_place_name)
- gl_places[gl_place_name][0]["channel_id"] = channel.id
- gl_places[gl_place_name][0]["user"].append(gl_places[gl_place_name][0]["channel_id"])
- dump_array("world.json", gl_places)
- except: # if the user is not on a server
- gl_receiver = message.author
- gl_content = gl_receiver.mention + "Please execute this command on a server!"
- try: # remove place out of worlddata
- gl_places.pop(gl_place_name)
- except:
- print("")
- gl_place_name = ""
- # print(gl_content)
- yield from client.send_message(gl_receiver, gl_content) # Return only to the user
- gl_content = "sdjfkl öasdljal"
- else: # if command isnt executed
- # to which user should the message be sent?
- user_place = gl_userArray[message.author.id][0]["place"] # present users
- gl_content_list = gl_places[user_place][0]["user"] # list with user ids
- # print(gl_content_list)
- for receiver in gl_content_list:
- receiver = discord.Object(receiver) # user id to user object
- # print(receiver)
- content = gl_userArray[message.author.id][0]["nickname"] + ": " + message.content
- try:
- if not message.content.startswith("%"): # if message starts with % it will not send
- if message.author.id != receiver.id:
- yield from client.send_message(receiver, content) # send message to current receiver
- yield from client.add_reaction(message, "📲") # send reaction to sender
- except: # i dont know what this does
- user = yield from client.get_user_info(receiver.id)
- yield from client.send_message(user, content)
- def commands(author, channel, content, timestamp, app):
- global gl_userArray
- global gl_receiver
- global gl_content
- global gl_place_name
- # Commands
- if content.startswith(".register"):
- register(author, channel, app)
- return True
- if content.startswith(".addPlace") or content.startswith(".addplace"):
- arguments = content.split()
- gl_place_name = arguments[1]
- add_place(author, channel, arguments[1], arguments[2], arguments[3], arguments[4], arguments[5])
- return True
- if content.startswith(".changeLocation") or content.startswith(".changelocation"):
- arguments = content.split()
- change_location(arguments, author)
- return True
- if content.startswith(".info") or content.startswith(".i"):
- info(author, channel)
- return True
- # if no command is executed
- return False
- def add_place(author, answer_channel, place_name, travel_locations, hear_locations, command_list, emoji):
- global gl_places
- global gl_content
- global gl_receiver
- global gl_place_name
- user = []
- for i in gl_places: # is place already existing?
- if place_name == i:
- gl_content = author.mention + " This place already exists!"
- gl_receiver = answer_channel
- gl_place_name = ""
- return
- gl_places[place_name] = [
- {"name": place_name, "travel_locations": json.loads(travel_locations),
- "hear_locations": json.loads(hear_locations), "commands": json.loads(command_list),
- "user": user, "channel_id": "", "emoji": emoji}]
- gl_receiver = answer_channel
- gl_content = author.mention + " " + place_name + " added successfully."
- return
- def change_location(arguments, user):
- global gl_places
- global gl_content
- global gl_receiver
- global gl_userArray
- user_place = gl_userArray[user.id][0]["place"]
- destination = arguments[1]
- for place in gl_places[user_place][0]["travel_locations"]: # Can user travel to his destination?
- # print(place)
- if place == destination or gl_places[place][0]["emoji"] == destination: # place name or emoji
- try:
- gl_places[user_place][0]["user"].remove(user.id) # remove user from current location
- except:
- print("")
- gl_places[place][0]["user"].append(user.id) # add user to new location
- gl_userArray[user.id][0]["place"] = place # change place in userdata
- gl_content = "```Welcome to " + place + "! Type \".info\" for info```"
- gl_receiver = user
- dump_array("world.json", gl_places)
- dump_array("user.json", gl_userArray)
- return
- # if user cant travel:
- gl_content = "You can not travel to " + destination + ". Type .info to get information about your current location."
- gl_receiver = user
- def info(user, channel):
- global gl_userArray
- global gl_receiver
- global gl_places
- global gl_content
- gl_receiver = channel # destination channel (mostly a user)
- user_place = gl_userArray[user.id][0]["place"]
- gl_content = "You are in **" + user_place + "**. \n From here you can travel to these places:\n"
- # print(gl_places[user_place])
- for location in gl_places[user_place][0]["travel_locations"]:
- emoji = gl_places[location][0]["emoji"]
- gl_content = gl_content + "-" + emoji + location + "\n"
- gl_content = gl_content + "\n From here you can execute this commands:\n"
- for command in gl_places[user_place][0]["commands"]:
- gl_content = gl_content + "-" + command + "\n"
- def register(user, channel, app):
- global gl_userArray
- global gl_receiver
- global gl_content
- global gl_places
- bol_register = True
- for i in gl_userArray: # Userdatenbank durchsuchen
- if user.id == i:
- if gl_userArray[i][0]["app"] == app:
- gl_content = user.mention + " You have already registered. Look in your direct messages!"
- gl_receiver = channel
- return bol_register
- gl_userArray[user.id] = [{"name": user.name, "discriminator": user.discriminator, "app": app,
- "nickname": nickname(user), "place": "the-market"}]
- # print(type(gl_places["Markt"][0]["user"]))
- gl_places["the-market"][0]["user"].append(user.id)
- dump_array("world.json", gl_places)
- bol_register = False
- gl_receiver = channel
- dump_array("user.json", gl_userArray)
- gl_content = user.mention + " You signed in successfully!"
- return bol_register
- def nickname(user):
- return user.name
- def dump_array(file, array):
- with open(file, "w") as file:
- file.write(json.dumps(array))
- client.run("Mzk4NTk4MjYyNTI2ODM2NzM3.DTA3Hg.gDBbb042WpxPe5fTCveSX7bL8X8")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement