解码麻烦

问题描述 投票:0回答:0
import disnake
from disnake.ext import commands
import re
import asyncio

from disnake import Intents
bot = commands.Bot(command_prefix="*", intents=disnake.Intents.all())



player_ku = {}

isConsole = False
isChat = True
isId = True

@bot.event
async def on_ready():
    print(f'{bot.user.name} has connected to Discord!')
    channel1 = bot.get_channel(1105450189688414208)  
    channel2 = bot.get_channel(1105450335947989042)  
    channel3 = bot.get_channel(1105694581800042566)
    last_line_file1 = ''
    last_line_file2 = ''
    while True:
        console_disabled_message_sended = False
        if isConsole:
            with open("server_log.txt", 'r', encoding='cp1251', errors='ignore') as file1: # Пусть к выводу консольи
                last_line = file1.readlines()[-1].strip() 
                last_line_utf82 = last_line.encode('cp1251', errors='backslashreplace').decode("utf8",errors='ignore')
                if last_line_utf82 != last_line_file1:

                    await channel1.send(f'```\n{last_line}\n```')

                    last_line_file1 = last_line_utf82 
                    asyncio.sleep(1)
        
        if isChat:
            with open('server_chat_log.txt', 'r', encoding="cp1251", errors='ignore') as file2: # Пусть к выводу чата

                last_line = file2.readlines()[-1].strip()  
                last_line_utf8 = last_line.encode('cp1251', errors='backslashreplace').decode("utf8",errors='ignore')
                if last_line_utf8 != last_line_file2:

                    cleaned_message = last_line_utf8.split("]: ")[1].split()
                    clean_message = " ".join(cleaned_message)
                    mega_cleaned_message = re.sub('[:(\)\\[\\]]', '', clean_message).split()
                    ku = mega_cleaned_message[1]
                    nick = mega_cleaned_message[2] 

                    if cleaned_message[0] == "[Say]":

                        await channel2.send(f"```\n💬 {clean_message}```")

                        last_line_file2 = last_line_utf8
                        if not ku in player_ku:
                            
                            player_ku[ku] = nick
                            print(player_ku)
                            with open("savedKu.txt", "a") as file:
                                for key in player_ku:
                                    encoded_key = key.encode('cp1251', errors='backslashreplace').decode('utf-8', errors='ignore')
                                    file.write(f"\n{encoded_key}: {player_ku[key]}\n")
                                    if isId:
                                        await channel3.send(f"```\n🆔{encoded_key} = {player_ku[key]}```")

                    elif cleaned_message[0] == "[Whisper]":

                        await channel2.send(f"```\n🤫 {clean_message}```")

                        last_line_file2 = last_line_utf8
                        if not ku in player_ku:
                            print(player_ku)
                            

                    elif cleaned_message[0] == "[Leave":

                        await channel2.send(f"```\n⬅️ {clean_message}```")

                        last_line_file2 = last_line_utf8

                    elif cleaned_message[0] == "[Join":

                        await channel2.send(f"```\n➡️ {clean_message}```")

                        last_line_file2 = last_line_utf8

                    elif cleaned_message[0] == "[Announcement]":

                        await channel2.send(f"```\n📢 {clean_message}```")

                        last_line_file2 = last_line_utf8

                    elif cleaned_message[0] == "[Death":

                        await channel2.send(f"```\n☠️ {clean_message}```")

                        last_line_file2 = last_line_utf8

                    else:

                        await channel2.send(f"```\n⬛️ {clean_message}```")

                        last_line_file2 = last_line_utf8

bot.run('---')

output: return Ignoring exception in on_ready Traceback (最近的 最后调用):文件“C:\Users\N.Sarychev\Desktop\WTFbotControl ot.py”, 第 66 行,在 on_ready file.write(f" {编码密钥}: {player_ku[key]} ") 文件 "C:\Program 文件\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib ncoding

python utf-8 decode cp1251 disnake
© www.soinside.com 2019 - 2024. All rights reserved.