Reputation: 1
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 (most recent call last): File "C:\Users\N.Sarychev\Desktop\WTFbotControl\bot.py", line 66, in on_ready file.write(f"\n{encoded_key}: {player_ku[key]}\n") File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\encodings\cp1251.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\U000f000f' in position 15: character maps to
unknown character: (this is a don't starve together emoji)
string look like this: [02:41:27]: [Say] (KU_nAt72Ax2) 󰀏трэш󰀏:
I need to ignore this character.
The bot continues to be online, but the on_ready()
function stops working
Upvotes: 0
Views: 56