Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
axty666 authored Jun 30, 2022
1 parent 4d48f27 commit 143e8c5
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 31 deletions.
12 changes: 6 additions & 6 deletions axty/bot_plugins/about.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
from nonebot import on_command, CommandSession
from nonebot import on_natural_language, NLPSession, IntentCommand
from jieba import posseg


__plugin_name__ = '关于axtybot'
__plugin_usage__ = (
'有关的axtybot的信息和感谢列表'
)

@on_command('aboutaxtybot', aliases={'关于axtybot', '/ababout', '/ab关于'})
@on_command('关于', aliases={'关于axtybot', 'about', '关于', 'aboutaxtybot'})
async def _(session: CommandSession):
await session.send('''A MARYT PLAYER BOT NAMED axtybot
version:1.9.20220403_MARYT10th
版本:1.9.20220403_MARYT10th
await session.send('''a•x•t•y•b•o•t
version:1.9.0.BlessedorCursed
(将升级到2.x版本,敬请期待)
如果你想与axty一起整点活可以私聊axty,若了解Python编程可以前往https://github.com/axty666/axtybot 一起整活并fork you!
---------
(axty大爱Fantasy_Z(逃)''')

@on_natural_language(keywords={'/ababout', '/ab关于'}, only_to_me=False)
@on_natural_language(keywords={'关于axtybot', '/ababout', '/ab关于', 'aboutaxtybot'}, only_to_me=False)
async def _(session: NLPSession):
return IntentCommand(90.0, 'aboutaxtybot')
return IntentCommand(90.0, 'aboutaxtybot')
49 changes: 24 additions & 25 deletions axty/bot_plugins/ping.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
from nonebot.command import CommandSession
from nonebot.experimental.plugin import on_command
from nonebot import on_command, CommandSession
from nonebot import on_natural_language, NLPSession, IntentCommand
from pythonping import ping
from jieba import posseg
import tldextract


__plugin_name__ = 'ping'
__plugin_usage__ = '查看指定主机名与axtybotの连接情况'


@on_command('ping')
async def ping(session: CommandSession):
host = session.get('host', prompt='主机名?')
start_ping_result = await get_ping_of_result(host)
await session.send(start_ping_result)

@ping.args_parser
@on_command('ping', aliases=('PING', 'Ping'))
async def _(session: CommandSession):
stripped_arg = session.current_arg_text.strip()

if session.is_first_run:
if stripped_arg:
session.state['host'] = stripped_arg
return

if not stripped_arg:
session.pause('整点主机名⑧')

session.state[session.current_key] = stripped_arg
# 取得消息的内容,并且去掉首尾的空白符
host = session.current_arg_text.strip()
if not host:
host = (await session.aget(prompt='主机名是什么捏?')).strip()
while not host:
host = (await session.aget(prompt='主机名不能为空的捏')).strip()
else:
await session.send('好耶,咱正在尝试ping一波')
ping_result = await get_ping_of_result(host)
await session.send(ping_result)

async def get_ping_of_result(host: str) -> str:
ping_result = await ping_results(host)
return f'{host}与axtybot的连接情况'+ str(ping_result)

async def ping_results(host):
ping ('{host}')
try:
result = ping (host)
return f'{host}与axtybot的连接情况:\n'+ str(result)
except:
return f'{host}与axtybot连接出错,请自行排查'

@on_natural_language(keywords={'ping', 'PING'}, only_to_me=False)
async def _(session: NLPSession):
return IntentCommand(90.0, 'ping')
84 changes: 84 additions & 0 deletions axty/bot_plugins/weather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from nonebot import on_command, CommandSession
from nonebot import on_natural_language, NLPSession, IntentCommand
from jieba import posseg
from aiocache import cached
from httpx import AsyncClient, HTTPError
import jieba


# on_command 装饰器将函数声明为一个命令处理器
# 这里 weather 为命令的名字,同时允许使用别名「天气」「天气预报」「查天气」


@on_command('weather', aliases=('天气', '天气预报', '查天气'))
async def weather(session: CommandSession):
# 取得消息的内容,并且去掉首尾的空白符
args = session.current_arg_text.strip().split(' ', 1)
if not args[0]:
city = await session.aget(key='city', prompt='请问是什么城市呢?', at_sender=True)
else:
city = args[0]
is_detailed = (len(args) == 2 and args[1] == '详细') or session.state.get('is_detailed')
# 获取城市的天气预报
try:
func = get_current_weather_desc if is_detailed else get_weather_of_city
except HTTPError as e:
return f'API 服务目前不可用'
# 向用户发送天气预报
result = await func(city)
await session.send(result)

async def jieba_ns(city: str) -> str:
args = str(city)
words = posseg.lcut(args)
args = {}
for word, flag in words:
if flag == 'ns':
city = word
return city
else: # 没用 鬼知道为啥删了就无法限制
return f'请输入有效的地名!'

@cached(ttl=60)
async def get_weather_of_city(city: str) -> str:
city = await jieba_ns(city)
return (await fetch_text(f'https://wttr.in/{city}?format=1&lang=zh')).strip()

@cached(ttl=60)
async def get_current_weather_desc(city: str) -> str:
_format = (
'%l:\n'
'+%c+%C:+%t\n'
'+💦+Humidity:+%h\n'
'+💧+Precipitation:+%p\n'
'+🍃+Wind:+%w'
)
city = await jieba_ns(city)
return await fetch_text(f'https://wttr.in/{city}?format={_format}&lang=zh')

async def fetch_text(uri: str) -> str:
async with AsyncClient(headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'}) as client:
try:
res = await client.get(uri)
res.raise_for_status()
except HTTPError as e:
return f'API 服务目前不可用,或者可能输入了无效的地名'
return res.text



@on_natural_language(keywords={'天气', '天气预报', '查天气', 'weather'}, only_to_me=False)
async def _(session: NLPSession):
# 使用 jieba 将消息句子分词
words = posseg.lcut(session.msg_text.strip())
args = {}
for word, flag in words:
if flag == 'ns': # ns 表示该词为地名
args['city'] = word
return args
elif word in ('详细', '报告', '详情'):
args['is_detailed'] = True
return args

# 置信度为 90,意为将此会话当作 'weather' 命令处理
return IntentCommand(90, 'weather', args=args)

0 comments on commit 143e8c5

Please sign in to comment.