Make message formatting more flexible.
This commit is contained in:
parent
dc3a45297b
commit
c05aff718e
|
@ -51,6 +51,20 @@ class BotInstance:
|
||||||
self.misskey_token = config["misskey_token"]
|
self.misskey_token = config["misskey_token"]
|
||||||
self.max_page_number = config["max_page_number"]
|
self.max_page_number = config["max_page_number"]
|
||||||
|
|
||||||
|
def format_message(self, image_src, image_url):
|
||||||
|
if isinstance(self.bot_message, list):
|
||||||
|
msg = random.choice(self.bot_message)
|
||||||
|
else:
|
||||||
|
msg = self.bot_message
|
||||||
|
if random.randint(0, 100) < 5:
|
||||||
|
msg = msg.replace("$dh$", self.bot_hashtags)
|
||||||
|
else:
|
||||||
|
msg = msg.replace("$dh$", "")
|
||||||
|
msg = msg.replace("$gel_src$", "[Gelbooru Link](%s)" % (image_url))
|
||||||
|
msg = msg.replace("$src$", "[Source](%s)" % (image_src))
|
||||||
|
msg = msg.strip()
|
||||||
|
return msg
|
||||||
|
|
||||||
# Get a random image from Gelbooru
|
# Get a random image from Gelbooru
|
||||||
def get_random_image(self, max_page_number = 100):
|
def get_random_image(self, max_page_number = 100):
|
||||||
# Get a random page number
|
# Get a random page number
|
||||||
|
@ -102,12 +116,6 @@ class BotInstance:
|
||||||
post_json = post_request.json()
|
post_json = post_request.json()
|
||||||
if 'id' in post_json:
|
if 'id' in post_json:
|
||||||
# Submit a /notes/create request to Misskey
|
# Submit a /notes/create request to Misskey
|
||||||
if isinstance(self.bot_message, list):
|
|
||||||
msg = random.choice(self.bot_message)
|
|
||||||
else:
|
|
||||||
msg = self.bot_message
|
|
||||||
if random.randint(0, 100) < 5:
|
|
||||||
msg += " " + self.bot_hashtags
|
|
||||||
create_note_request = requests.post(self.misskey_url + "notes/create", json = {"renoteId": post_json['id'], "text":"", "i": self.misskey_token})
|
create_note_request = requests.post(self.misskey_url + "notes/create", json = {"renoteId": post_json['id'], "text":"", "i": self.misskey_token})
|
||||||
# If error, print error and exit
|
# If error, print error and exit
|
||||||
if create_note_request.status_code != 200:
|
if create_note_request.status_code != 200:
|
||||||
|
@ -211,12 +219,7 @@ class BotInstance:
|
||||||
time.sleep(min(30, (attempts ** 2) / 2))
|
time.sleep(min(30, (attempts ** 2) / 2))
|
||||||
|
|
||||||
# Submit a /notes/create request to Misskey
|
# Submit a /notes/create request to Misskey
|
||||||
if isinstance(self.bot_message, list):
|
msg = self.format_message(image_src, image_url)
|
||||||
msg = random.choice(self.bot_message)
|
|
||||||
else:
|
|
||||||
msg = self.bot_message
|
|
||||||
if random.randint(0, 100) < 5:
|
|
||||||
msg += " " + self.bot_hashtags
|
|
||||||
create_note_request = requests.post(self.misskey_url + "notes/create", json = {"fileIds": [file_id], "text": "%s\n[Source](%s)\n" % (msg, image_src), "i": self.misskey_token})
|
create_note_request = requests.post(self.misskey_url + "notes/create", json = {"fileIds": [file_id], "text": "%s\n[Source](%s)\n" % (msg, image_src), "i": self.misskey_token})
|
||||||
# If error, print error and exit
|
# If error, print error and exit
|
||||||
if create_note_request.status_code != 200:
|
if create_note_request.status_code != 200:
|
||||||
|
|
Loading…
Reference in New Issue