Add channel id by name message
This commit is contained in:
parent
3d7ec85a55
commit
4cd8fb80da
5 changed files with 45 additions and 12 deletions
|
|
@ -28,6 +28,7 @@ class Bot:
|
|||
INIT_MESSAGE: str = ('Bot initialized.\nThis is the current configuration used.\n'
|
||||
'You can upload a new one to update the configuration.')
|
||||
MAX_DOWNLOAD_SIZE: int = 50_000
|
||||
YT_CHANNEL_NAME_URL = 'https://www.youtube.com/@'
|
||||
|
||||
class Task(Enum):
|
||||
DELETE_MESSAGES = 1
|
||||
|
|
@ -41,7 +42,7 @@ class Bot:
|
|||
raise RuntimeError('Cannot current bot version')
|
||||
return tomllib.loads(pyproject_path.read_text(encoding='utf-8'))['project']['version']
|
||||
|
||||
def __init__(self, bot_token: str, guild_id: int, config: Config | None = None,
|
||||
def __init__(self, bot_token: str, guild_id: int, yt_api_key: str, config: Config | None = None,
|
||||
log_level: int = logging.INFO):
|
||||
self.config: Config = config or Config()
|
||||
self.guild_id = guild_id
|
||||
|
|
@ -86,7 +87,7 @@ class Bot:
|
|||
raise RuntimeError("Couldn't initialize bot channel/role/permission")
|
||||
self.bot_channel: TextChannel = bot_channel
|
||||
|
||||
self.yt_manager = YoutubeManager(logger=self.logger)
|
||||
self.yt_manager = YoutubeManager(api_key=yt_api_key, logger=self.logger)
|
||||
self._yt_subscriptions: Subscriptions = {}
|
||||
self._scan_bot_channel()
|
||||
self.tasks.append((
|
||||
|
|
@ -144,10 +145,31 @@ class Bot:
|
|||
new_subscriptions: Subscriptions | None = None
|
||||
delayed_delete: dict[int, Message] = {}
|
||||
immediate_delete: dict[int, Message] = {}
|
||||
|
||||
for message in messages:
|
||||
if init_message_found:
|
||||
self.logger.debug('Marking message for immediate deletion (init found): %s', message)
|
||||
immediate_delete[message.id] = message
|
||||
if message.author.id != self.bot_user.id and message.content.startswith(self.YT_CHANNEL_NAME_URL):
|
||||
self.logger.debug('Parsing message for youtube channel name conversion: %s', message)
|
||||
answers: list[str] = []
|
||||
for line in message.content.splitlines():
|
||||
if line.startswith(self.YT_CHANNEL_NAME_URL):
|
||||
channel_name = line.rstrip()[len(self.YT_CHANNEL_NAME_URL):]
|
||||
channel_id = self.yt_manager.request_channel_id(
|
||||
channel_name, request_timeout=self.config.request_timeout)
|
||||
answers.append(f'{channel_name} -> {channel_id}')
|
||||
bot_message = self.discord_manager.create_message(self.bot_channel, {
|
||||
'content': '\n'.join(answers),
|
||||
'message_reference': MessageReference(
|
||||
type=MessageReferenceType.DEFAULT,
|
||||
message_id=message.id,
|
||||
channel_id=self.bot_channel.id,
|
||||
guild_id=None,
|
||||
fail_if_not_exists=None)}, request_timeout=self.config.request_timeout)
|
||||
delayed_delete[bot_message.id] = bot_message
|
||||
delayed_delete[message.id] = message
|
||||
else:
|
||||
self.logger.debug('Marking message for immediate deletion (init found): %s', message)
|
||||
immediate_delete[message.id] = message
|
||||
continue
|
||||
|
||||
if len(message.attachments) <= 0:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue