1
0

refactor: restructure no duplicate part

This commit is contained in:
オスカー、 2023-12-01 10:47:20 +09:00
parent 2f4f028225
commit 2061d53f7b
No known key found for this signature in database
GPG Key ID: B1EFBBF5C93FF78F
2 changed files with 8 additions and 8 deletions

View File

@ -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):

10
main.py
View File

@ -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)