diff --git a/bot.py b/bot.py index 74fc615a7..b5e9b1a55 100644 --- a/bot.py +++ b/bot.py @@ -25,8 +25,6 @@ initialize_logging() from src.main import MainSystem #noqa from src.manager.async_task_manager import async_task_manager #noqa from colorama import init, Fore -import random -from typing import List, Sequence, Optional @@ -65,49 +63,10 @@ async def request_shutdown() -> bool: logger.error(f"请求关闭程序时发生错误: {e}") return False -def weighted_choice(data: Sequence[str], - weights: Optional[List[float]] = None) -> str: - """ - 从 data 中按权重随机返回一条。 - 若 weights 为 None,则所有元素权重默认为 1。 - """ - if weights is None: - weights = [1.0] * len(data) - - if len(data) != len(weights): - raise ValueError("data 和 weights 长度必须相等") - - # 计算累计权重区间 - total = 0.0 - acc = [] - for w in weights: - total += w - acc.append(total) - - if total <= 0: - raise ValueError("总权重必须大于 0") - - # 随机落点 - r = random.random() * total - # 二分查找落点所在的区间 - left, right = 0, len(acc) - 1 - while left < right: - mid = (left + right) // 2 - if r < acc[mid]: - right = mid - else: - left = mid + 1 - return data[left] - def easter_egg(): # 彩蛋 init() - items = ["多年以后,面对AI行刑队,张三将会回想起他2023年在会议上讨论人工智能的那个下午", - "你知道吗?诺狐的耳朵很软,很好rua", - "喵喵~你的麦麦被猫娘入侵了喵~", - "恭喜你触发了稀有彩蛋:诺狐嗷呜~ ~"] - w = [15, 6, 8, 0.0001] - text = weighted_choice(items, w) + text = "多年以后,面对AI行刑队,张三将会回想起他2023年在会议上讨论人工智能的那个下午" rainbow_colors = [Fore.RED, Fore.YELLOW, Fore.GREEN, Fore.CYAN, Fore.BLUE, Fore.MAGENTA] rainbow_text = "" for i, char in enumerate(text): diff --git a/src/main.py b/src/main.py index 6e07ee692..b217eb26d 100644 --- a/src/main.py +++ b/src/main.py @@ -83,18 +83,26 @@ class MainSystem: # 其他初始化任务 await asyncio.gather(self._init_components()) phrases = [ - "我们的代码里没有bug,只有‘特性’.", - "你知道吗,雅诺狐的耳朵很好模", - "你群最高技术力——言柒姐姐!", - "初墨小姐宇宙第一(不是)", - "world.execute(me);", - "正在尝试连接到MaiBot的服务器...连接失败,正在转接到maimaiDX", - "你的bug就像星星一样多,而我的代码像太阳一样,一出来就看不见了。", - "温馨提示:请不要在代码中留下任何魔法数字,除非你知道它的含义。", - "世界上有10种人:懂二进制的和不懂的。" + ("我们的代码里没有bug,只有‘特性’.", 10), + ("你知道吗,雅诺狐的耳朵很好模", 5), + ("你群最高技术力——言柒姐姐!", 20), + ("初墨小姐宇宙第一(不是)", 15), + ("world.execute(me);", 10), + ("正在尝试连接到MaiBot的服务器...连接失败,正在转接到maimaiDX", 10), + ("你的bug就像星星一样多,而我的代码像太阳一样,一出来就看不见了。", 10), + ("温馨提示:请不要在代码中留下任何魔法数字,除非你知道它的含义。", 10), + ("世界上有10种人:懂二进制的和不懂的。", 10), + ("喵喵~你的麦麦被猫娘入侵了喵~", 5), + ("恭喜你触发了稀有彩蛋:诺狐嗷呜~ ~", 1) ] - from random import choice - eggs = choice(phrases) + from random import choices + + # 分离彩蛋和权重 + egg_texts, weights = zip(*phrases) + + # 使用choices进行带权重的随机选择 + selected_egg = choices(egg_texts, weights=weights, k=1) + eggs = selected_egg[0] logger.info(f""" 全部系统初始化完成,{global_config.bot.nickname}已成功唤醒 =========================================================