Skip to content

Commit

Permalink
Feature #63: trigger のエイリアス対応
Browse files Browse the repository at this point in the history
  • Loading branch information
TakumiTsuruta committed Jul 8, 2022
1 parent 06c0b2e commit 2faabf2
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions cogs/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class Trigger(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.worksheet = None

@commands.Cog.listener()
async def on_message(self, message):
Expand All @@ -32,19 +33,19 @@ async def on_message(self, message):
gc = gspread.authorize(credentials)

# 共有設定したスプレッドシートのtriggerシートを開く
worksheet = gc.open_by_key(DIC_KEY).worksheet('trigger')
self.worksheet = gc.open_by_key(DIC_KEY).worksheet('trigger')

trigger = message.content.lstrip(PREFIX)
trigger_cell = worksheet.find(str(trigger), in_column=1, case_sensitive=False)
# ヘッダー行をスプレッドシートから取得
header_list: List[str] = self.worksheet.row_values(2)

if trigger_cell is None:
trigger: str = message.content.lstrip(PREFIX)
row_num = self._find_trigger_row_number(trigger, header_list)

if not row_num:
return
else:
# ヘッダー行データをスプレッドシートから取得
header_list: List[str] = worksheet.row_values(2)

# トリガー行データをスプレッドシートから取得
trigger_value_list: List[str] = worksheet.row_values(trigger_cell.row)
trigger_value_list: List[str] = self.worksheet.row_values(row_num)

pad_len = len(header_list) - len(trigger_value_list)
pad_list = ["" for _ in range(pad_len)]
Expand Down Expand Up @@ -96,6 +97,32 @@ def _get_index(self, target: List[str], value: str) -> Optional[int]:
return index
except ValueError:
return None

def _find_trigger_row_number(self, trigger: str, header_list: List[str]) -> Optional[int]:
"""triggerが含まれている行番号を返します
Args:
trigger (str): trigger
header_list (List[str]): trigger db のヘッダーリスト
Returns:
Optional[int]: trigger が含まれている行番号
"""
trigger_columns = ["trigger", "alias01", "alias01"]
for trigger_column in trigger_columns:
index = self._get_index(header_list, trigger_column)
if not index:
# header_list に trigger_column が存在しない場合
continue
else:
trigger_cell = self.worksheet.find(trigger, in_column=index, case_sensitive=False)
if not trigger_cell:
# trigger column に trigger が存在しない場合
continue
else:
return trigger_cell.row

return None


def setup(bot):
Expand Down

0 comments on commit 2faabf2

Please sign in to comment.