| 78 | return False |
| 79 | |
| 80 | class aclient(discord.Client): |
| 81 | def __init__(self, shodan_key) -> None: |
| 82 | super().__init__(intents=discord.Intents.default()) |
| 83 | self.shodan = shodan.Shodan(shodan_key) |
| 84 | self.tree = discord.app_commands.CommandTree(self) |
| 85 | self.activity = discord.Activity(type=discord.ActivityType.watching, name="/shodan") |
| 86 | self.discord_message_limit = 2000 |
| 87 | |
| 88 | async def send_split_messages(self, interaction, message: str, require_response=True): |
| 89 | """Sends a message, and if it's too long for Discord, splits it.""" |
| 90 | # Handle empty messages |
| 91 | if not message.strip(): |
| 92 | logging.warning("Attempted to send an empty message.") |
| 93 | return |
| 94 | |
| 95 | # Extract the user's query/command from the interaction to prepend it to the first chunk |
| 96 | query = "" |
| 97 | for option in interaction.data.get("options", []): |
| 98 | if option.get("name") == "query": |
| 99 | query = option.get("value", "") |
| 100 | break |
| 101 | |
| 102 | prepend_text = "" |
| 103 | if query: |
| 104 | prepend_text = f"Query: {query}\n\n" |
| 105 | |
| 106 | lines = message.split("\n") |
| 107 | chunks = [] |
| 108 | current_chunk = "" |
| 109 | |
| 110 | # First, add the prepend_text (if any) to the initial chunk |
| 111 | if prepend_text: |
| 112 | current_chunk += prepend_text |
| 113 | |
| 114 | for line in lines: |
| 115 | # If the individual line is too long, split it up before chunking |
| 116 | while len(line) > self.discord_message_limit: |
| 117 | sub_line = line[:self.discord_message_limit] |
| 118 | if len(current_chunk) + len(sub_line) + 1 > self.discord_message_limit: |
| 119 | chunks.append(current_chunk) |
| 120 | current_chunk = "" |
| 121 | current_chunk += sub_line + "\n" |
| 122 | line = line[self.discord_message_limit:] |
| 123 | |
| 124 | # If adding the next line to the current chunk would exceed the Discord message limit |
| 125 | if len(current_chunk) + len(line) + 1 > self.discord_message_limit: |
| 126 | chunks.append(current_chunk) |
| 127 | current_chunk = line + "\n" |
| 128 | else: |
| 129 | current_chunk += line + "\n" |
| 130 | |
| 131 | if current_chunk: |
| 132 | chunks.append(current_chunk) |
| 133 | |
| 134 | # Check if there are chunks to send |
| 135 | if not chunks: |
| 136 | logging.warning("No chunks generated from the message.") |
| 137 | return |