async def url_clear(self, ctx, user: discord.Member = None, start: int = None, end: int = None): if user and await self.bot.is_owner(ctx.author): target = user.id else: target = ctx.author.id try: get_data(target) except: await ctx.send("資料庫內沒儲存的網址", delete_after=3) else: if not start: set_data(target) await ctx.send("儲存的網址已清空", delete_after=3) else: urls = get_data(target) if not end: del urls[start - 1] else: del urls[start - 1:end] set_data(target, urls) await ctx.send("指定網址已刪除", delete_after=3)
async def url_count(self, ctx, user: discord.Member = None): if user and await self.bot.is_owner(ctx.author): target = user.id else: target = ctx.author.id try: urls = get_data(target) except: await ctx.send("資料庫內沒儲存的網址", delete_after=3) else: await ctx.send(f"資料庫中有 `{len(urls)}` 條網址", hidden=True)
async def store_url(self, ctx, urls: list): try: data = get_data(ctx.author.id) except: data = [] finally: length = len(data) for url in urls: if url not in data: data.append(url) set_data(ctx.author.id, data) await ctx.send(f"已儲存 `{len(data)-length}` 個網址", delete_after=3)
async def url_download(self, ctx, user: discord.Member = None): if not user or not await self.bot.is_owner(ctx.author): user = ctx.author try: data = get_data(user.id) except: await ctx.send("無網址資料", delete_after=3) else: line = '{0}. <a href="{1}" target="_blank">{1}</a><br>\n' with open("urls.html", mode="w") as f: f.write(f"{user.name}儲存的網址<br>") for i, url in enumerate(data): f.write(line.format(i, url)) await ctx.send("檔案已發送至私訊", delete_after=3) await ctx.author.send("", file=discord.File("urls.html")) set_data(user.id) os.remove("urls.html")
async def url_show(self, ctx, user: discord.Member = None): if user and await self.bot.is_owner(ctx.author): target = user.id else: target = ctx.author.id try: urls = get_data(target) except Exception as e: await ctx.send("資料庫內沒儲存的網址", delete_after=3) print(e) else: msg = "" for i, url in enumerate(urls): if len(msg) + len(url) + len(str(i + 1)) + 2 > 2000: await ctx.send(msg, hidden=True) msg = "" msg += f"{i+1}. {url}\n" await ctx.send(msg, hidden=True)