🎨 Use random module instead of secrets module

This commit is contained in:
洛水居室 2022-10-29 12:11:26 +08:00 committed by GitHub
parent 755c224ca0
commit d3fb2ef386
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 18 deletions

View File

@ -1,4 +1,4 @@
import secrets import random
from typing import Tuple, List from typing import Tuple, List
from modules.gacha.banner import GachaBanner from modules.gacha.banner import GachaBanner
@ -73,8 +73,7 @@ class BannerSystem:
if weight < 0: if weight < 0:
raise GachaIllegalArgument("Weights must be non-negative!") raise GachaIllegalArgument("Weights must be non-negative!")
total += weight total += weight
secrets_random = secrets.SystemRandom() roll = random.randint(0, min(total, cutoff)) # nosec
roll = int(secrets_random.random() * min(total, cutoff))
sub_total = 0 sub_total = 0
for index, value in enumerate(weights): for index, value in enumerate(weights):
sub_total += value sub_total += value
@ -146,11 +145,8 @@ class BannerSystem:
@staticmethod @staticmethod
def get_random(items) -> int: def get_random(items) -> int:
secrets_random = secrets.SystemRandom() return random.choice(items) # nosec
roll = int(secrets_random.random() * len(items))
return items[roll]
@staticmethod @staticmethod
def random_range(_mix: int, _max: int) -> int: def random_range(_mix: int, _max: int) -> int:
secrets_random = secrets.SystemRandom() return random.randint(_mix, _max) # nosec
return int(secrets_random.uniform(_mix, _max))

View File

@ -1,4 +1,4 @@
import secrets import random
from typing import Optional from typing import Optional
from genshin import Client from genshin import Client
@ -109,7 +109,7 @@ class UserStatsPlugins(Plugin, BasePlugin):
("雷神瞳", "electroculi"), ("雷神瞳", "electroculi"),
("草神瞳", "dendroculi"), ("草神瞳", "dendroculi"),
], ],
"style": secrets.choice(["mondstadt", "liyue"]), "style": random.choice(["mondstadt", "liyue"]), # nosec
} }
# html = await self.template_service.render_async( # html = await self.template_service.render_async(

View File

@ -1,6 +1,6 @@
import asyncio import asyncio
import datetime import datetime
import secrets import random
import time import time
from aiohttp import ClientConnectorError from aiohttp import ClientConnectorError
@ -37,14 +37,14 @@ class SignJob(Plugin):
self.sign_service = sign_service self.sign_service = sign_service
self.cookies_service = cookies_service self.cookies_service = cookies_service
self.user_service = user_service self.user_service = user_service
self.random = secrets.SystemRandom()
async def single_sign(self, user_id: int) -> str: @staticmethod
async def single_sign(user_id: int) -> str:
client = await get_genshin_client(user_id) client = await get_genshin_client(user_id)
if recognize_genshin_server(client.uid) in ("cn_gf01", "cn_qd01"): if recognize_genshin_server(client.uid) in ("cn_gf01", "cn_qd01"):
await asyncio.sleep(10 + self.random.random() * 300) # 延迟 [10, 300) await asyncio.sleep(random.randint(10, 300)) # nosec
else: else:
await asyncio.sleep(self.random.random() * 3) # 延迟 [0, 3) await asyncio.sleep(random.randint(0, 3)) # nosec
rewards = await client.get_monthly_rewards(game=Game.GENSHIN, lang="zh-cn") rewards = await client.get_monthly_rewards(game=Game.GENSHIN, lang="zh-cn")
daily_reward_info = await client.get_reward_info(game=Game.GENSHIN) daily_reward_info = await client.get_reward_info(game=Game.GENSHIN)
if not daily_reward_info.signed_in: if not daily_reward_info.signed_in:

View File

@ -1,6 +1,6 @@
import asyncio import asyncio
import datetime import datetime
import secrets import random
from aiohttp import ClientConnectorError from aiohttp import ClientConnectorError
from genshin import InvalidCookies, AlreadyClaimed, GenshinException from genshin import InvalidCookies, AlreadyClaimed, GenshinException
@ -29,7 +29,6 @@ class SignAll(Plugin):
self.sign_service = sign_service self.sign_service = sign_service
self.cookies_service = cookies_service self.cookies_service = cookies_service
self.user_service = user_service self.user_service = user_service
self.random = secrets.SystemRandom()
@handler(CommandHandler, command="sign_all", block=False) @handler(CommandHandler, command="sign_all", block=False)
@bot_admins_rights_check @bot_admins_rights_check
@ -70,7 +69,8 @@ class SignAll(Plugin):
try: try:
if "今天旅行者已经签到过了~" not in text: if "今天旅行者已经签到过了~" not in text:
await context.bot.send_message(sign_db.chat_id, text, parse_mode=ParseMode.HTML) await context.bot.send_message(sign_db.chat_id, text, parse_mode=ParseMode.HTML)
await asyncio.sleep(10 + self.random.random() * 50) # 回复延迟 [10, 60) 避免触发洪水防御 await asyncio.sleep(random.randint(10, 50)) # nosec
# 回复延迟 [10, 60] 避免触发洪水防御
except BadRequest as exc: except BadRequest as exc:
logger.error(f"执行自动签到时发生错误 用户UID[{user_id}]") logger.error(f"执行自动签到时发生错误 用户UID[{user_id}]")
logger.exception(exc) logger.exception(exc)