Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8aa61a109e | |||
| 08e0d41a09 |
295
main.py
295
main.py
|
|
@ -1,11 +1,13 @@
|
|||
import discord
|
||||
from discord.ext import commands
|
||||
from discord.ext import commands, tasks
|
||||
from discord.commands import option
|
||||
import requests
|
||||
import json
|
||||
from datetime import datetime as dt
|
||||
from datetime import datetime
|
||||
import aiohttp
|
||||
import os
|
||||
import asyncio
|
||||
from math import floor
|
||||
|
||||
|
||||
### reads token files
|
||||
|
|
@ -15,13 +17,69 @@ def readToken(filePath):
|
|||
|
||||
|
||||
|
||||
### sends a get request to the sheet
|
||||
async def getFromSheet(gsheetToken, data):
|
||||
|
||||
params = {"type":data}
|
||||
print(params)
|
||||
async with session.get(gsheetToken, params=params) as response:
|
||||
print("Status:", response.status)
|
||||
text = await response.text()
|
||||
print(text)
|
||||
return text
|
||||
|
||||
|
||||
### takes a json, returns a http code
|
||||
async def postToSheet(data, gsheetToken):
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(gsheetToken, json=data) as response:
|
||||
# print("Status:", response.status)
|
||||
return response.status
|
||||
async with session.post(gsheetToken, json=data) as response:
|
||||
text = await response.text()
|
||||
print(text)
|
||||
print("Status:", response.status)
|
||||
return response.status
|
||||
|
||||
|
||||
### carrier state parser, takes the values from the sheet and turn it into something we can use
|
||||
|
||||
async def parseCarrierState(state):
|
||||
tupleList = []
|
||||
splitState = state.split(",")
|
||||
|
||||
for i in range(0,len(splitState),2):
|
||||
carrier = splitState[i]
|
||||
state = splitState[i+1]
|
||||
if carrier != "" and state != "":
|
||||
tupleList.append((carrier, state))
|
||||
|
||||
return tupleList
|
||||
|
||||
### converts a list of tuple back to the sheet syntax (CSV)
|
||||
async def toRawState(tupleList):
|
||||
rawState = []
|
||||
for value in tupleList:
|
||||
for subval in value:
|
||||
rawState.append([subval])
|
||||
rawLen = len(rawState)
|
||||
|
||||
for i in range(rawLen, 41):
|
||||
rawState.append([""])
|
||||
|
||||
return rawState
|
||||
|
||||
|
||||
async def getListOfCarriers(state):
|
||||
splitState = state.split(",")
|
||||
carrierList = []
|
||||
for i in range(0,len(splitState),2):
|
||||
carrier = splitState[i]
|
||||
if carrier != "":
|
||||
carrierList.append(carrier)
|
||||
|
||||
return carrierList
|
||||
|
||||
### used to query the carrier state internally
|
||||
async def getInternalCarrierState():
|
||||
response = await getFromSheet(gsheetToken, "carrier")
|
||||
return response
|
||||
|
||||
|
||||
|
||||
|
|
@ -71,6 +129,7 @@ async def delLastCommandFile():
|
|||
|
||||
|
||||
|
||||
|
||||
bot = commands.Bot()
|
||||
|
||||
# various tokens
|
||||
|
|
@ -78,10 +137,21 @@ discordToken = readToken("discord.token")
|
|||
gsheetToken = readToken("sheet.token")
|
||||
lastCommandFile = "command.last"
|
||||
|
||||
|
||||
|
||||
# default command to see if the bot has lived
|
||||
@bot.listen()
|
||||
async def on_connect():
|
||||
global session
|
||||
print("I'm alive, bitch")
|
||||
session = aiohttp.ClientSession()
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
print("I'm ready, bitch")
|
||||
writeToWhatsLeftToHaul.start()
|
||||
|
||||
|
||||
|
||||
# delivery command
|
||||
# guild IDs are both IDA servers
|
||||
|
|
@ -122,6 +192,7 @@ async def delivery(ctx,
|
|||
await ctx.defer()
|
||||
|
||||
data = {
|
||||
"postType":"delivery",
|
||||
"username":author,
|
||||
"commodity": commodity,
|
||||
"quantity": quantity,
|
||||
|
|
@ -132,7 +203,7 @@ async def delivery(ctx,
|
|||
response = await postToSheet(data, gsheetToken)
|
||||
if response == 200:
|
||||
await ctx.followup.send(f"your delivery of {quantity} of {commodity} to a {target} has been added to the sheet!")
|
||||
print(f"[{dt.isoformat(dt.now())}]{author} delivery of {quantity} of {commodity} to a {target}")
|
||||
print(f"[{datetime.isoformat(datetime.now())}]{author} delivery of {quantity} of {commodity} to a {target}")
|
||||
await writeDeliveryToCommandFile(author, data)
|
||||
else:
|
||||
await ctx.followup.send(f"Failed to log delivery (HTTP {response}). Please contact the yellow people if that keeps happening")
|
||||
|
|
@ -159,8 +230,9 @@ async def misc(ctx,
|
|||
await ctx.defer()
|
||||
|
||||
data = {
|
||||
"postType":"delivery",
|
||||
"username":author,
|
||||
"commodity": "Miscellaneous",
|
||||
"commodity": commodity,
|
||||
"quantity": quantity,
|
||||
"target": target
|
||||
}
|
||||
|
|
@ -169,7 +241,7 @@ async def misc(ctx,
|
|||
response = await postToSheet(data, gsheetToken)
|
||||
if response == 200:
|
||||
await ctx.followup.send(f"your delivery of {quantity} of Miscellaneous stuff to a {target} has been added to the sheet!")
|
||||
print(f"[{dt.isoformat(dt.now())}]{author} delivery of {quantity} of Miscellaneous to a {target}")
|
||||
print(f"[{datetime.isoformat(datetime.now())}]{author} delivery of {quantity} of Miscellaneous to a {target}")
|
||||
await writeDeliveryToCommandFile(author, data)
|
||||
else:
|
||||
await ctx.followup.send(f"Failed to log delivery (HTTP {response}). Please contact the yellow people if that keeps happening")
|
||||
|
|
@ -179,6 +251,7 @@ async def misc(ctx,
|
|||
|
||||
|
||||
|
||||
|
||||
@bot.slash_command(
|
||||
name="last",
|
||||
# guild_ids=[401372086746087425],
|
||||
|
|
@ -199,7 +272,7 @@ async def last(ctx):
|
|||
response = await postToSheet(data, gsheetToken)
|
||||
if response == 200:
|
||||
await ctx.followup.send(f"your delivery of {data['quantity']} of {data['commodity']} to a {data['target']} has been added to the sheet!")
|
||||
print(f"[{dt.isoformat(dt.now())}] delivery of {data['quantity']} of {data['commodity']} to a {data['target']}")
|
||||
print(f"[{datetime.isoformat(datetime.now())}] delivery of {data['quantity']} of {data['commodity']} to a {data['target']}")
|
||||
await writeDeliveryToCommandFile(author, data)
|
||||
else:
|
||||
await ctx.followup.send(f"Failed to log delivery (HTTP {response}). Please contact the yellow people if that keeps happening")
|
||||
|
|
@ -211,9 +284,6 @@ async def last(ctx):
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#change url command
|
||||
@bot.slash_command(
|
||||
name="change-sheet-url",
|
||||
|
|
@ -235,6 +305,205 @@ async def changeSheetUrl(ctx,
|
|||
print("deleted command.last file content")
|
||||
except Exception as e:
|
||||
await ctx.followup.send(f"something shat the bed")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@bot.slash_command(
|
||||
name="getcarrierstate",
|
||||
# guild_ids=[401372086746087425],
|
||||
description= "gets the carriers state from the sheet"
|
||||
)
|
||||
async def getCarrierState(ctx):
|
||||
response = await getInternalCarrierState()
|
||||
carrierState = await parseCarrierState(response)
|
||||
await ctx.defer()
|
||||
|
||||
text = "Current Carrier State : \n"
|
||||
|
||||
for carrier, state in carrierState:
|
||||
text += f"{carrier} is {state}\n"
|
||||
|
||||
await ctx.followup.send(text)
|
||||
|
||||
|
||||
async def getCarriers(ctx: discord.AutocompleteContext):
|
||||
state = await getInternalCarrierState()
|
||||
carrierList = await getListOfCarriers(state)
|
||||
# print(carrierList)
|
||||
# return [carrier for carriers in carrierList]
|
||||
return carrierList
|
||||
|
||||
|
||||
@bot.slash_command(
|
||||
name="setcarrierstate",
|
||||
# guild_ids=[401372086746087425],
|
||||
description= "Sets a carrier state on the sheet"
|
||||
)
|
||||
@option("carrier", description="The carrier you want to edit", autocomplete=getCarriers)
|
||||
@option("state",required=True, description="The carrier you want to edit", choices=["Loading","Unloading","Full","Empty","Inactive"])
|
||||
async def setCarrierState(ctx, carrier, state):
|
||||
response = await getFromSheet(gsheetToken)
|
||||
carrierState = await parseCarrierState(response)
|
||||
|
||||
newCarrierState = []
|
||||
|
||||
print(f"requested values: {carrier} to {state}")
|
||||
|
||||
print(f"current values : {carrierState}")
|
||||
|
||||
for carrierS, stateS in carrierState:
|
||||
if carrier == carrierS:
|
||||
newCarrierState.append((carrier,state))
|
||||
else:
|
||||
newCarrierState.append((carrierS,stateS))
|
||||
|
||||
print(f"new values : {newCarrierState}")
|
||||
|
||||
rawState = await toRawState(newCarrierState)
|
||||
|
||||
print(rawState)
|
||||
|
||||
await ctx.defer()
|
||||
i = 1
|
||||
text = "Current Carrier State : \n"
|
||||
|
||||
|
||||
data = {
|
||||
"postType":"carrier",
|
||||
"carrierState": rawState
|
||||
|
||||
}
|
||||
|
||||
|
||||
try:
|
||||
response = await postToSheet(data, gsheetToken)
|
||||
if response == 200:
|
||||
await ctx.followup.send(f"hey")
|
||||
else:
|
||||
await ctx.followup.send(f"i fucked up ")
|
||||
except Exception as e:
|
||||
await ctx.followup.send(f"help : Error: {e}")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@bot.slash_command(
|
||||
name="lefttohaul",
|
||||
# guild_ids=[401372086746087425],
|
||||
description= "gets the what's left to haul from the sheet"
|
||||
)
|
||||
async def lefttohaul(ctx):
|
||||
response = await getFromSheet(gsheetToken, "commodity")
|
||||
|
||||
await ctx.defer()
|
||||
|
||||
text = "\n\n\n### Here's what's left to haul! Get to work!\n"
|
||||
|
||||
commodity, values = response.split("|")
|
||||
|
||||
commodity = commodity.split(",")
|
||||
values = values.split(",")
|
||||
finalDict = {}
|
||||
|
||||
for i in range(len(commodity)):
|
||||
finalDict[commodity[i]] = int(values[i])
|
||||
|
||||
finalDict = dict(sorted(finalDict.items(), key=lambda item: item[1], reverse=True))
|
||||
|
||||
|
||||
for key in finalDict:
|
||||
if finalDict[key] > 0:
|
||||
text += f"{key} : {finalDict[key]}\n"
|
||||
|
||||
await ctx.followup.send(text)
|
||||
|
||||
|
||||
|
||||
@tasks.loop(seconds=60)
|
||||
async def writeToWhatsLeftToHaul():
|
||||
response = await getFromSheet(gsheetToken, "commodity")
|
||||
print("test")
|
||||
text = "```"
|
||||
|
||||
commodity, values = response.split("|")
|
||||
|
||||
commodity = commodity.split(",")
|
||||
values = values.split(",")
|
||||
finalDict = {}
|
||||
|
||||
for i in range(len(commodity)):
|
||||
finalDict[commodity[i]] = int(values[i])
|
||||
|
||||
finalDict = dict(sorted(finalDict.items(), key=lambda item: item[1], reverse=True))
|
||||
|
||||
|
||||
for key in finalDict:
|
||||
if finalDict[key] > 0:
|
||||
text += f"{key} : {finalDict[key]}\n"
|
||||
|
||||
channel = bot.get_channel(1519457137234022460)
|
||||
# await channel.purge(limit=1)
|
||||
|
||||
text += "```"
|
||||
|
||||
embed = discord.Embed(title="Estimated remaining amounts and where to get them",
|
||||
colour=discord.Colour(0x29ac08),
|
||||
description=text,
|
||||
timestamp=datetime.now())
|
||||
embed.set_thumbnail(url="https://cdn.discordapp.com/emojis/536647643367997492.png")
|
||||
|
||||
embed.set_footer(text="This message updates every 60 seconds(ish). Last updated at")
|
||||
# if 'station_name' in config.config and 'station_link' in config.config:
|
||||
# embed.set_author(name=u'\U0001F517 ' + config.config['station_name'] + u' \U0001F517',
|
||||
# url=config.config['station_link'])
|
||||
|
||||
|
||||
channel = bot.get_channel(1519457137234022460)
|
||||
found = False
|
||||
async for oldmsg in channel.history(limit=2):
|
||||
if oldmsg.author == bot.user:
|
||||
found = True
|
||||
await oldmsg.edit(embed=embed)
|
||||
break
|
||||
if not found:
|
||||
await channel.send(embed=embed)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user