Skip to content

Commit

Permalink
V1.2.0
Browse files Browse the repository at this point in the history
- 新增 随机发送信息
- 优化 重构部分代码
  • Loading branch information
JAs0n319 committed Nov 6, 2023
1 parent 8c2250d commit 79e6edb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 56 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 11 additions & 11 deletions README_zh_cn.md
Original file line number Diff line number Diff line change
@@ -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$。
2 changes: 1 addition & 1 deletion mcdreforged.plugin.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
63 changes: 30 additions & 33 deletions welcome_msg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,62 @@
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):

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"))
)

0 comments on commit 79e6edb

Please sign in to comment.