Skip to content

Commit 65d9d86

Browse files
committed
fix: global 导致的错误引用
1 parent 5564510 commit 65d9d86

3 files changed

Lines changed: 33 additions & 37 deletions

File tree

telegram_chat/__init__.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from . import tools
1111
from .commands import ChatType, MessageType, register_commands
1212
from .command_builder import CommandBuilder
13-
from .config import *
13+
from .config import ConfigManager
1414
from .const import VERSION, VERSION_STR
1515
from .telegram_manager import TelegramBot
1616

@@ -33,22 +33,21 @@ async def on_load(server: PluginServerInterface, old):
3333
"""
3434
插件加载操作
3535
"""
36-
global bindings, ban_list, bot, logger
3736

3837
ConfigManager.load_data(server)
3938

40-
online_player_api = server.get_plugin_instance("online_player_api")
41-
if online_player_api is None: raise Exception("Unable to load dependency \"online_player_api\"")
39+
ConfigManager.online_player_api = server.get_plugin_instance("online_player_api")
40+
if ConfigManager.online_player_api is None: raise Exception("Unable to load dependency \"online_player_api\"")
4241

4342
async def action(event: Update, context: ContextTypes.DEFAULT_TYPE):
4443
await on_message(server, event, context)
4544

46-
bot = TelegramBot(server.logger, ConfigManager.config.telegram["token"]) if ConfigManager.config.telegram["api"] is None else TelegramBot(server.logger, ConfigManager.config.telegram["token"], ConfigManager.config.telegram["api"])
47-
bot.action = action
48-
bot.register()
49-
bot.start(True)
45+
ConfigManager.bot = TelegramBot(server.logger, ConfigManager.config.telegram["token"]) if ConfigManager.config.telegram["api"] is None else TelegramBot(server.logger, ConfigManager.config.telegram["token"], ConfigManager.config.telegram["api"])
46+
ConfigManager.bot.action = action
47+
ConfigManager.bot.register()
48+
ConfigManager.bot.start(True)
5049

51-
logger = server.logger
50+
ConfigManager.logger = server.logger
5251
register_commands()
5352

5453
server.register_help_message("!!tg", "向 Telegram 群聊发送聊天信息")
@@ -62,7 +61,7 @@ async def action(event: Update, context: ContextTypes.DEFAULT_TYPE):
6261
# await tools.send_to_group(tip)
6362
server.say(f"§7{tip}")
6463

65-
def on_unload(server: PluginServerInterface): bot.stop() if bot is not None else None
64+
def on_unload(server: PluginServerInterface): ConfigManager.bot.stop() if ConfigManager.bot is not None else None
6665

6766
async def on_user_info(server: PluginServerInterface, info: Info):
6867
if ConfigManager.config.forwardings["mc_to_tg"] is True and info.player:
@@ -81,7 +80,7 @@ async def on_message(server: PluginServerInterface, event: Update, context: Cont
8180
content = event.message.text
8281
if content is None: return
8382
event_type = tools.parse_event_type(event)
84-
logger.debug(f"Update 数据:\n{event}")
83+
ConfigManager.logger.debug(f"Update 数据:\n{event}")
8584

8685
typ = tools.get_type(event)
8786
# 防止自己的机器人被别人拉去用还越权
@@ -96,11 +95,11 @@ async def on_message(server: PluginServerInterface, event: Update, context: Cont
9695
# 普通信息
9796
if ConfigManager.config.forwardings["tg_to_mc"] is True and typ == ChatType.GROUP:
9897
id: str = str(tools.get_id(event))
99-
name: str = f"§a<{bindings[id]}>§7" if id in bindings else f"§4<{event.message.chat.full_name} ({id})>§7"
98+
name: str = f"§a<{ConfigManager.bindings[id]}>§7" if id in ConfigManager.bindings else f"§4<{event.message.chat.full_name} ({id})>§7"
10099
server.say(f"§7[TG] {name}: {content}")
101100

102101
# 封禁列表,不作应答
103-
if tools.get_id(event) in ban_list:
102+
if tools.get_id(event) in ConfigManager.ban_list:
104103
server.logger.debug(f"用户 {tools.get_id(event)} 已被封禁,拒绝处理其请求")
105104
return
106105

@@ -113,13 +112,11 @@ async def mc_command_tg(src: CommandSource, ctx: CommandContext):
113112
player = "Console"
114113
if src.is_player:
115114
player = src.player # type: ignore
116-
if player not in bindings.values():
115+
if player not in ConfigManager.bindings.values():
117116
src.reply("请先在群内绑定你的账号!")
118117
return
119-
elif (not str(next((key for key, value in bindings.items() if value == player), 0)) in ConfigManager.config.admins and not src.has_permission(2)) and (player != "Console"):
118+
elif (not str(next((key for key, value in ConfigManager.bindings.items() if value == player), 0)) in ConfigManager.config.admins and not src.has_permission(2)) and (player != "Console"):
120119
src.reply("你没有足够的权限!")
121120
return
122121
msg = f"{player}:\n{ctx['message']}"
123-
await tools.send_to_group(msg, entities=[MessageEntity("bold", 0, len(player) + 1)])
124-
125-
__all__ = ["bot", "command_tree", "logger"]
122+
await tools.send_to_group(msg, entities=[MessageEntity("bold", 0, len(player) + 1)])

telegram_chat/commands/bind.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from telegram.ext import ContextTypes
88

99
from .. import tools
10-
from ..config import *
10+
from ..config import ConfigManager
1111
from .types import MessageType
1212

1313
async def bind_admin(server: PluginServerInterface, event: Update, context: ContextTypes.DEFAULT_TYPE, command: List[str],
@@ -16,9 +16,9 @@ async def bind_admin(server: PluginServerInterface, event: Update, context: Cont
1616
id: str = command[0]
1717
player: str = command[1]
1818

19-
if id in bindings:
19+
if id in ConfigManager.bindings:
2020
await bind_unbind(server, event, context, [id], event_type)
21-
bindings[id] = player
21+
ConfigManager.bindings[id] = player
2222
await tools.send_to(
2323
event,
2424
context,
@@ -33,8 +33,8 @@ async def bind_user(server: PluginServerInterface, event: Update, context: Conte
3333
player = command[0]
3434
user_id = str(tools.get_id(event))
3535

36-
if user_id in bindings.keys():
37-
value = bindings[user_id]
36+
if user_id in ConfigManager.bindings.keys():
37+
value = ConfigManager.bindings[user_id]
3838
await tools.send_to(
3939
event,
4040
context,
@@ -60,7 +60,7 @@ async def bind_user(server: PluginServerInterface, event: Update, context: Conte
6060
)
6161
return
6262

63-
bindings[user_id] = player
63+
ConfigManager.bindings[user_id] = player
6464
await tools.send_to(
6565
event,
6666
context,
@@ -74,9 +74,9 @@ async def bind_unbind(server: PluginServerInterface, event: Update, context: Con
7474
event_type: MessageType):
7575
if event_type == MessageType.ADMIN:
7676
id: str = command[0]
77-
if id in bindings:
78-
player: str = bindings[id]
79-
del bindings[id]
77+
if id in ConfigManager.bindings:
78+
player: str = ConfigManager.bindings[id]
79+
del ConfigManager.bindings[id]
8080
ConfigManager.save_data(server)
8181
await tools.send_to(
8282
event,
@@ -97,8 +97,8 @@ async def bind_query(server: PluginServerInterface, event: Update, context: Cont
9797
match typ:
9898
case "TG":
9999
result = None
100-
if value in bindings:
101-
result = bindings[value]
100+
if value in ConfigManager.bindings:
101+
result = ConfigManager.bindings[value]
102102

103103
if result is None:
104104
await tools.send_to(
@@ -114,8 +114,8 @@ async def bind_query(server: PluginServerInterface, event: Update, context: Cont
114114
)
115115
case "ID":
116116
result = None
117-
if value in bindings.values():
118-
result = [k for k, v in bindings.items() if v == value]
117+
if value in ConfigManager.bindings.values():
118+
result = [k for k, v in ConfigManager.bindings.items() if v == value]
119119

120120
if result is None:
121121
await tools.send_to(

telegram_chat/config.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ class ConfigManager:
3333

3434
@staticmethod
3535
def load_data(server: PluginServerInterface):
36-
global config, bindings, ban_list
37-
config = server.load_config_simple(target_class=Config) # type: ignore
38-
bindings = server.load_config_simple(
36+
ConfigManager.config = server.load_config_simple(target_class=Config) # type: ignore
37+
ConfigManager.bindings = server.load_config_simple(
3938
"bindings.json",
4039
default_config={"data": {}},
4140
echo_in_console=False
4241
)["data"]
43-
ban_list = server.load_config_simple(
42+
ConfigManager.ban_list = server.load_config_simple(
4443
"ban_list.json",
4544
default_config={"data": []},
4645
echo_in_console=False
@@ -53,13 +52,13 @@ def save_data(server: PluginServerInterface):
5352
"""
5453
server.save_config_simple(
5554
{
56-
"data": bindings,
55+
"data": ConfigManager.bindings,
5756
},
5857
"bindings.json"
5958
)
6059
server.save_config_simple(
6160
{
62-
"data": ban_list,
61+
"data": ConfigManager.ban_list,
6362
},
6463
"ban_list.json"
6564
)

0 commit comments

Comments
 (0)