Text Datei auslesen und Wert in String schreiben
Verfasst: Montag 22. Februar 2021, 16:20
Hey, ich schreibe gerade einen Discord Chatbot welcher die Nachrichten eines Benutzers zählen soll. Dabei wird bei der ersten Nachricht ein Textfile erstellt in welchem eine 1 steht. Wenn das Textfile eines Users bereits erstellt wurde und er eine neue Nachricht versendet soll sein Textfile ausgelesen werden und dann ein wert_ausgelesen = wert_ausgelesen + 1 rechnen. Dabei bekomme ich Folgende Fehlermeldung:
File "C:\Users\Jannik\Desktop\Kinnaj Bot\!levels.py", line 39, in on_message
cnt = cnt+1
TypeError: can only concatenate str (not "int") to str
Kann mir da Jemand helfen? Hier ist mein bisheriger Code, danke im Vorraus.
Liebe Grüße
File "C:\Users\Jannik\Desktop\Kinnaj Bot\!levels.py", line 39, in on_message
cnt = cnt+1
TypeError: can only concatenate str (not "int") to str
Kann mir da Jemand helfen? Hier ist mein bisheriger Code, danke im Vorraus.
Code: Alles auswählen
import os
import discord
import random
from discord import guild
from discord.utils import get
import os
import time
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
from pathlib import Path
#bot festlegen
class MyClient(discord.Client):
async def on_ready(self):
print('Levelsbot -> ', self.user)
#levelcheck
async def on_message(self, message):
if message.author == self.user:
return
#file festlegung
fileName = Path(str(message.author) + ".txt")
fileObj = Path(fileName)
if fileObj.is_file():
#in file zählen
text_file = open(str(message.author) + ".txt", "r+")
print("Datei von " + str(message.author) + " gefunden!")
cnt = (text_file.read())
#ausgabe aktueller wert
text_file.close()
#altes dokument leeren
open(str(message.author) + ".txt", 'w').close()
# + 1
cnt = cnt+1
#neu beschreiben
text_file = open(str(message.author) + ".txt", "r+")
text_file.write(cnt)
text_file.close()
else:
#file erstellen
text_file = open(str(message.author) + ".txt", "w")
print("Erstelle Datei für " + str(message.author))
text_file.write("1")
text_file.close()
#ownstats
if message.content == "/stats":
text_file = open(str(message.author) + ".txt", "r+")
print("Stats von " + str(message.author) + " gesendet!")
look = (text_file.read())
await message.channel.send(str(message.author) + " hat " + look + " 💬 auf diesem Server versendet.")
text_file.close()
#offstats
if message.content == "/stats.off":
print("Deaktiviere Stats für " + str(message.author))
await message.channel.send("Stats deaktiviert für " + str(message.author))
#leeren
open(str(message.author) + ".txt", 'w').close()
text_file = open(str(message.author) + ".txt", "r+")
text_file.write("---")
text_file.close()
#statson
if message.content == "/stats.on":
print("Aktiviere Stats für " + str(message.author))
await message.channel.send("Stats aktiviert für " + str(message.author))
#leeren
open(str(message.author) + ".txt", 'w').close()
text_file = open(str(message.author) + ".txt", "r+")
text_file.write("1")
text_file.close()
if message.content == "/stats.help":
test = "------------------------"
embed = discord.Embed(title="Serverstats Hilfe", colour=0xfbff00)
embed.add_field(name="/stats.on -> Aktiviert die Statistiken neu", value=test, inline=False)
embed.add_field(name="/stats.off -> Deaktiviert die Statistiken", value=test, inline=False)
embed.add_field(name="/stats -> Zeigt den Aktuellen Wert an", value=test, inline=False)
await message.channel.send(embed=embed)
client = MyClient()
client.run("bot-token") #Bot ID