diff --git a/README.md b/README.md index 0bbb611..7fb5d8f 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,21 @@ # Welcome_MSG [中文](./README_zh_cn.md) ### Send welcome message to player +### Ver1.2.0 -> Random sending welcomemessage ## welcome_msg.json ``` { - "setting": { - "StartDate": "1988-01-01", // Date of Server Open If don't change will cause error message in text - "Tell_Say": { // msg visiable range - "0": "Say", // Say = All Player - "1": "Tell" // Tell = The Plyer who join - } - }, - "text": { - "0": "欢迎$PlayerName$进入服务器", // $PlayerName$ $Days$ can be change the place - "1": "今天是开服的第$Days$天" - } + "setting": { + "StartDate": "1988-01-01", + }, + "text": { + "0": ["ToAll","欢迎$PlayerName$进入服务器","欢迎$PlayerName$~"], + "1": ["ToPlayer","今天是开服的第$Days$天","今天是开服的第$Days$天"], + }, + "ErrorMessage": "配置错了,通知一下腐竹吧" } ``` +#### · Number of any term of "text" must be same. +#### · Reserved word are \$PlayerName$ and \$Days$, Don't use it. \ No newline at end of file diff --git a/README_zh_cn.md b/README_zh_cn.md index 92c69cd..86a9a52 100644 --- a/README_zh_cn.md +++ b/README_zh_cn.md @@ -1,21 +1,21 @@ # Welcome_MSG [English](./README.md) ### 发送欢迎消息 +### Ver1.2.0 -> 随机发送欢迎信息 ## welcome_msg.json ``` { - "setting": { - "StartDate": "1988-01-01", // 开服日期 如果不更改会导致消息中含有报错消息 - "Tell_Say": { // 消息可见范围设置 - "0": "Say", // Say = 全部玩家 - "1": "Tell" // Tell = 加入的玩家 - } - }, - "text": { - "0": "欢迎$PlayerName$进入服务器", // $PlayerName$ $Days$ 这两个可以任意更改位置 - "1": "今天是开服的第$Days$天" - } + "setting": { + "StartDate": "1988-01-01", + }, + "text": { + "0": ["ToAll","欢迎$PlayerName$进入服务器","欢迎$PlayerName$~"], + "1": ["ToPlayer","今天是开服的第$Days$天","今天是开服的第$Days$天"], + }, + "ErrorMessage": "配置错了,通知一下腐竹吧" } ``` +#### · "text" 下每一项的数量需相同。 +#### · 保留词有且仅有 \$PlayerName$ 与 \$Days$。 \ No newline at end of file diff --git a/mcdreforged.plugin.json b/mcdreforged.plugin.json index f68abb0..39248ea 100644 --- a/mcdreforged.plugin.json +++ b/mcdreforged.plugin.json @@ -1,6 +1,6 @@ { "id": "welcome_msg", - "version": "1.1.0", + "version": "1.2.0", "name": "welcome_msg", "description": { "en_us": "Send welcome Msg to all player", diff --git a/welcome_msg/__init__.py b/welcome_msg/__init__.py index 056e478..cd0fddb 100644 --- a/welcome_msg/__init__.py +++ b/welcome_msg/__init__.py @@ -1,53 +1,50 @@ from datetime import datetime +import random from mcdreforged.api.all import * default_config = { - 'setting': { - 'StartDate': '1988-01-01', - 'Tell_Say': { - '0': 'Say', - '1': 'Tell' - } + "setting": { + "StartDate": "1988-01-01", }, - 'text': { - '0': '欢迎$PlayerName$进入服务器', - '1': '今天是开服的第$Days$天' - } + "text": { + "0": ["ToAll","欢迎$PlayerName$进入服务器","欢迎$PlayerName$~"], + "1": ["ToPlayer","今天是开服的第$Days$天","今天是开服的第$Days$天"], + }, + "ErrorMessage": "配置错了,通知一下腐竹吧" } def sendMsg(server: ServerInterface,text: str,player: str,num: str): - if player == 'Test_Player': + + if player == "Test_Player": server.broadcast(text) + elif config["text"][num][0] == "ToPlayer": + server.say(text) + elif config["text"][num][0] == "ToAll": + server.tell(player,text) else: - if config['setting']['Tell_Say'][num] == 'Say': - server.say(text) - elif config['setting']['Tell_Say'][num] == 'Tell': - server.tell(player,text) - else: - server.say('配置文件错误,请联系管理员修改配置文件') + server.say(config["ErrorMessage"]) def get_the_days(): - if config['setting']['StartDate'] != '1988-01-01': - days = datetime.now() - datetime.strptime(config['setting']['StartDate'],'%Y-%m-%d') - return str(days.days) - - else: - return '(没有找到有效日期)' + days = datetime.now() - datetime.strptime(config['setting']['StartDate'],'%Y-%m-%d') + return str(days.days) def decodeMsg(server: ServerInterface,player: str): - for k in range(0,len(config['text'])): - text = config['text'][str(k)] + date = get_the_days() + randomNumber = int(random.randint(2,len(config["text"]['0']))) - if text.find('$PlayerName$') != -1: - text = text.replace('$PlayerName$',player) - if text.find('$Days$') != -1: - text = text.replace('$Days$',get_the_days()) + for k in range(0,len(config["text"])): + + text = config["text"][str(k)][randomNumber-1] + + text = text.replace('$PlayerName$',player) + text = text.replace('$Days$',date) sendMsg(server,text,player,str(k)) def on_player_joined(server: PluginServerInterface,player: str,info: Info): + decodeMsg(server,player) def on_load(server: PluginServerInterface, old): @@ -55,11 +52,11 @@ def on_load(server: PluginServerInterface, old): global config global comm - config = server.load_config_simple('config.json',default_config) - comm = '!!joinMsg' + config = server.load_config_simple("config.json",default_config) + comm = "!!joinMsg" - server.register_help_message(comm,'展示欢迎消息') + server.register_help_message(comm,"展示欢迎消息") server.register_command( Literal(comm) - .runs(lambda src: decodeMsg(server,'Test_Player')) + .runs(lambda src: decodeMsg(server,"Test_Player")) ) \ No newline at end of file