diff --git a/bot.py b/bot.py index 6ab3b40..db5abed 100644 --- a/bot.py +++ b/bot.py @@ -7,16 +7,19 @@ from bs4 import BeautifulSoup import nextcord from nextcord.ext import tasks, commands + def load_config(config_name): if not os.path.exists(config_name): return None with open(config_name, "rt", encoding="utf-8") as f: return json.load(f) + def save_config(config_name, config): with open(config_name, 'wt', encoding="utf-8") as f: json.dump(config, f, indent=1, ensure_ascii=False) + # dictionary of urls and time they were added processed_urls = {} @@ -30,10 +33,12 @@ if not config.get("deepl_key") or not config.get("discord_bot_token"): intents = nextcord.Intents.default() bot = commands.Bot(command_prefix=".", intents=intents) -headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'} +headers = { + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'} deepl_headers = {'Authorization': f'DeepL-Auth-Key {config["deepl_key"]}', 'User-Agent': 'YahooScraper/1.0.0'} search_term = 'けものフレンズ' -ping_terms : dict = config["ping_terms"] +ping_terms: dict = config["ping_terms"] + def add_ping(term, user_id): if term not in ping_terms: @@ -43,11 +48,12 @@ def add_ping(term, user_id): else: print(f"{user_id} is already being pinged for {term}") return False - + config["ping_terms"] = ping_terms save_config("config.json", config) return True + def remove_ping(term, user_id): if term not in ping_terms: return False @@ -58,30 +64,36 @@ def remove_ping(term, user_id): config["ping_terms"] = ping_terms save_config("config.json", config) return True - + + def get_user_pings(user_id): return [term for term in ping_terms.keys() if user_id in ping_terms[term]] + async def ping_users(auction_name_jp, auction_name_en): """If the name of the listing is particularly interesting, ping some people - HAV0X""" users_to_ping = [] + interested_terms = [] for term in ping_terms.keys(): - if term in auction_name_jp or term in auction_name_en: + if term.lower() in auction_name_jp.lower() or term.lower() in auction_name_en.lower(): + interested_terms.append(term) for user in ping_terms[term]: if user not in users_to_ping: users_to_ping.append(user) - + if len(users_to_ping) > 0: - ping_message = "Interesting listing, pinging: " + ", ".join([f"<@{user}>" for user in users_to_ping]) + ping_message = "Interesting listing, pinging: " + ", ".join([f"<@{user}>" for user in users_to_ping]) + "\nTerms of interest: " + ', '.join([f'"{s}"' for s in interested_terms]) await send_message_text("Japari Modding", "yahoo-auctions", ping_message) + def translate(text: str): text = requests.post("https://api-free.deepl.com/v2/translate", json={"text": [text], "target_lang": "EN", 'source_lang': 'JA'}, headers=deepl_headers).json()[ 'translations'][0]['text'] return text.replace("Beast Friends", "Kemono Friends") + def embed_auction(service: str, url: str, name: str, name_en: str, thumbnail: str, price=None): desc = name if price is not None: @@ -91,6 +103,7 @@ def embed_auction(service: str, url: str, name: str, name_en: str, thumbnail: st embed.set_image(url=thumbnail) return embed + async def send_message(server_name: str, channel_name: str, embed: nextcord.Embed): for guild in bot.guilds: if guild.name == server_name: @@ -99,6 +112,7 @@ async def send_message(server_name: str, channel_name: str, embed: nextcord.Embe await channel.send(embed=embed) break + async def send_message_text(server_name: str, channel_name: str, text: str): for guild in bot.guilds: if guild.name == server_name: @@ -107,6 +121,7 @@ async def send_message_text(server_name: str, channel_name: str, text: str): await channel.send(content=text) break + async def check_yahoo_fleamarket(search_term: str, page: int, notify: bool): url = f'https://buyee.jp/paypayfleamarket/search?keyword={search_term}&order-sort=created_time&page={page}' @@ -142,6 +157,7 @@ async def check_yahoo_fleamarket(search_term: str, page: int, notify: bool): except Exception as e: print(url, e) + async def check_yahoo_auction(search_term: str, page: int, notify: bool): url = f'https://buyee.jp/item/search/query/{search_term}?sort=end&order=d&page={page}' @@ -174,6 +190,7 @@ async def check_yahoo_auction(search_term: str, page: int, notify: bool): except Exception as e: print(url, e) + @bot.event async def on_ready(): for i in range(1, 4): @@ -182,6 +199,7 @@ async def on_ready(): await asyncio.sleep(10) myLoop.start() + @tasks.loop(minutes=30) async def myLoop(): fulldate = datetime.now() @@ -203,26 +221,33 @@ async def myLoop(): if onlydate - processed_urls[key] > timedelta(weeks=3): del processed_urls[key] + @bot.slash_command(description="Pings you when auction name contains the given term") -async def ping_me(interaction: nextcord.Interaction, term : str): +async def ping_me(interaction: nextcord.Interaction, term: str): if add_ping(term, interaction.user.id): await interaction.response.send_message(f"Success! You will be pinged for {term}", ephemeral=False) else: pings = get_user_pings(interaction.user.id) if len(pings) > 0: - await interaction.response.send_message(f"Failed to add {term}. You are pinged for {', '.join(pings)}", ephemeral=True) + await interaction.response.send_message(f"Failed to add {term}. You are pinged for {', '.join(pings)}", + ephemeral=True) else: - await interaction.response.send_message(f"Failed to add {term}. You are not pinged for anything", ephemeral=True) + await interaction.response.send_message(f"Failed to add {term}. You are not pinged for anything", + ephemeral=True) + @bot.slash_command(description="No longer pings you when auction name contains the given term") -async def unping_me(interaction: nextcord.Interaction, term : str): +async def unping_me(interaction: nextcord.Interaction, term: str): if remove_ping(term, interaction.user.id): await interaction.response.send_message(f"Success! You will no longer be pinged for {term}", ephemeral=False) else: pings = get_user_pings(interaction.user.id) if len(pings) > 0: - await interaction.response.send_message(f"Failed to remove {term}. You are pinged for {', '.join(pings)}", ephemeral=True) + await interaction.response.send_message(f"Failed to remove {term}. You are pinged for {', '.join(pings)}", + ephemeral=True) else: - await interaction.response.send_message(f"Failed to remove {term}. You are not pinged for anything", ephemeral=True) + await interaction.response.send_message(f"Failed to remove {term}. You are not pinged for anything", + ephemeral=True) + bot.run(config["discord_bot_token"], reconnect=True) \ No newline at end of file