#yo im having a error can y'all check it?

7 messages · Page 1 of 1 (latest)

ashen loom
#

Thanks for your question :clap:, if someone gives you an answer it would be great if you thanked them with a :white_check_mark: in response. This response will earn you both points for special roles on this server.

#

`import http.client
import json
import time

with open('./config.json') as f:
config_data = json.load(f)
channel_id = config_data['Config'][0]['channelid'] # config.json
token = config_data['Config'][0]['token'] # config.json
message = config_data['Config'][0]['message'] # config.json

header_data = {
"Content-Type": "application/json",
"User-Agent": "DiscordBot",
"Authorization": token
}

def get_connection():
return http.client.HTTPSConnection("discord.com", 443)

def send_message(conn, channel_id, message_data):
try:
conn.request("POST", f"/api/v10/channels/{channel_id}/messages", message_data, header_data)
resp = conn.getresponse()

    if 199 < resp.status < 300: 
        print("Message Sent.") 
    else: 
        print(f"HTTP {resp.status}: {resp.reason}")
except: 
    print("Error.") 

def main():
message_data = {
"content": message,
"tts": False
}

send_message(get_connection(), channel_id, json.dumps(message_data)) 

if name == 'main':
while True:
main()
time.sleep(60) # 1 Hour`

#

this is the code

#

Traceback (most recent call last):
File "/home/runner/AutoSendDiscordMessage/main.py", line 6, in <module>
config_data = json.load(f)
File "/nix/store/1gc9wvzsy15pclrqfspii166p52lmh5i-python3-3.10.13/lib/python3.10/json/init.py", line 293, in load
return loads(fp.read(),
File "/nix/store/1gc9wvzsy15pclrqfspii166p52lmh5i-python3-3.10.13/lib/python3.10/json/init.py", line 346, in loads
return _default_decoder.decode(s)
File "/nix/store/1gc9wvzsy15pclrqfspii166p52lmh5i-python3-3.10.13/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/nix/store/1gc9wvzsy15pclrqfspii166p52lmh5i-python3-3.10.13/lib/python3.10/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid control character at: line 6 column 63 (char 232)
[?25l

#

and i get this

#

@ashen loom arawnio.'s Question:

Traceback (most recent call last): File "/home/runner/AutoSendDiscordMessage/main.py", line 6, in <module> config_data = json.load(f) File "/nix/store/1gc9wvzsy15pclrqfspii166p52lmh5i-python3-3.10.13/lib/python3.10/json/__init__.py", line 293, in load return loads(fp.read(), File "/nix/store/1gc9wvzsy15pclrqfspii166p52lmh5i-python3-3.10.13/lib/python3.10/json/__init__.py", line 346, in loads return _default_decoder.decode(s) File "/nix/store/1gc9wvzsy15pclrqfspii166p52lmh5i-python3-3.10.13/lib/python3.10/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/nix/store/1gc9wvzsy15pclrqfspii166p52lmh5i-python3-3.10.13/lib/python3.10/json/decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Invalid control character at: line 6 column 63 (char 232) [?25l

The error message you're seeing indicates that your JSON file (config_data) contains an invalid control character at line 6, column 63. JSON strings must not contain control characters (characters with ASCII codes less than 0x20) other than the escape sequences (\b, \f, \n, \r, \t, \\, \", and \/).

Here's what you can do to fix this issue:

  1. Open the JSON file and look for any control characters at the specified location.
  2. Remove the invalid character or replace it with a valid escape sequence.
  3. Ensure that your JSON file is properly formatted as per JSON standards.

If you're unsure of what the character might be, a simple way to make your JSON file safe for parsing is to open it in a text editor that displays non-printable characters. Alternatively, you could use a tool or a script to scan your JSON file and escape or remove any invalid characters.

For example, you could use Python to read the file, detect non-printable characters, and fix the issue:

import json
import re

def remove_control_characters(json_text):
#

Pattern to match all control characters except common escape sequences

control_char_pattern = r'[\x00-\x1f\x7f-\x9f]+(?!\\b|\\f|\\n|\\r|\\t|\\\\|\\"|\\/)'
# Replace control characters with an empty string
return re.sub(control_char_pattern, '', json_text)

Path to the JSON file

file_path = '/path/to/your/jsonfile.json'

Open the file, read the content and remove possible control characters

with open(file_path, 'r') as f:
file_content = f.read()
safe_content = remove_control_characters(file_content)

Attempt to decode the cleaned JSON string

try:
config_data = json.loads(safe_content)
except json.decoder.JSONDecodeError as e:
print(f'JSON decoding error: {e}')

Optionally, overwrite the JSON file with the corrected content

with open(file_path, 'w') as f:
f.write(safe_content)


Please replace '/path/to/your/jsonfile.json' with the actual path to your JSON file.