-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscord_bot.py
More file actions
95 lines (74 loc) · 2.76 KB
/
Copy pathdiscord_bot.py
File metadata and controls
95 lines (74 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import discord
from discord.ext import commands
from main import (
create_shopping_list,
go_shopping,
)
from anon import get_instacart_url_from_anon
from discord.ext import commands
# create discord bot
intents = discord.Intents.default()
intents.message_content = True
intents.dm_messages = True
intents.guild_messages = True
intents.members = True
bot = commands.Bot(intents=intents, command_prefix="!")
@bot.event
async def on_ready():
print(f"{bot.user} has connected to Discord!")
@bot.event
async def on_message(message):
# only respond in dms
if message.guild:
return
if message.author == bot.user:
return
if len(message.attachments) == 2:
# send loading message
await message.channel.send("Building shopping list...")
# Download the images
before_img = message.attachments[0]
after_img = message.attachments[1]
await before_img.save("before.jpeg")
await after_img.save("after.jpeg")
# Generate shopping list
shopping_list = create_shopping_list("before.jpeg", "after.jpeg")
# Format shopping list message
list_msg = "Shopping List:\n"
for item in shopping_list:
list_msg += f"• {item.item_name}"
if hasattr(item, "quantity") and item.quantity:
list_msg += f" (x{item.quantity})"
if hasattr(item, "brand_name") and item.brand_name:
list_msg += f" - {item.brand_name}"
list_msg += "\n"
# Send message and add reaction
msg = await message.channel.send(list_msg)
await msg.add_reaction("✅")
# Store shopping list in bot's memory
setattr(bot, "shopping_list", shopping_list)
else:
await message.channel.send(
"Please attach exactly 2 images - before and after photos"
)
@bot.event
async def on_reaction_add(reaction, user):
if user == bot.user:
return
if reaction.emoji == "✅" and hasattr(bot, "shopping_list"):
await reaction.message.channel.send("Adding items to cart...")
try:
url, debug_url = get_instacart_url_from_anon()
# send debug url
await reaction.message.channel.send(
f"[Watch me add them to your cart!]({debug_url})"
)
await go_shopping(url, getattr(bot, "shopping_list"))
await reaction.message.channel.send(
"Your shopping list has been created! [Confirm on your mobile device](https://www.instacart.com/store/checkout_v4)"
)
except Exception as e:
await reaction.message.channel.send(f"Error adding items to cart: {str(e)}")
# Replace TOKEN with your Discord bot token
bot.run(os.getenv("DISCORD_TOKEN") or "")