2020-02-19 15:31:39 +00:00
|
|
|
|
""" Message related utilities. """
|
|
|
|
|
|
|
|
|
|
from telethon.tl.functions.messages import DeleteChatUserRequest
|
2021-07-15 11:27:22 +00:00
|
|
|
|
from telethon.tl.functions.channels import LeaveChannelRequest
|
2021-11-25 09:44:38 +00:00
|
|
|
|
from telethon.errors import ForbiddenError, AuthKeyError
|
2021-12-10 15:37:07 +00:00
|
|
|
|
from telethon.errors.rpcerrorlist import ChatIdInvalidError, FloodWaitError, UserNotParticipantError, MessageIdInvalidError
|
2021-08-18 08:41:22 +00:00
|
|
|
|
from distutils.util import strtobool
|
2021-09-15 13:24:56 +00:00
|
|
|
|
|
|
|
|
|
from telethon.tl.types import ChannelForbidden
|
|
|
|
|
|
2021-11-25 09:44:38 +00:00
|
|
|
|
from pagermaid import bot, log, config
|
2020-02-19 15:31:39 +00:00
|
|
|
|
from pagermaid.listener import listener
|
2022-01-20 08:27:20 +00:00
|
|
|
|
from pagermaid.utils import lang, alias_command, client
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
|
|
2021-11-10 07:46:07 +00:00
|
|
|
|
def isfloat(value):
|
|
|
|
|
try:
|
|
|
|
|
float(value)
|
|
|
|
|
return True
|
|
|
|
|
except ValueError:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("id"),
|
2021-04-12 16:25:32 +00:00
|
|
|
|
description=lang('id_des'))
|
2020-02-19 15:31:39 +00:00
|
|
|
|
async def userid(context):
|
|
|
|
|
""" Query the UserID of the sender of the message you replied to. """
|
2020-08-08 11:40:54 +00:00
|
|
|
|
message = await context.get_reply_message()
|
|
|
|
|
text = "Message ID: `" + str(context.message.id) + "`\n\n"
|
|
|
|
|
text += "**Chat**\nid:`" + str(context.chat_id) + "`\n"
|
2021-02-07 01:24:10 +00:00
|
|
|
|
msg_from = context.chat if context.chat else (await context.get_chat())
|
2020-08-08 11:40:54 +00:00
|
|
|
|
if context.is_private:
|
|
|
|
|
try:
|
2021-02-07 01:24:10 +00:00
|
|
|
|
text += "first_name: `" + msg_from.first_name + "`\n"
|
2020-08-08 11:40:54 +00:00
|
|
|
|
except TypeError:
|
|
|
|
|
text += "**死号**\n"
|
2021-02-07 01:24:10 +00:00
|
|
|
|
if msg_from.last_name:
|
|
|
|
|
text += "last_name: `" + msg_from.last_name + "`\n"
|
|
|
|
|
if msg_from.username:
|
|
|
|
|
text += "username: @" + msg_from.username + "\n"
|
|
|
|
|
if msg_from.lang_code:
|
|
|
|
|
text += "lang_code: `" + msg_from.lang_code + "`\n"
|
2020-08-08 11:40:54 +00:00
|
|
|
|
if context.is_group or context.is_channel:
|
2021-02-07 01:24:10 +00:00
|
|
|
|
text += "title: `" + msg_from.title + "`\n"
|
2021-06-19 07:25:31 +00:00
|
|
|
|
try:
|
|
|
|
|
if msg_from.username:
|
|
|
|
|
text += "username: @" + msg_from.username + "\n"
|
|
|
|
|
except AttributeError:
|
|
|
|
|
await context.edit(lang('leave_not_group'))
|
|
|
|
|
return
|
2021-02-07 01:24:10 +00:00
|
|
|
|
text += "date: `" + str(msg_from.date) + "`\n"
|
2020-02-19 15:31:39 +00:00
|
|
|
|
if message:
|
2021-06-19 07:25:31 +00:00
|
|
|
|
text += "\n" + lang('id_hint') + "\nMessage ID: `" + str(message.id) + "`\n\n**User**\nid: `" + str(message.sender_id) + "`"
|
2020-08-08 11:40:54 +00:00
|
|
|
|
try:
|
2021-06-19 07:25:31 +00:00
|
|
|
|
if message.sender.bot:
|
|
|
|
|
text += f"\nis_bot: {lang('id_is_bot_yes')}"
|
|
|
|
|
try:
|
|
|
|
|
text += "\nfirst_name: `" + message.sender.first_name + "`"
|
|
|
|
|
except TypeError:
|
|
|
|
|
text += f"\n**{lang('id_da')}**"
|
|
|
|
|
if message.sender.last_name:
|
|
|
|
|
text += "\nlast_name: `" + message.sender.last_name + "`"
|
|
|
|
|
if message.sender.username:
|
|
|
|
|
text += "\nusername: @" + message.sender.username
|
|
|
|
|
if message.sender.lang_code:
|
|
|
|
|
text += "\nlang_code: `" + message.sender.lang_code + "`"
|
|
|
|
|
except AttributeError:
|
|
|
|
|
pass
|
2020-08-08 11:40:54 +00:00
|
|
|
|
if message.forward:
|
|
|
|
|
if str(message.forward.chat_id).startswith('-100'):
|
|
|
|
|
text += "\n\n**Forward From Channel**\nid: `" + str(
|
|
|
|
|
message.forward.chat_id) + "`\ntitle: `" + message.forward.chat.title + "`"
|
2021-09-15 13:24:56 +00:00
|
|
|
|
if not isinstance(message.forward.chat, ChannelForbidden):
|
|
|
|
|
if message.forward.chat.username:
|
|
|
|
|
text += "\nusername: @" + message.forward.chat.username
|
|
|
|
|
text += "\nmessage_id: `" + str(message.forward.channel_post) + "`"
|
|
|
|
|
if message.forward.post_author:
|
|
|
|
|
text += "\npost_author: `" + message.forward.post_author + "`"
|
|
|
|
|
text += "\ndate: `" + str(message.forward.date) + "`"
|
2020-08-08 11:40:54 +00:00
|
|
|
|
else:
|
|
|
|
|
if message.forward.sender:
|
|
|
|
|
text += "\n\n**Forward From User**\nid: `" + str(
|
|
|
|
|
message.forward.sender_id) + "`"
|
|
|
|
|
try:
|
2021-06-19 07:25:31 +00:00
|
|
|
|
if message.forward.sender.bot:
|
|
|
|
|
text += f"\nis_bot: {lang('id_is_bot_yes')}"
|
|
|
|
|
try:
|
|
|
|
|
text += "\nfirst_name: `" + message.forward.sender.first_name + "`"
|
|
|
|
|
except TypeError:
|
|
|
|
|
text += f"\n**{lang('id_da')}**"
|
|
|
|
|
if message.forward.sender.last_name:
|
|
|
|
|
text += "\nlast_name: `" + message.forward.sender.last_name + "`"
|
|
|
|
|
if message.forward.sender.username:
|
|
|
|
|
text += "\nusername: @" + message.forward.sender.username
|
|
|
|
|
if message.forward.sender.lang_code:
|
|
|
|
|
text += "\nlang_code: `" + message.forward.sender.lang_code + "`"
|
|
|
|
|
except AttributeError:
|
|
|
|
|
pass
|
2020-08-08 11:40:54 +00:00
|
|
|
|
text += "\ndate: `" + str(message.forward.date) + "`"
|
2020-06-06 14:40:52 +00:00
|
|
|
|
await context.edit(text)
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("uslog"),
|
2021-04-12 16:25:32 +00:00
|
|
|
|
description=lang('uslog_des'),
|
2020-02-19 15:31:39 +00:00
|
|
|
|
parameters="<string>")
|
2020-03-20 12:15:45 +00:00
|
|
|
|
async def uslog(context):
|
2020-02-19 15:31:39 +00:00
|
|
|
|
""" Forwards a message into log group """
|
|
|
|
|
if strtobool(config['log']):
|
|
|
|
|
if context.reply_to_msg_id:
|
|
|
|
|
reply_msg = await context.get_reply_message()
|
|
|
|
|
await reply_msg.forward_to(int(config['log_chatid']))
|
|
|
|
|
elif context.arguments:
|
|
|
|
|
await log(context.arguments)
|
|
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
|
await context.edit(lang('arg_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
|
return
|
2021-04-12 16:25:32 +00:00
|
|
|
|
await context.edit(lang('uslog_success'))
|
2020-02-19 15:31:39 +00:00
|
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
|
await context.edit(lang('uslog_log_disable'))
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("log"),
|
2021-04-12 16:25:32 +00:00
|
|
|
|
description=lang('log_des'),
|
2020-03-13 08:48:50 +00:00
|
|
|
|
parameters="<string>")
|
2021-04-12 16:25:32 +00:00
|
|
|
|
async def logging(context):
|
2020-03-13 08:48:50 +00:00
|
|
|
|
""" Forwards a message into log group """
|
|
|
|
|
if strtobool(config['log']):
|
|
|
|
|
if context.reply_to_msg_id:
|
|
|
|
|
reply_msg = await context.get_reply_message()
|
|
|
|
|
await reply_msg.forward_to(int(config['log_chatid']))
|
|
|
|
|
elif context.arguments:
|
|
|
|
|
await log(context.arguments)
|
|
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
|
await context.edit(lang('arg_error'))
|
2020-03-13 08:48:50 +00:00
|
|
|
|
return
|
|
|
|
|
await context.delete()
|
|
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
|
await context.edit(lang('uslog_log_disable'))
|
2020-03-13 08:48:50 +00:00
|
|
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("re"),
|
2021-04-12 16:25:32 +00:00
|
|
|
|
description=lang('re_des'),
|
|
|
|
|
parameters=lang('re_parameters'))
|
2020-03-20 12:15:45 +00:00
|
|
|
|
async def re(context):
|
2020-04-05 03:26:51 +00:00
|
|
|
|
""" Forwards a message into this group """
|
|
|
|
|
reply = await context.get_reply_message()
|
|
|
|
|
if reply:
|
|
|
|
|
if context.arguments == '':
|
|
|
|
|
num = 1
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
num = int(context.arguments)
|
|
|
|
|
if num > 100:
|
2021-04-12 16:25:32 +00:00
|
|
|
|
await context.edit(lang('re_too_big'))
|
2020-04-05 03:26:51 +00:00
|
|
|
|
return True
|
|
|
|
|
except:
|
2021-04-12 16:25:32 +00:00
|
|
|
|
await context.edit(lang('re_arg_error'))
|
2020-04-05 03:26:51 +00:00
|
|
|
|
return True
|
2021-08-16 14:34:37 +00:00
|
|
|
|
try:
|
|
|
|
|
await context.delete()
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
2021-12-10 15:37:07 +00:00
|
|
|
|
forward_allowed = True
|
|
|
|
|
for nums in range(0, num):
|
|
|
|
|
try:
|
|
|
|
|
if forward_allowed:
|
|
|
|
|
await reply.forward_to(reply.peer_id)
|
|
|
|
|
else:
|
|
|
|
|
await bot.send_message(reply.peer_id, reply)
|
|
|
|
|
except MessageIdInvalidError:
|
|
|
|
|
if forward_allowed:
|
|
|
|
|
forward_allowed = False
|
|
|
|
|
await bot.send_message(reply.peer_id, reply)
|
|
|
|
|
else:
|
|
|
|
|
await context.respond(lang('re_forbidden'))
|
|
|
|
|
return
|
|
|
|
|
except ForbiddenError:
|
|
|
|
|
return
|
|
|
|
|
except FloodWaitError:
|
|
|
|
|
return
|
|
|
|
|
except ValueError:
|
|
|
|
|
return
|
|
|
|
|
except AuthKeyError:
|
|
|
|
|
await context.respond(lang('re_forbidden'))
|
|
|
|
|
return
|
2020-03-13 14:08:28 +00:00
|
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
|
await context.edit(lang('not_reply'))
|
2020-03-13 14:08:28 +00:00
|
|
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("leave"),
|
2021-04-12 16:25:32 +00:00
|
|
|
|
description=lang('leave_res'))
|
2020-02-19 15:31:39 +00:00
|
|
|
|
async def leave(context):
|
|
|
|
|
""" It leaves you from the group. """
|
|
|
|
|
if context.is_group:
|
2021-04-12 16:25:32 +00:00
|
|
|
|
await context.edit(lang('leave_bye'))
|
2020-02-19 15:31:39 +00:00
|
|
|
|
try:
|
|
|
|
|
await bot(DeleteChatUserRequest(chat_id=context.chat_id,
|
|
|
|
|
user_id=context.sender_id
|
|
|
|
|
))
|
|
|
|
|
except ChatIdInvalidError:
|
2021-09-24 14:30:48 +00:00
|
|
|
|
try:
|
|
|
|
|
await bot(LeaveChannelRequest(context.chat_id))
|
|
|
|
|
except UserNotParticipantError:
|
|
|
|
|
await context.delete()
|
2020-02-19 15:31:39 +00:00
|
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
|
await context.edit(lang('leave_not_group'))
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("meter2feet"),
|
2021-04-12 16:25:32 +00:00
|
|
|
|
description=lang('m2f_des'),
|
2020-02-19 15:31:39 +00:00
|
|
|
|
parameters="<meters>")
|
|
|
|
|
async def meter2feet(context):
|
|
|
|
|
""" Convert meter to feet. """
|
|
|
|
|
if not len(context.parameter) == 1:
|
2021-04-12 16:25:32 +00:00
|
|
|
|
await context.edit(lang('arg_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
|
return
|
2021-10-03 16:42:16 +00:00
|
|
|
|
try:
|
|
|
|
|
meter = float(context.parameter[0])
|
|
|
|
|
except ValueError:
|
|
|
|
|
await context.edit(lang('re_arg_error'))
|
|
|
|
|
return
|
2020-02-19 15:31:39 +00:00
|
|
|
|
feet = meter / .3048
|
2021-10-03 16:42:16 +00:00
|
|
|
|
await context.edit(f"{lang('m2f_get')} {str(meter)}{lang('m2f_meter')} {lang('m2f_convert_to')} "
|
|
|
|
|
f"{str(feet)} {lang('m2f_feet')}")
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("feet2meter"),
|
2021-04-12 16:25:32 +00:00
|
|
|
|
description=lang('f2m_des'),
|
2020-02-19 15:31:39 +00:00
|
|
|
|
parameters="<feet>")
|
|
|
|
|
async def feet2meter(context):
|
|
|
|
|
""" Convert feet to meter. """
|
|
|
|
|
if not len(context.parameter) == 1:
|
2021-04-12 16:25:32 +00:00
|
|
|
|
await context.edit(lang('arg_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
|
return
|
2021-11-10 07:46:07 +00:00
|
|
|
|
if not isfloat(context.parameter[0]):
|
|
|
|
|
return await context.edit(lang('re_arg_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
|
feet = float(context.parameter[0])
|
|
|
|
|
meter = feet * .3048
|
2021-10-03 16:42:16 +00:00
|
|
|
|
await context.edit(f"{lang('m2f_get')} {str(feet)} {lang('m2f_feet')}{lang('m2f_convert_to')} {str(meter)} "
|
|
|
|
|
f"{lang('m2f_meter')}")
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("hitokoto"),
|
2021-04-12 16:25:32 +00:00
|
|
|
|
description=lang('hitokoto_des'))
|
2020-04-01 11:04:56 +00:00
|
|
|
|
async def hitokoto(context):
|
|
|
|
|
""" Get hitokoto.cn """
|
2020-05-16 15:10:12 +00:00
|
|
|
|
hitokoto_while = 1
|
2021-07-18 05:52:17 +00:00
|
|
|
|
hitokoto_json = None
|
2020-05-16 15:10:12 +00:00
|
|
|
|
try:
|
2022-01-20 08:27:20 +00:00
|
|
|
|
hitokoto_json = (await client.get("https://v1.hitokoto.cn/?charset=utf-8")).json()
|
2021-07-15 11:27:22 +00:00
|
|
|
|
except ValueError:
|
2020-05-16 15:10:12 +00:00
|
|
|
|
while hitokoto_while < 10:
|
|
|
|
|
hitokoto_while += 1
|
|
|
|
|
try:
|
2022-01-20 08:27:20 +00:00
|
|
|
|
hitokoto_json = (await client.get("https://v1.hitokoto.cn/?charset=utf-8")).json()
|
2020-05-16 15:10:12 +00:00
|
|
|
|
break
|
|
|
|
|
except:
|
|
|
|
|
continue
|
2021-07-18 05:52:17 +00:00
|
|
|
|
if not hitokoto_json:
|
|
|
|
|
return
|
|
|
|
|
hitokoto_type = {'a': lang('hitokoto_type_anime'), 'b': lang('hitokoto_type_manga'),
|
|
|
|
|
'c': lang('hitokoto_type_game'), 'd': lang('hitokoto_type_article'),
|
|
|
|
|
'e': lang('hitokoto_type_original'), 'f': lang('hitokoto_type_web'),
|
|
|
|
|
'g': lang('hitokoto_type_other'), 'h': lang('hitokoto_type_movie'),
|
|
|
|
|
'i': lang('hitokoto_type_poem'), 'j': lang('hitokoto_type_netease_music'),
|
|
|
|
|
'k': lang('hitokoto_type_philosophy'), 'l': lang('hitokoto_type_meme')}
|
|
|
|
|
await context.edit(f"{hitokoto_json['hitokoto']} - {hitokoto_json['from']}({hitokoto_type[hitokoto_json['type']]})")
|