Skip to content

Commit

Permalink
Fix database operation error
Browse files Browse the repository at this point in the history
  • Loading branch information
KunoiSayami committed Oct 9, 2020
1 parent 9fc7981 commit e683d9e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 78 deletions.
97 changes: 25 additions & 72 deletions cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import time
from dataclasses import dataclass
from typing import Dict, List, Optional, Union

from libpy3.aiopgsqldb import PgSQLdb
Expand All @@ -35,80 +36,30 @@ def admins_list(self) -> Optional[List[int]]:
return self._admins_list

@admins_list.setter
def admins_list(self, value: List[int]) -> List[int]:
def admins_list(self, value: List[int]) -> None:
del self._admins_list
self._admins_list = value
self._last_fetch = time.time()
return self._admins_list


@dataclass(init=False)
class GroupProperty:
welcome_text: Optional[str]
no_welcome: bool
no_service_msg: bool
no_new_member: bool
no_blue: bool
ignore_err: bool

def __init__(self, text: Optional[str], no_welcome: bool, no_service_msg: bool,
no_new_member: bool, no_blue: bool, ignore_err: bool):
self._welcome_text: Optional[str] = text
self._no_welcome: bool = no_welcome
self._no_service_msg: bool = no_service_msg
self._no_new_member: bool = no_new_member
self._no_blue: bool = no_blue
self._ignore_err: bool = ignore_err
self.welcome_text: Optional[str] = text
self.no_welcome: bool = no_welcome
self.no_service_msg: bool = no_service_msg
self.no_new_member: bool = no_new_member
self.no_blue: bool = no_blue
self.ignore_err: bool = ignore_err
self._admins_list: GroupAdmins = GroupAdmins()
self._last_fetch: float = 0.0

@property
def no_welcome(self) -> bool:
return self._no_welcome

@property
def no_service_msg(self) -> bool:
return self._no_service_msg

@property
def no_blue(self) -> bool:
return self._no_blue

@property
def no_new_member(self) -> bool:
return self._no_new_member

@property
def ignore_err(self) -> bool:
return self._ignore_err

@no_welcome.setter
def no_welcome(self, value: bool) -> None:
self._no_welcome = value

@no_service_msg.setter
def no_service_msg(self, value: bool) -> None:
self._no_service_msg = value

@no_blue.setter
def no_blue(self, value: bool) -> None:
self._no_blue = value

@no_new_member.setter
def no_new_member(self, value: bool) -> None:
self._no_new_member = value

@ignore_err.setter
def ignore_err(self, value: bool) -> None:
self._ignore_err = value

@property
def welcome_text(self) -> Optional[str]:
return self._welcome_text

@welcome_text.setter
def welcome_text(self, value: str) -> None:
self._welcome_text = value

@property
def last_fetch(self) -> float:
return self._last_fetch

@last_fetch.setter
def last_fetch(self, value: float) -> None:
self._last_fetch = value

@property
def admins(self) -> Optional[List[int]]:
Expand Down Expand Up @@ -166,12 +117,14 @@ async def insert_group(self, chat_id: int) -> GroupProperty:

async def update_group(self, chat_id: int, new_property: GroupProperty, no_update: bool = False) -> None:
self.groups.update({chat_id: new_property})
if no_update: return
if await self.conn.query1('''SELECT 1 FROM "welcome_msg" WHERE "group_id" = %s''',
if no_update:
return
if await self.conn.query1('''SELECT 1 FROM "welcome_msg" WHERE "group_id" = $1''',
chat_id) is None: # type: ignore
await self.conn.execute('''INSERT INTO "welcome_msg"
("group_id", "msg", "ignore_err", "no_blue", "no_service", "no_welcome", "no_new_member")
VALUES ($1, $2, $3, $4, $5, $6, $7)''',
("group_id", "msg", "ignore_err", "no_blue", "no_service",
"no_welcome", "no_new_member")
VALUES ($1, $2, $3, $4, $5, $6, $7)''',
chat_id,
new_property.welcome_text,
new_property.ignore_err,
Expand All @@ -182,8 +135,8 @@ async def update_group(self, chat_id: int, new_property: GroupProperty, no_updat
) # type: ignore
else:
await self.conn.execute('''UPDATE "welcome_msg"
SET "msg" = $1, "ignore_err" = $2, "no_blue" = $3, "no_service" = $4,
"no_welcome" = $5, "no_new_member" = $6 WHERE "group_id" = $7''',
SET "msg" = $1, "ignore_err" = $2, "no_blue" = $3, "no_service" = $4,
"no_welcome" = $5, "no_new_member" = $6 WHERE "group_id" = $7''',
new_property.welcome_text,
new_property.ignore_err,
new_property.no_blue,
Expand All @@ -194,4 +147,4 @@ async def update_group(self, chat_id: int, new_property: GroupProperty, no_updat
) # type: ignore

async def delete_group(self, chat_id: int) -> None:
await self.conn.execute('''DELETE FROM "welcome_msg" WHERE "group_id" = %s''', chat_id) # type: ignore
await self.conn.execute('''DELETE FROM "welcome_msg" WHERE "group_id" = $1''', chat_id) # type: ignore
12 changes: 6 additions & 6 deletions welcome_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,25 @@ async def new_chat_member(self, client: Client, msg: Message) -> None:
welcome_text = group_setting.welcome_text
if welcome_text is not None:
try:
last_msg = (await msg.reply(welcome_text.replace('$name', parse_user_name(msg.from_user)),
last_msg = (await msg.reply(welcome_text.replace('$name', parse_user_name(msg.new_chat_members[0])),
parse_mode='markdown', disable_web_page_preview=True)).message_id
except pyrogram.errors.ChatWriteForbidden:
logger.error('Got ChatWriterForbidden in %d', msg.chat.id)
await msg.chat.leave()
await self.groups.delete_group(msg.chat.id)
return
pervious_msg = await self.conn.query_last_message_id(msg.chat.id)
previous_msg = await self.conn.query_last_message_id(msg.chat.id)
await self.conn.insert_last_message_id(msg.chat.id, last_msg)
if self.groups[msg.chat.id].no_welcome:
if pervious_msg is not None:
await client.delete_messages(msg.chat.id, pervious_msg)
if previous_msg is not None:
await client.delete_messages(msg.chat.id, previous_msg)

async def left_chat_member(self, _client: Client, msg: Message) -> None:
if self.bot_id == msg.left_chat_member.id:
await self.groups.delete_group(msg.chat.id)

async def privileges_control(self, client: Client, msg: Message) -> None:
bot_name = re.match(r'^/(setwelcome|clear|status)(@[a-zA-Z_]*bot)?\s?', msg.text).group(2)
bot_name = re.match(r'^/(setwelcome|clear|status|setflag)(@[a-zA-Z_]*bot)?\s?', msg.text).group(2)
if bot_name is not None and bot_name[1:] != self.bot_name:
return
group_info = await self.get_groups_cache_s(msg.chat.id)
Expand Down Expand Up @@ -216,7 +216,7 @@ async def set_group_prop(self, _client: Client, msg: Message) -> None:
group_info.no_blue = value
elif r.group(2) == 'ignore_err':
group_info.ignore_err = value
elif r.group(2) == 'no_service_msg':
elif r.group(2) == 'no_service':
group_info.no_service_msg = value
elif r.group(2) == 'no_new_member':
group_info.no_new_member = value
Expand Down

0 comments on commit e683d9e

Please sign in to comment.