import os import asyncio import discord import random import difflib import json from dotenv import load_dotenv from datetime import datetime racetext="" stopart=False load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN') GUILD = os.getenv('DISCORD_GUILD') intents = discord.Intents.all() client = discord.Client(intents=intents) discord.member = True with open("data_file.json", "r") as read_file: data = json.load(read_file) with open("words.json", "r") as read_file: wordlist = json.load(read_file) with open("countr.json", "r") as read_file: countr = json.load(read_file) @client.event async def on_ready(): for guild in client.guilds: if guild.name == GUILD: break print( f'{client.user} is connected to the following guild:\n' f'{guild.name}(id: {guild.id})' ) async def arty(channel): global stopart x=1 o=1 ox=1 sent= await channel.send("+") while True: if stopart: break strr="+\n" j=x for i in range(0,20): if j >= 5 or j < 0: o*=-1 strr+=" "*j+"#\n" j+=o await sent.edit(content=strr) await asyncio.sleep(0.3) x+=ox if x >= 5 or x <= 0: ox*=-1 async def count_down(channel,count): sent= await channel.send("**"+str(count)+"**") while count>0: count-=1 await asyncio.sleep(1) await sent.edit(content="**"+str(count)+"**") def generate_txt(wordlist): size=random.randint(10,20) text="" for i in range(0,size): text+=random.choice(wordlist)+" " text+=random.choice(wordlist)+"." return text def add_noise(racetext): return (chr(173)).join(list(racetext)) @client.event async def on_message(message): channel=message.channel authorId = str(message.author.id) global data global racetext global startime global count global rank global answered if channel.name == "countr": if message.content == str(countr["count"]): countr["count"]+=1 with open("countr.json", "w") as write_file: json.dump(countr, write_file) else: await message.delete() if message.content.split()[0]=="!answer": answer = " ".join(message.content.split()[1:]) if racetext: if answer.find(chr(173))!=-1: await channel.send(f"**User:** {message.author.name} \n**...Dont copy stuff...**") elif authorId in answered: await channel.send(f"**User:** {message.author.name} \n**...Already answered...**") else: accuracy=difflib.SequenceMatcher(None,racetext,answer).ratio() speed=(len(answer)/5)/((datetime.now().timestamp()-startime)/60) await channel.send(f"**User:** {message.author.name} \n **Accuracy :** `"+str(round(accuracy*100, 1))+"%`" "\n **Speed :**`"+str(round(speed))+"wpm`" ) if not(authorId in data): data[authorId]={"score":0} score=round(speed*round(accuracy*100,-1)) data[authorId]["score"]+=score with open("data_file.json", "w") as write_file: json.dump(data, write_file) await channel.send("**Your Score:**"+str(score)+"\n" "**Total Score:**"+str(data[authorId]["score"])+"\n" "**Rank:**"+str(rank) ) answered.append(authorId) rank+=1 else: await message.channel.send("There's no Race ,type `!race` to start a race.") if message.content.split()[0]=="!race": await channel.send("type `!answer ` with the text\n**Starting after:**") await count_down(channel,10) racetext=generate_txt(wordlist) await channel.send("`"+add_noise(racetext)+"`") rank=1 answered=[] startime=datetime.now().timestamp() if message.content.split()[0]=="!endrace": racetext="" rank=1 answered=[] await channel.send("**::END::**") if message.content.split()[0]=="!score": if not(authorId in data): data[authorId]={"score":0} await channel.send(f"`{message.author.name}`**'s Score:**"+str(data[authorId]["score"])) if message.content.split()[0]=="!art": await arty(channel) client.run(TOKEN)