From 2061d53f7baeb4310bb2fc0d8e4c9fac0f8b7323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=84=EB=A5=B4=ED=8E=98?= Date: Fri, 1 Dec 2023 10:47:20 +0900 Subject: [PATCH] refactor: restructure no duplicate part --- exts/post.py | 6 +----- main.py | 10 +++++++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/exts/post.py b/exts/post.py index 62e262f..742696b 100644 --- a/exts/post.py +++ b/exts/post.py @@ -9,7 +9,6 @@ class Post(commands.Cog): self.bot = bot self.max_count = bot.config.max or 3 self.visibility = bot.config.visibility or "home" - self.posted = [] self.rate = bot.config.rate or 30 self.minute = bot.config.start_time or 0 @@ -22,13 +21,10 @@ class Post(commands.Cog): if self.minute >= 60: self.minute = self.minute - 60 - line = await self.bot.get_random_line(self.posted) + line = await self.bot.get_random_line() template = self.bot.config.note result = template.replace("{text}", line.text).replace("{from}", line.where).replace("{number}", line.number) await self.bot.client.note.action.send(content=result, visibility=self.visibility) - self.posted.append(line.location) - if len(self.posted) > self.max_count: - self.posted.pop(0) async def setup(bot: Bot): diff --git a/main.py b/main.py index 30c08b2..0e16ffa 100644 --- a/main.py +++ b/main.py @@ -67,6 +67,7 @@ class Line: class Autoposter(commands.Bot): def __init__(self): super().__init__() + self.posted = [] self.config: Config = Config("./config.json") self.agcm = gspread_asyncio.AsyncioGspreadClientManager(self.config.get_creds) @@ -76,16 +77,19 @@ class Autoposter(commands.Bot): worksheet = await spreadsheet.get_worksheet(0) return worksheet - async def get_random_line(self, ignore: list) -> Line: + async def get_random_line(self) -> Line: sheet: Worksheet = await self.get_worksheet() response = await sheet.get("F4") if response is None or response == "": return count = int(response[0][0]) number = 0 - while number in ignore or number == 0: + while number in self.posted or number == 0: result = random.randint(1, count) number = result + 2 + self.posted.append(number) + if len(self.posted) > self.max_count: + self.posted.pop(0) return await Line.from_number(number, sheet) async def get_line(self, number: int) -> Line: @@ -112,7 +116,7 @@ class Autoposter(commands.Bot): if notice.note.reply_id is not None: return - line: Line = self.get_random_line() + line: Line = await self.get_random_line() template = self.config.reply result = template.replace("{text}", line.text).replace("{from}", line.where).replace("{number}", line.number) await notice.note.api.action.reply(content=result, visibility=notice.note.visibility, reply_id=notice.note.id)