Skip to content

Commit

Permalink
Merge pull request #5 from markelovstyle/master
Browse files Browse the repository at this point in the history
Some changes
  • Loading branch information
nm17 authored May 18, 2020
2 parents 4048bd0 + 7e6af74 commit 122ac01
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 61 deletions.
7 changes: 3 additions & 4 deletions nicevk/api.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import asyncio
import dotenv
import json

from functools import wraps
from pathlib import Path

from loguru import logger
from python_rucaptcha.ImageCaptcha import ImageCaptcha, aioImageCaptcha
from vkbottle import User, Message, VKError
import dotenv
from vkbottle.framework import Middleware
import json

from nicevk.errors import CaptchaError


Expand Down
5 changes: 4 additions & 1 deletion nicevk/plugins/help_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@

@user.on.message_handler(text=".help")
async def help_(ans: Message):
await edit(ans, "Available commands:\n\n" + "\n".join(commands))
await edit(
ans.peer_id, ans.id,
"Available commands:\n\n" + "\n".join(commands)
)
12 changes: 6 additions & 6 deletions nicevk/plugins/ignore_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def help_(ans: Message, username: str):
).object_id,
]
save_state()
await edit(ans, "User ignored")
await edit(ans.peer_id, ans.id, "User ignored")


@user.on.message_handler(text=".rm-ignore <username>")
Expand All @@ -42,14 +42,14 @@ async def help_(ans: Message, username: str):
)
]
except ValueError:
await edit(ans, "That user is not muted")
await edit(ans.peer_id, ans.id, "That user is not muted")
else:
await edit(ans, "User removed from ignore")
await edit(ans.peer_id, ans.id, "User removed from ignore")
finally:
save_state()


@user.on.message_handler()
async def middleware(message: Message):
if message.from_id in state.get("ignore", []):
await message.api.messages.delete([message.id])
async def middleware(ans: Message):
if ans.from_id in state.get("ignore", []):
await ans.api.messages.delete(message_ids=ans.id)
10 changes: 3 additions & 7 deletions nicevk/plugins/load_plugin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from datetime import datetime
import wget
import runpy

from datetime import datetime
from vkbottle import Message

from nicevk.api import user, commands, nicevk_folder

import wget

import runpy

commands.append(".dl_mod <url> - download and execute plugin")


Expand All @@ -19,7 +16,6 @@ async def help_(ans: Message, url: str):
nicevk_folder / f"module_{datetime.now().isoformat().replace('.', ' ')}.py"
)
wget.download(url.strip(), out=out_path)

runpy.run_path(out_path)

await ans.api.messages.edit(
Expand Down
15 changes: 9 additions & 6 deletions nicevk/plugins/notes_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@

@user.on.message_handler(text=".notes")
async def list_notes(ans: Message):
await ans.api.messages.edit(
ans.peer_id,
ans.id,
f"Available notes:\n\n" + "\n".join(state.get("notes", {}).keys()),
await edit(
ans.peer_id, ans.id,
f"Available notes:\n\n" + "\n".join(state.get("notes", {}).keys())
)


Expand All @@ -28,7 +27,8 @@ async def get_notes(ans: Message, name: str):
save_state()
if name in state["notes"].keys():
await edit(
ans, f"Note '{name}'\n\n{state['notes'][name]}"
ans.peer_id, ans.id,
f"Note '{name}'\n\n{state['notes'][name]}"
)
else:
await ans.api.messages.edit(ans.peer_id, ans.id, f"Note '{name}' is not found")
Expand All @@ -39,5 +39,8 @@ async def save_notes(ans: Message, name: str, text: str):
if "notes" not in state.keys():
state["notes"] = {}
state["notes"][name] = text
await ans.api.messages.edit(ans.peer_id, ans.id, f"Note '{name}' saved!")
await edit(
ans.peer_id, ans.id,
f"Note '{name}' saved!"
)
save_state()
3 changes: 1 addition & 2 deletions nicevk/plugins/restart_plugin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import asyncio
import subprocess
from functools import wraps

from functools import wraps
from vkbottle import Message, VKError

from nicevk.api import user, commands, state, save_state, coro

commands.append(".restart - restart nicevk")
Expand Down
2 changes: 1 addition & 1 deletion nicevk/plugins/spam_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
@user.on.message_handler(text=".spam <amount> <text>")
async def help_(ans: Message, amount: str, text: str):
amount = int(amount)
await user.api.messages.delete([ans.id], delete_for_all=True)
await user.api.messages.delete(ans.id, delete_for_all=True)
for i in range(amount):
await ans(text)
51 changes: 17 additions & 34 deletions nicevk/utils.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
from vkbottle import Message
from vkbottle.api import UserApi

api = UserApi.get_current()

async def edit(ans: Message, text: str):
data = ans.__dict__
keys_to_pop = [
"message",
"flags",
"timestamp",
"text",
"random_id",
"id",
"conversation_message_id",
"from_id",
"date",
"out",
"read_state",
"ref",
"ref_source",
"important",
"reply_message",
"fwd_messages",
"action",
]
for key in keys_to_pop:
data.pop(key, None)
if ans.geo is not None:
lat, long = ans.geo.coordinates.latitude, ans.geo.coordinates.longitude
data["lat"] = lat
data["long"] = long
data.pop("geo", None)
await ans.api.messages.edit(
message=text,
attachment=",".join(data.pop("attachments")),
**data,
keep_forward_messages=True

def get_params(obj: dict) -> dict:
return {
k: v for k, v in obj.items()
if v is not None and k != "params"
}


async def edit(
peer_id: int, msg_id: int,
text: str, **params
):
locals().update(params)
return await api.messages.edit(
**get_params(locals())
)

0 comments on commit 122ac01

Please sign in to comment.