Improved code quality with more idiomatic Python patterns and safer initialization.
This commit is contained in:
@@ -1 +1,15 @@
|
||||
# Copyright 2026 Logan Fick
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Background tasks for Crabstero."""
|
||||
|
||||
@@ -59,40 +59,32 @@ async def ingest_channel(
|
||||
:param channel: The channel to ingest.
|
||||
:param db: The database instance.
|
||||
"""
|
||||
try:
|
||||
if not channel.permissions_for(channel.guild.me).read_message_history:
|
||||
logger.warning(
|
||||
"[%s] Unable to ingest channel history"
|
||||
" due to lacking permissions. Ignoring.",
|
||||
channel.id,
|
||||
)
|
||||
return
|
||||
|
||||
if await db.is_channel_ingested(channel.id):
|
||||
return
|
||||
|
||||
await db.mark_channel_ingested(channel.id)
|
||||
|
||||
logger.info("[%s] Starting ingestion of textable channel history.", channel.id)
|
||||
|
||||
with metrics.CHANNEL_INGESTION_DURATION.time():
|
||||
count = 0
|
||||
async for message in channel.history(limit=MAXIMUM_MESSAGES_PER_CHANNEL):
|
||||
count += 1
|
||||
await ingest_message(db, message)
|
||||
|
||||
metrics.CHANNEL_INGESTION_MESSAGES.observe(count)
|
||||
|
||||
logger.info(
|
||||
"[%s] Ingestion of channel history complete. %d messages ingested.",
|
||||
channel.id,
|
||||
count,
|
||||
)
|
||||
metrics.CHANNELS_INGESTED.inc()
|
||||
|
||||
except Exception:
|
||||
metrics.CHANNEL_INGESTION_ERRORS.inc()
|
||||
logger.exception(
|
||||
"[%s] An error occurred while ingesting textable channel history!",
|
||||
if not channel.permissions_for(channel.guild.me).read_message_history:
|
||||
logger.warning(
|
||||
"[%s] Unable to ingest channel history"
|
||||
" due to lacking permissions. Ignoring.",
|
||||
channel.id,
|
||||
)
|
||||
return
|
||||
|
||||
if await db.is_channel_ingested(channel.id):
|
||||
return
|
||||
|
||||
await db.mark_channel_ingested(channel.id)
|
||||
|
||||
logger.info("[%s] Starting ingestion of textable channel history.", channel.id)
|
||||
|
||||
with metrics.CHANNEL_INGESTION_DURATION.time():
|
||||
count = 0
|
||||
async for message in channel.history(limit=MAXIMUM_MESSAGES_PER_CHANNEL):
|
||||
count += 1
|
||||
await ingest_message(db, message)
|
||||
|
||||
metrics.CHANNEL_INGESTION_MESSAGES.observe(count)
|
||||
|
||||
logger.info(
|
||||
"[%s] Ingestion of channel history complete. %d messages ingested.",
|
||||
channel.id,
|
||||
count,
|
||||
)
|
||||
metrics.CHANNELS_INGESTED.inc()
|
||||
|
||||
Reference in New Issue
Block a user