-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
151 lines (123 loc) · 5.63 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import discord
import json
import logging
import requests
ICONASCII = """
.
#= ___ _ _ ___ ,__ . ___ ____ .
*::- .' `. \ ___ ____ ` ___/ .' \ .___ ___ / ` _/_ .' \ __. .___ ___ / \ __. _/_
*-::+ | | |/ \ ( | / | | / \ / ` |__ | | .' \ / \ .' ` |,_-< .' \ |
* .# | | | ` `--. | ,' | | | ' | | | | | | | | ' |----' | ` | | |
.::.+ `.__.' `___,' \___.' / `___,' `.__, / `.__/| | \__/ `.__, `._.' / `.___, `----' `._.' \__/
:.
==========================================================================================================================
"""
print(ICONASCII)
# 設定 logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
with open('config.json', 'r', encoding='utf8') as jfile:
config = json.load(jfile)
intents = discord.Intents.default()
intents.members = True
bot = discord.Bot(intents=intents)
# 定義基本常數
SAMHEADERURL = "https://img.onl/Kgrc15"
TOKEN = config["discord-token"]
@bot.slash_command(name="ip", description="顯示伺服器IP")
async def ip(ctx: discord.ApplicationContext):
# 判斷伺服器是否開啟
if config["java-server"]["opened"]:
jeId = config["java-server"]["mc-server-ip"]
jeVer = config["java-server"]["mc-server-version"]
if config["java-server"]["mc-server-port"] == "":
jePort = "19132"
else:
jePort = config["bedrock-server"]["mc-server-port"]
jeId = config["java-server"]["mc-server-ip"] + ":" + str(jePort)
else:
jeId = "伺服器未開啟"
if config["bedrock-server"]["opened"]:
# 是否為Realm伺服器
if config["bedrock-server"]["mc-server-is-realm"]:
beId = f"Realm伺服器,加入連結為{config['bedrock-server']['mc-server-realm-uri']}"
beVer = "目前的最新版本"
else:
beId = config["bedrock-server"]["mc-server-ip"]
if config["bedrock-server"]["mc-server-latested"]:
beVer = "目前的最新版本"
else:
beVer = config["bedrock-server"]["mc-server-version"]
if config["bedrock-server"]["mc-server-port"] == "":
bePort = "19132"
else:
bePort = config["bedrock-server"]["mc-server-port"]
else:
beId = "伺服器未開啟"
# 創建一個 Embed 物件
embed = discord.Embed(title="伺服器資訊", color=0x00ff00)
embed.add_field(name="Java 伺服器", value=jeId, inline=False)
embed.add_field(name="Java 版本", value=jeVer, inline=False)
embed.add_field(name="Bedrock 伺服器", value=beId, inline=False)
embed.add_field(name="Bedrock 端口", value=bePort, inline=False)
embed.add_field(name="Bedrock 版本", value=beVer, inline=False)
embed.set_footer(text="機器人由 SamHacker 設計", icon_url=SAMHEADERURL)
# 發送 Embed 訊息
await ctx.respond(embed=embed)
# ======================
# 取得伺服器版本(Beta)
# ======================
@bot.slash_command(name="version", description="顯示伺服器版本")
async def version(ctx: discord.ApplicationContext):
# 判斷伺服器是否開啟
if config["java-server"]["opened"]:
jeVer = config["java-server"]["mc-server-version"]
else:
jeVer = "伺服器未開啟"
if config["bedrock-server"]["opened"]:
# 是否為Realm伺服器
if config["bedrock-server"]["mc-server-is-realm"]:
beVer = "目前的最新版本"
else:
if config["bedrock-server"]["mc-server-latested"]:
beVer = "目前的最新版本"
else:
beVer = config["bedrock-server"]["mc-server-version"]
else:
beVer = "伺服器未開啟"
# 創建一個 Embed 物件
embed = discord.Embed(title="伺服器版本", color=0x00ff00)
embed.add_field(name="Java 版本", value=jeVer, inline=False)
embed.add_field(name="Bedrock 伺服器", value=beVer, inline=False)
embed.set_footer(text="機器人由 SamHacker 設計", icon_url=SAMHEADERURL)
# 發送 Embed 訊息
await ctx.respond(embed=embed)
@bot.slash_command(name="say", description="借刀殺人好方法")
async def global_command(
ctx: discord.ApplicationContext, message: str
): # Takes one integer parameter
await ctx.respond(message)
@bot.slash_command(name="alive", description="機器人的生命到頭沒")
async def alive(ctx: discord.ApplicationContext):
await ctx.respond("我是機器人,不會死")
@bot.slash_command(name="乒", description="顯示機器人延遲")
async def ping(ctx: discord.ApplicationContext):
await ctx.respond(f"乓!{round(bot.latency * 1000)}ms")
# 系統訊息
@bot.event
async def on_ready():
print(f"==========\n"*2)
print(f"Logged in as {bot.user}")
logging.info(f"Logged in as {bot.user} (ID: {bot.user.id})")
@bot.event
async def on_message(message):
# 如果訊息是由 bot 自己發送的,則直接返回
if message.author == bot.user:
return
# 如果訊息提及了 bot,則回覆 "有何貴事"
if bot.user in message.mentions:
await message.reply("有何貴事")
# 如果訊息內容包含 "活著嗎",則回覆 "我是機器人,不會死"
if "活著嗎" in message.content:
await message.reply("我是機器人,不會死")
# To learn how to add descriptions and choices to options, check slash_options.py
bot.run(TOKEN)