bot.py
· 2.0 KiB · Python
Raw
#!/usr/bin/env python3
from requests import get
from interactions import slash_command, SlashContext, Client, slash_option, OptionType, auto_defer, Embed, check, AutocompleteContext, listen
from os import environ
suburbs = {f"{d['locality']} {d['state']}": d for d in get("https://www.matthewproctor.com/Content/postcodes/australian_postcodes.json").json()}
bot = Client(token=environ["BOT_TOKEN"])
guilds = [353071720342487040]
@slash_command(name="fuel", description="Check Fair Fuel prices (RACQ)", scopes=guilds)
@slash_option(name="location", description="Suburb", required=True, opt_type=OptionType.STRING, autocomplete=True)
async def fuel(ctx: SlashContext, location: str):
suburb = suburbs[location]
fair_response = get("https://www.racq.com.au/ajaxpages/fuelprice/fairfuelpricesapi.ashx", params={"lat": suburb["lat"], "lng": suburb["long"], "fueltype": 37}).json()
# area_response = get(f"https://www.racq.com.au/ajaxpages/fuelprice/fuelpricesapi.ashx?lat={suburb['lat']}&lng={suburb['long']}&fueltype=37").json()
embed = Embed(title="Area prices").add_field(name="RACQ", value=f"https://www.racq.com.au/car/fair-fuel-prices?phracq_body_0_phracq_contentcontainer_0_FuelTypesDDL=37&location={location.replace(' ', '%20')}").add_field(name="PetrolSpy", value=f"https://petrolspy.com.au/map/latlng/{suburb['lat']}/{suburb['long']}")
# for station in area_response["Stations"]:
# embed.add_field(name=station["Name"], value=station["Price"])
await ctx.send(f"Fair Fuel price in {fair_response['FairPrice']['Location']}: {fair_response['FairPrice']['RoundedPrice']}{' :thumbsup:' if fair_response['FairPrice']['PriceIndicator'] == 2 else ''}", embed=embed)
@fuel.autocomplete("location")
async def autocomplete(ctx: AutocompleteContext):
inp = ctx.input_text.lower()
results = [a for a in suburbs if a.lower().startswith(inp)][:25]
await ctx.send(choices=[{"name": r, "value": r} for r in results])
@listen()
async def on_startup():
print("Bot started")
bot.start()
| 1 | #!/usr/bin/env python3 |
| 2 | from requests import get |
| 3 | from interactions import slash_command, SlashContext, Client, slash_option, OptionType, auto_defer, Embed, check, AutocompleteContext, listen |
| 4 | from os import environ |
| 5 | |
| 6 | suburbs = {f"{d['locality']} {d['state']}": d for d in get("https://www.matthewproctor.com/Content/postcodes/australian_postcodes.json").json()} |
| 7 | |
| 8 | bot = Client(token=environ["BOT_TOKEN"]) |
| 9 | guilds = [353071720342487040] |
| 10 | |
| 11 | @slash_command(name="fuel", description="Check Fair Fuel prices (RACQ)", scopes=guilds) |
| 12 | @slash_option(name="location", description="Suburb", required=True, opt_type=OptionType.STRING, autocomplete=True) |
| 13 | async def fuel(ctx: SlashContext, location: str): |
| 14 | suburb = suburbs[location] |
| 15 | fair_response = get("https://www.racq.com.au/ajaxpages/fuelprice/fairfuelpricesapi.ashx", params={"lat": suburb["lat"], "lng": suburb["long"], "fueltype": 37}).json() |
| 16 | # area_response = get(f"https://www.racq.com.au/ajaxpages/fuelprice/fuelpricesapi.ashx?lat={suburb['lat']}&lng={suburb['long']}&fueltype=37").json() |
| 17 | embed = Embed(title="Area prices").add_field(name="RACQ", value=f"https://www.racq.com.au/car/fair-fuel-prices?phracq_body_0_phracq_contentcontainer_0_FuelTypesDDL=37&location={location.replace(' ', '%20')}").add_field(name="PetrolSpy", value=f"https://petrolspy.com.au/map/latlng/{suburb['lat']}/{suburb['long']}") |
| 18 | # for station in area_response["Stations"]: |
| 19 | # embed.add_field(name=station["Name"], value=station["Price"]) |
| 20 | await ctx.send(f"Fair Fuel price in {fair_response['FairPrice']['Location']}: {fair_response['FairPrice']['RoundedPrice']}{' :thumbsup:' if fair_response['FairPrice']['PriceIndicator'] == 2 else ''}", embed=embed) |
| 21 | |
| 22 | @fuel.autocomplete("location") |
| 23 | async def autocomplete(ctx: AutocompleteContext): |
| 24 | inp = ctx.input_text.lower() |
| 25 | results = [a for a in suburbs if a.lower().startswith(inp)][:25] |
| 26 | await ctx.send(choices=[{"name": r, "value": r} for r in results]) |
| 27 | |
| 28 | @listen() |
| 29 | async def on_startup(): |
| 30 | print("Bot started") |
| 31 | |
| 32 | bot.start() |
docker-compose.yml
· 205 B · YAML
Raw
services:
bot:
image: ghcr.io/alyssadev/python-discord-bot:latest
environment:
- BOT_TOKEN=-
volumes:
- .:/usr/src/app
- /etc/localtime:/etc/localtime:ro
restart: always
| 1 | services: |
| 2 | bot: |
| 3 | image: ghcr.io/alyssadev/python-discord-bot:latest |
| 4 | environment: |
| 5 | - BOT_TOKEN=- |
| 6 | volumes: |
| 7 | - .:/usr/src/app |
| 8 | - /etc/localtime:/etc/localtime:ro |
| 9 | restart: always |