Skip to content

Instantly share code, notes, and snippets.

@lap00zza lap00zza/lapzbotAPI.py
Last active Jul 9, 2016

Embed
What would you like to do?
lapzbotAPI
# coding=utf-8
"""
LAPZBOT Restful
Handles the inter process communications between Lapzbot and the Console, without having to rely on the database
for Server / User information. This is intended to be run in a thread.
Typical UseCase:
----------------
import lapzbotAPI
import discord
import threading
class Bot(discord.Client):
def __init__(self):
super().__init__()
t = threading.Thread(target=lapzbotAPI.run_server, args=[self])
t.start()
bot = Bot()
bot.run('token')
Available Endpoints:
-------------------
::Server Endpoints
GET /api_v1/server/<server_id>/
GET /api_v1/server/<server_id>/member_count/
GET /api_v1/server/<server_id>/member_list/
GET /api_v1/server/<server_id>/role_list/
GET /api_v1/server/<server_id>/channel_list/
::Member Endpoints
GET /api_v1/member/<member_id>/
:NOTE:
Member List, Channel List and User List are kept as separate endpoints by design choice.
:LICENSE:
Copyright 2016 Jewel Mahanta (@lapoozza, https://github.com/lap00zza)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from flask import Flask, jsonify
import os
import discord
app = Flask(__name__)
context = None
@app.route('/api_v1/server/<server_id>/', methods=['GET'])
def server_op(server_id):
"""
Returns Server related information.
:param server_id: Server ID
:return: Server related information containing: name, id, owner etc
:rtype: JSON
"""
try:
server = discord.utils.get(context.servers, id=server_id)
if server:
try:
region = server.region.value
except AttributeError:
region = server.region
try:
s_def = {"name": server.name,
"id": server.id,
"owner": {"name": server.owner.name,
"id": server.owner.id,
"avatar_url": server.owner.avatar_url,
"discriminator": server.owner.discriminator},
"large": server.large,
"icon_url": server.icon_url,
"created_at": server.created_at,
"member_count": server.member_count,
# "mfa_level": server.mfa_level,
"region": region}
return jsonify({"server": s_def}), 200
except Exception as e:
print(e)
return jsonify({"server": None}), 404
return jsonify({"server": None}), 404
except Exception as e:
print(e)
return jsonify({"server": None}), 404
@app.route('/api_v1/server/<server_id>/member_count/', methods=['GET'])
def member_count(server_id):
"""
Returns member count for a server.
:param server_id: Server ID
:return: The total number of members in the server
:rtype: JSON
"""
try:
server = discord.utils.get(context.servers, id=server_id)
if server:
try:
return jsonify({"member_count": server.member_count}), 200
except Exception as e:
print(e)
return jsonify({"member_count": None}), 404
return jsonify({"member_count": None}), 404
except Exception as e:
print(e)
return jsonify({"member_count": None}), 404
@app.route('/api_v1/server/<server_id>/member_list/', methods=['GET'])
def member_list(server_id):
"""
Returns member list for a server.
:param server_id: Server ID
:return: A list of all the members in the server
:rtype: JSON
"""
try:
server = discord.utils.get(context.servers, id=server_id)
m_dict = []
if server:
for m in server.members:
r_dict = []
roles = m.roles
for r in roles:
r_dict.append({"name": r.name, "id": r.id})
m_def = {"name": m.name,
"id": m.id,
"discriminator": m.discriminator,
"bot": m.bot,
"avatar_url": m.avatar_url,
"default_avatar_url": m.default_avatar_url,
"created_at": m.created_at,
"display_name": m.display_name,
"roles": r_dict,
"joined_at": m.joined_at,
"colour": m.colour.value,
"top_role": {"name": m.top_role.name, "id": m.top_role.id}}
m_dict.append(m_def)
try:
return jsonify({"member_list": m_dict}), 200
except Exception as e:
print(e)
return jsonify({"member_list": None}), 404
return jsonify({"member_list": None}), 404
except Exception as e:
print(e)
return jsonify({"member_list": None}), 404
@app.route('/api_v1/server/<server_id>/role_list/', methods=['GET'])
def role_list(server_id):
"""
Returns role list for a server.
:param server_id: Server ID
:return: A list of all the roles in the server
:rtype: JSON
"""
try:
server = discord.utils.get(context.servers, id=server_id)
r_dict = []
if server:
for r in server.roles:
r_dict.append({"name": r.name, "id": r.id, "colour": r.colour.value})
try:
return jsonify({"role_list": r_dict}), 200
except Exception as e:
print(e)
return jsonify({"role_list": None}), 404
return jsonify({"role_list": None}), 404
except Exception as e:
print(e)
return jsonify({"role_list": None}), 404
@app.route('/api_v1/server/<server_id>/channel_list/', methods=['GET'])
def channel_list(server_id):
"""
Returns channel list for a server.
:param server_id: Server ID
:return: A list of all the channels in the server
:rtype: JSON
"""
try:
server = discord.utils.get(context.servers, id=server_id)
c_dict = []
if server:
for c in server.channels:
c_dict.append({"name": c.name,
"id": c.id,
"topic": c.topic,
"type": c.type.value,
"created_at": c.created_at})
try:
return jsonify({"channel_list": c_dict}), 200
except Exception as e:
print(e)
return jsonify({"channel_list": None}), 404
return jsonify({"channel_list": None}), 404
except Exception as e:
print(e)
return jsonify({"channel_list": None}), 404
@app.route('/api_v1/member/<member_id>/', methods=['GET'])
def member(member_id):
"""
Returns member information
:param member_id: Member ID
:return: Member related information containing: name, id, avatar etc
:rtype: JSON
"""
try:
print(member_id)
m = discord.utils.get(context.get_all_members(), id=member_id)
if m:
try:
m_def = {"name": m.name,
"id": m.id,
"discriminator": m.discriminator,
"bot": m.bot,
"avatar_url": m.avatar_url,
"default_avatar_url": m.default_avatar_url,
"created_at": m.created_at,
"display_name": m.display_name}
return jsonify({"member": m_def}), 200
except Exception as e:
print(e)
return jsonify({"member": None}), 404
return jsonify({"member": None}), 404
except Exception as e:
print(e)
return jsonify({"member": None}), 404
def run_server(ctx):
"""
This method is called for running the server.
"""
global context
context = ctx
port = int(os.environ.get("PORT", 4000))
app.run(host='127.0.0.1', port=port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.