Fix discord channel name and add verification
This commit is contained in:
parent
cc40e9dcf3
commit
d645fecae0
7 changed files with 23 additions and 4 deletions
|
|
@ -95,6 +95,10 @@ class Api:
|
|||
default_auto_archive_duration: int
|
||||
default_thread_rate_limit_per_user: int
|
||||
|
||||
@staticmethod
|
||||
def delete_channel(channel_id: int) -> tuple[ApiAction, str]:
|
||||
return ApiAction.DELETE, f'/channels/{channel_id}'
|
||||
|
||||
@staticmethod
|
||||
def list_guilds(guild_id: int) -> tuple[ApiAction, str]:
|
||||
return ApiAction.GET, f'/guilds/{guild_id}/channels'
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import http.client
|
|||
import logging
|
||||
import operator
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
from typing import Any
|
||||
|
|
@ -377,6 +376,10 @@ class Bot:
|
|||
allow=Permissions.VIEW_CHANNEL | Permissions.SEND_MESSAGES,
|
||||
deny=Permissions.NONE)]},
|
||||
request_timeout=self.config.request_timeout)
|
||||
if sub_channel != subscription.name:
|
||||
self.logger.error('Cannot create channel "%s" : mismatch name -> %s',
|
||||
subscription.name, sub_channel.name)
|
||||
self.discord_manager.delete_text_channel(sub_channel.id, request_timeout=self.config.request_timeout)
|
||||
return sub_channel
|
||||
|
||||
def _refresh_subscription(self, connection: http.client.HTTPSConnection, subscription: SubscriptionInfo):
|
||||
|
|
|
|||
|
|
@ -152,6 +152,11 @@ class DiscordManager:
|
|||
raise RuntimeError(f'Error creating channel with params (no info): {params}')
|
||||
return TextChannel.from_dict(channel_info)
|
||||
|
||||
def delete_text_channel(self, channel_id: int, request_timeout: float):
|
||||
_, _ = self._send_request(
|
||||
*Api.Guild.delete_channel(channel_id=channel_id), request_timeout=request_timeout,
|
||||
expected_code=204)
|
||||
|
||||
def create_message(self, channel: TextChannel, params: Api.Message.CreateParams, request_timeout: float,
|
||||
upload_files: list[tuple[str, FileMime, bytes]] | None = None) -> Message:
|
||||
if 'content' in params and len(params['content']) >= self.MAX_MESSAGE_LENGTH:
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ from __future__ import annotations
|
|||
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
import re
|
||||
from typing import NewType
|
||||
|
||||
from breadtube_bot.unidecode import unidecode
|
||||
|
|
@ -68,7 +69,10 @@ class SubscriptionInfo:
|
|||
@staticmethod
|
||||
def discord_compatible_name(text: str) -> SubscriptionName:
|
||||
assert text, 'Channel name cannot be empty'
|
||||
return SubscriptionName(unidecode(text[:100].replace(' ', '-')))
|
||||
name = unidecode(text[:100].replace(' ', '-'))
|
||||
name = re.sub(r'-+', '-', name)
|
||||
name = re.sub(r'-+$', '', name)
|
||||
return SubscriptionName(name)
|
||||
|
||||
@staticmethod
|
||||
def from_dict(info: dict) -> SubscriptionInfo:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue