Skip to content

Instantly share code, notes, and snippets.

@marios8543
Created August 17, 2018 13:29
Show Gist options
  • Select an option

  • Save marios8543/a63de75fa96b5741c826b62e0bfac212 to your computer and use it in GitHub Desktop.

Select an option

Save marios8543/a63de75fa96b5741c826b62e0bfac212 to your computer and use it in GitHub Desktop.
from pbot_utils import *
import aiohttp
BASE_URL = config['konishi_url']
async def handle_image(msg):
if len(msg.attachments)>0:
img = msg.attachments[0]
async with aiohttp.get(img.url) as r:
r = await r.read()
return {'data':r,'name':img.filename}
return
@client.group(pass_context=True)
async def konishi(ctx):
if not ctx.message.channel.is_private:
return await client.say(":negative_squared_cross_mark: You can only use Konishi in PM")
if not ctx.invoked_subcommand:
return await client.say(":negative_squared_cross_mark: Bad command")
@konishi.command(pass_context=True)
async def login(ctx):
creds={"username":"","password":""}
await client.say("""
Logging into Konishi through >PBot will bind your account to your Discord ID
If you agree proceed or else type `cancel`...""")
for i in creds:
await client.say("Enter your {}".format(i))
msg = await client.wait_for_message(author=ctx.message.author,channel=ctx.message.channel,timeout=120)
if msg==None or 'cancel' in msg.content:
return await client.say(":zzz: Cancelled...")
creds[i]=msg.content
await client.send_typing(ctx.message.channel)
async with aiohttp.post("{}/login".format(BASE_URL),params=creds) as resp:
r = await resp.json()
if resp.status==200:
await client.say(":white_check_mark: Login successfull!")
return await db.update(table='members',values={'konishi':r['access_token']},params={'id':ctx.message.author.id})
else:
return await client.say(":negative_squared_cross_mark: {}".format(r['message']))
@konishi.command(pass_context=True)
async def post(ctx):
tkn = db.select(table='members',fields=['konishi'],params={'id':ctx.message.author.id})
if not tkn:
return await client.say(":negative_squared_cross_mark: Not logged in...")
else:
tkn = tkn
await client.say(":pencil: Write your post. You can also include an image...")
msg = await client.wait_for_message(author=ctx.message.author,channel=ctx.message.channel,timeout=300)
if not msg:
return await client.say(":negative_squared_cross_mark: Cancelled...")
content = msg.content
img = handle_image(msg)
if img:
data = aiohttp.FormData()
data.add_field('file',img['data'],filename=img['name'])
async with session.post("{}/imageupload".format(BASE_URL),data=data) as resp:
r = await resp.json()
if resp.status==200:
img = r['image_id']
else:
return await client.say(":negative_squared_cross_mark: Image upload error: {}".format(r['message']))
async with aiohttp.post("{}/post".format(BASE_URL),params={'content':content,'image_id':img}) as resp:
r = await resp.json()
if resp.status==201:
return await client.say(":white_check_mark: Post created successfully!")
else:
return await clieny.say(":negative_squared_cross_mark: Error: {}".format(r['message']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment