async def on_message(self, message): # fights against people trying to ruin my images hehe ;) if message.content.startswith(".place setpixel ") and self.image is not None: cont = message.content.split(" ") try: x = int(cont[2]) y = int(cont[3]) except ValueError: return r, g, b, a = self.image.getpixel((x, y)) if a != 0: color = rgb2hex(r, g, b) if color != cont[4].lower(): channel = self.bot.get_channel(819966095070330950) if channel is None: channel = self.bot.get_channel(402563165247766528) await channel.send(f".place setpixel {x} {y} {color} | COUNTERING {message.author.name}") if message.author.id == self.userToCopyTextFrom and message.channel.id != 813430350965375046 and is_valid_msg(message.content): if len(self.queue) > 50: return pil_img, self.last_line, self.last_char = self.draw_text(message.content, self.last_line, self.last_char) SQLFunctions.insert_or_update_config("Draw_Last_Line", self.last_line, self.conn) SQLFunctions.insert_or_update_config("Draw_Last_Char", self.last_char, self.conn) # id to stop specific draw ID = str(random.randint(1000, 10000)) img = im2q.PixPlace(ID, ID, False, pil_img=pil_img) img.left_to_right() self.handle_image(img, 0, ID)
async def on_message(self, message): # only count stats in servers if message.guild is None: return # deletes the message if its in #newcomers if message.channel.id == 815881148307210260 and not message.author.bot: try: await message.delete() deleted_messages = SQLFunctions.get_config( "deleted_messages", self.conn) if len(deleted_messages) == 0: deleted_messages = 0 else: deleted_messages = deleted_messages[0] SQLFunctions.insert_or_update_config("deleted_messages", deleted_messages + 1, self.conn) except discord.NotFound: # message was already deleted pass SUBJECT_ID = self.get_current_subject() # Makes it better to work with the message msg = demojize(message.content) char_count = len(msg) word_count = len(msg.split(" ")) emoji_count = msg.count(":") // 2 spoiler_count = msg.count("||") // 2 # File Statistics files_amount = len(message.attachments) file_sizes = 0 images_amt = 0 for f in message.attachments: file_sizes += f.size if f.height is not None and f.height > 0: images_amt += 1 SQLFunctions.update_statistics(message.author, SUBJECT_ID, conn=self.conn, messages_sent=1, characters_sent=char_count, words_sent=word_count, spoilers_sent=spoiler_count, emojis_sent=emoji_count, files_sent=files_amount, file_size_sent=file_sizes, images_sent=images_amt)
async def watch(self, ctx, val=None): await ctx.message.delete() if val is None: # yes if we're tracking, no if we're at default value if self.watch_button_value < 1e6: await ctx.send(f"{self.watch_button_value} | {self.sent_message}", delete_after=5) else: await ctx.send("no", delete_after=5) else: if not val.isnumeric(): await ctx.send("not int", delete_after=5) raise discord.ext.commands.errors.BadArgument # saves the value to watch into config SQLFunctions.insert_or_update_config("ButtonValue", int(val), self.conn) self.watch_button_value = int(val) self.old_value = 1e6 self.sent_message = False await ctx.send("ok", delete_after=5)
def handle_image(self, img: im2q, drawn: int, ID: str): self.progress[ID] = { "count": drawn, "img": img, "queue": img.get_queue() } self.queue.append({ "ID": ID, "size": img.size, "img": img, "queue": img.get_queue() }) SQLFunctions.insert_or_update_config(f"Start_{ID}", 0, self.conn) SQLFunctions.insert_or_update_config(f"End_{ID}", img.size, self.conn) # saves the img as a numpy file so it can easily be reload when the bot restarts img.save_array(f"{self.place_path}{ID}")
async def draw_pixels(self, ID, channel, start, end) -> bool: pixels_queue = self.progress[ID]["queue"][start:end] # draws the pixels while len(pixels_queue) > 0: if self.cancel_all or str(ID) in self.cancel_draws: await channel.send(f"Canceled Project {ID}.") return True if self.pause_draws: return False pix = pixels_queue[0] pX = pix[0] pY = pix[1] pHex = rgb2hex(pix[2], pix[3], pix[4]) try: await channel.send(f".place setpixel {pX} {pY} {pHex} | PROJECT {ID}") self.progress[ID]["count"] += 1 pixels_queue.pop(0) if self.progress[ID]["count"] % 10 == 0: SQLFunctions.insert_or_update_config(f"Start_{ID}", self.progress[ID]["count"], self.conn) except Exception: await asyncio.sleep(5) return True
async def background_check_cases(self): try: await self.bot.wait_until_ready() # TURN OFF OVER NEW YEAR dt = datetime.now(timezone("Europe/Zurich")) if (dt.month == 12 and dt.day == 31) or (dt.month == 1 and dt.day in [1, 2]): return # Send the covid guesser notification general_channel = 898674880864743444 try: cur_time = datetime.now( timezone("Europe/Zurich")).strftime("%a:%H:%M") if not self.sent_covid and "10:00" in cur_time and "Sat" not in cur_time and "Sun" not in cur_time: self.sent_covid = True general = self.bot.get_channel(general_channel) msg = "Good Morning!\nGuess today's covid cases using `$g <guess>`!" embed = discord.Embed(description=msg, color=discord.Color.gold()) await general.send("<@&770968106679926868>", embed=embed) if "10:00" not in cur_time: self.sent_covid = False except Exception as e: print(e) # checks the daily cases url = "https://www.covid19.admin.ch/api/data/context/history" response = {} async with aiohttp.ClientSession() as cs: async with cs.get(url) as r: response = await r.json() # gets the last updated day from the website last_updated = response["dataContexts"][0]["date"] daily_url = response["dataContexts"][0]["dataContextUrl"] if len(last_updated) > 0: # to make sure we even got anything day = int(last_updated.split("-")[2]) else: owner = self.bot.get_user(205704051856244736) await owner.send( content= f"Covid cases failed updating. Last updated is empty:\n```{last_updated}```" ) raise ValueError # fetches the new cases # daily_url is of format https://www.covid19.admin.ch/api/data/DATE-random-gibberish/context # we want to go to this instead: https://www.covid19.admin.ch/api/data/DATE-random-gibberish/sources/COVID19Cases_geoRegion.json if self.last_cases_day != day: daily_url = daily_url.replace( "context", "sources/COVID19Cases_geoRegion.json") async with aiohttp.ClientSession() as cs: async with cs.get(daily_url) as r: response = await r.json() new_cases = -1 for line in response: # finds the cases for today's date and for all of switzrland and liechtenstein if line["geoRegion"] == "CHFL" and line[ "datum"] == last_updated: new_cases = line["entries_diff_last"] break if new_cases == -1: print("Wasn't able to get daily covid cases") raise ValueError self.cases_today = new_cases self.last_cases_day = day guild = self.bot.get_guild(747752542741725244) if guild is None: guild = self.bot.get_guild(237607896626495498) channel = guild.get_channel(general_channel) if channel is None: channel = guild.get_channel(402563165247766528) await self.send_message(channel, new_cases) log("Daily cases have been updated", print_it=True) SQLFunctions.insert_or_update_config("COVID_Cases", new_cases, self.conn) SQLFunctions.insert_or_update_config("COVID_Day", day, self.conn) SQLFunctions.store_covid_cases(new_cases, conn=self.conn) except Exception as e: print(f"COVID loop messed up:\n{e}") await asyncio.sleep(20)