added last command which replicates your last delivery
This commit is contained in:
parent
3a4d6bf8b2
commit
4fad0f36d0
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1,5 +1,7 @@
|
||||||
# Token files
|
# Token files
|
||||||
*.token
|
*.token
|
||||||
|
# Last file
|
||||||
|
*.last
|
||||||
|
|
||||||
# ---> Python
|
# ---> Python
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
|
|
|
||||||
93
main.py
93
main.py
|
|
@ -4,6 +4,8 @@ import requests
|
||||||
import json
|
import json
|
||||||
from datetime import datetime as dt
|
from datetime import datetime as dt
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
import os
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
|
||||||
### reads token files
|
### reads token files
|
||||||
|
|
@ -22,11 +24,61 @@ async def postToSheet(data, gsheetToken):
|
||||||
return response.status
|
return response.status
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#creates the file if it doesn't exist. stores last command used by... users on this delivery.
|
||||||
|
async def writeDeliveryToCommandFile(author, command):
|
||||||
|
|
||||||
|
#shitty hack to see if the file is empty
|
||||||
|
path = os.getcwd() + "/" + lastCommandFile
|
||||||
|
empty = os.stat(path).st_size < 1
|
||||||
|
|
||||||
|
with open(lastCommandFile, 'r') as f:
|
||||||
|
if empty:
|
||||||
|
jsonContent = json.loads("{}")
|
||||||
|
|
||||||
|
else:
|
||||||
|
print("loading from file")
|
||||||
|
jsonContent = json.load(f)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
with open(lastCommandFile, 'w') as f:
|
||||||
|
print(jsonContent)
|
||||||
|
jsonContent[author] = command
|
||||||
|
json.dump(jsonContent, f)
|
||||||
|
|
||||||
|
async def getDataFromDeliveryFile(author):
|
||||||
|
path = os.getcwd() + "/" + lastCommandFile
|
||||||
|
empty = os.stat(path).st_size < 1
|
||||||
|
|
||||||
|
with open(lastCommandFile, 'r') as f:
|
||||||
|
if empty:
|
||||||
|
return None
|
||||||
|
|
||||||
|
else:
|
||||||
|
jsonContent = json.load(f)
|
||||||
|
if author in jsonContent.keys():
|
||||||
|
return jsonContent[author]
|
||||||
|
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async def delLastCommandFile():
|
||||||
|
open(lastCommandFile, 'w').close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bot = commands.Bot()
|
bot = commands.Bot()
|
||||||
|
|
||||||
# various tokens
|
# various tokens
|
||||||
discordToken = readToken("discord.token")
|
discordToken = readToken("discord.token")
|
||||||
gsheetToken = readToken("sheet.token")
|
gsheetToken = readToken("sheet.token")
|
||||||
|
lastCommandFile = "command.last"
|
||||||
|
|
||||||
# default command to see if the bot has lived
|
# default command to see if the bot has lived
|
||||||
@bot.listen()
|
@bot.listen()
|
||||||
|
|
@ -37,7 +89,7 @@ async def on_connect():
|
||||||
# guild IDs are both IDA servers
|
# guild IDs are both IDA servers
|
||||||
@bot.slash_command(
|
@bot.slash_command(
|
||||||
name="delivery",
|
name="delivery",
|
||||||
guild_ids=[401372086746087425],
|
# guild_ids=[401372086746087425],
|
||||||
description= "sends your delivery to the bot!"
|
description= "sends your delivery to the bot!"
|
||||||
)
|
)
|
||||||
async def delivery(ctx,
|
async def delivery(ctx,
|
||||||
|
|
@ -83,6 +135,7 @@ async def delivery(ctx,
|
||||||
if response == 200:
|
if response == 200:
|
||||||
await ctx.followup.send(f"your delivery of {quantity} of {commodity} to a {target} has been added to the sheet!")
|
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"[{dt.isoformat(dt.now())}]{author} delivery of {quantity} of {commodity} to a {target}")
|
||||||
|
await writeDeliveryToCommandFile(author, data)
|
||||||
else:
|
else:
|
||||||
await ctx.followup.send(f"Failed to log delivery (HTTP {response}). Please contact the yellow people if that keeps happening")
|
await ctx.followup.send(f"Failed to log delivery (HTTP {response}). Please contact the yellow people if that keeps happening")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
@ -90,11 +143,45 @@ async def delivery(ctx,
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@bot.slash_command(
|
||||||
|
name="last",
|
||||||
|
# guild_ids=[401372086746087425],
|
||||||
|
description= "send the last delivery you made again!"
|
||||||
|
)
|
||||||
|
async def last(ctx):
|
||||||
|
|
||||||
|
author = str(ctx.author)
|
||||||
|
author = author[:author.find(" ")]
|
||||||
|
|
||||||
|
data = await getDataFromDeliveryFile(author)
|
||||||
|
|
||||||
|
await ctx.defer()
|
||||||
|
|
||||||
|
|
||||||
|
if data is not None:
|
||||||
|
try:
|
||||||
|
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']}")
|
||||||
|
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")
|
||||||
|
except Exception as e:
|
||||||
|
await ctx.followup.send(f"help : Error: {e}")
|
||||||
|
else:
|
||||||
|
await ctx.followup.send(f"You haven't delivered to this build yet, or I have amnesia.")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#change url command
|
#change url command
|
||||||
@bot.slash_command(
|
@bot.slash_command(
|
||||||
name="change-sheet-url",
|
name="change-sheet-url",
|
||||||
guild_ids=[401372086746087425],
|
# guild_ids=[401372086746087425],
|
||||||
description= "changes the URL of the active sheet, use with caution"
|
description= "changes the URL of the active sheet, use with caution"
|
||||||
)
|
)
|
||||||
async def changeSheetUrl(ctx,
|
async def changeSheetUrl(ctx,
|
||||||
|
|
@ -108,6 +195,8 @@ async def changeSheetUrl(ctx,
|
||||||
|
|
||||||
await ctx.followup.send(f"URL has been set to {url}")
|
await ctx.followup.send(f"URL has been set to {url}")
|
||||||
gsheetToken = readToken("sheet.token")
|
gsheetToken = readToken("sheet.token")
|
||||||
|
await delLastCommandFile()
|
||||||
|
print("deleted command.last file content")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
await ctx.followup.send(f"something shat the bed")
|
await ctx.followup.send(f"something shat the bed")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user