Skip to content

Commit

Permalink
json update
Browse files Browse the repository at this point in the history
added saving data to json file
  • Loading branch information
Setlerr committed Aug 26, 2022
1 parent 77721b9 commit a18dd2a
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
./cogs.pyc
./cogs/setting.py
apikeys.py
*.pyc
*~
Expand Down
31 changes: 28 additions & 3 deletions cogs/create.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
from http import server
from venv import create
import json
import nextcord
import os.path

from pathlib import Path
from nextcord.ext import commands

def create_event(channel_id,message_id,channel_name,author_id,author_name):
if os.path.exists("./data.json") is True:
raw_data = Path("./data.json").read_text()
print(raw_data)
Current_games = json.loads(raw_data)
else:
Current_games = {}
Current_games[str(channel_id)] = {'message_id' : str(message_id),'Channel_name': str(channel_name) ,'Author': str(author_id),'Author_name': author_name, 'Players': []}
with open('data.json', 'w') as f:
json.dump(Current_games, f)
f.close

intents = nextcord.Intents.all()
intents.members = True
intents.messages = True


class Create(commands.Cog):

Expand All @@ -24,7 +40,16 @@ async def create(self, ctx, *, word: str):
await ctx.channel.send("Już jest wydarzenie na tym kanale.",delete_after=5)
break
if marker==0:
await ctx.send(content="Event\n"+ word + "\nCreated by: " + str(ctx.author.mention))
message_info = await ctx.send(content=word)
message_id = message_info.id
message_channel_id = message_info.channel.id
message_author = ctx.author.mention
channel_name = message_info.channel.name
print(message_id)
print(message_channel_id)
print(channel_name)
print(str(message_author))
create_event(str(message_channel_id),str(message_id),str(channel_name),str(message_author),str(ctx.author.name))


def setup(client):
Expand Down
32 changes: 18 additions & 14 deletions cogs/edit.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import nextcord

import json
from pathlib import Path
from nextcord.ext import commands


intents = nextcord.Intents.all()
intents.members = True
intents.messages = True

class Edit(commands.Cog):

Expand All @@ -14,17 +13,22 @@ def __init__(self,client):
#edit event by creator
@commands.command()
async def edit(self, ctx, *, word: str): #edit
messages = await ctx.channel.history(limit=200).flatten()
for event in messages:
if "Event" in event.content:
await ctx.message.delete()
text = event.content
check_position = text.find("Created by: ")
check_text = text[check_position:]
if check_text.find(str(ctx.author.mention)) > -1:
await event.edit(content="Event\n"+ word + "\nCreated by: " + str(ctx.author.mention))
else:
await ctx.channel.send("Nie możesz edytować tej wiadomości!",delete_after=5)
channel_id = ctx.channel.id
raw_data = Path("./data.json").read_text()
print(raw_data)
Current_games = json.loads(raw_data)
print("current_Games")
print(Current_games)
message_id = Current_games[str(channel_id)]["message_id"]
author_id = Current_games[str(channel_id)]["Author"]
print(message_id)
msg = await ctx.fetch_message(message_id)
print(msg)
await ctx.message.delete()
if author_id==ctx.author.mention:
await msg.edit(content=word)
else:
await ctx.channel.send("Nie możesz edytować tej wiadomości!",delete_after=5)


def setup(client):
Expand Down
36 changes: 36 additions & 0 deletions cogs/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import nextcord
import json

from pathlib import Path
from nextcord.ext import commands

intents = nextcord.Intents.all()

class Export(commands.Cog):

def __init__(self,client):
self.client=client
#Export players from json file to activity channel
@commands.command()
async def export(self,ctx):
await ctx.message.delete()
channel_id = ctx.channel.id
raw_data = Path("./data.json").read_text()
print(raw_data)
Current_games = json.loads(raw_data)
print("current_Games")
print(Current_games)
Channel_name = str(Current_games[str(channel_id)]["Channel_name"])
Author_name = str(str(Current_games[str(channel_id)]["Author_name"]))
Players= Current_games[str(channel_id)]["Players"]
List_of_players = "**Gracze zapisani:**\n"
for player in Players:
List_of_players += str(player) +"\n"
Message_content = f"**Nazwa kanału:** {Channel_name}\n\n**Autor zapisów/misji:** {Author_name}\n\n{List_of_players}\n\n"
print(Message_content)
channel = ctx.get_channel(1008094444660203540)
await channel.send(Message_content)


def setup(client):
client.add_cog(Export(client))
55 changes: 35 additions & 20 deletions cogs/register.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from queue import Empty
import nextcord

import json
from nextcord.ext import commands

from pathlib import Path
intents = nextcord.Intents.all()
intents.members = True
intents.messages = True

def has_numbers(inputString):
return any(char.isdigit() for char in inputString)
Expand All @@ -14,25 +13,41 @@ def __init__(self,client):
self.client=client
#register
@commands.command(name='re',aliases=['zapis','register','zapisz'])
async def re(self, ctx, arg: str):
if arg.find("slot") == -1:
async def re(self, ctx, arg1:str, arg2: nextcord.User = None):
print(arg2)
if arg1.find("slot") == -1:
await ctx.message.delete()
await ctx.channel.send("Nie ma takiej roli",delete_after=5)
elif arg.find("slot") > -1:
if has_numbers(arg)==True:
elif arg1.find("slot") > -1:
if has_numbers(arg1)==True:
await ctx.message.delete()
messages = await ctx.channel.history(limit=200).flatten()
for event in messages:
if "Event" in event.content:
mess = event.content
check_position = mess.find("Created by: ")
check_text = mess[:check_position]
print(check_text)
if check_text.find(str(ctx.author.name))>-1:
await ctx.channel.send("Już jesteś zapisany!",delete_after=5)
else:
mess = mess.replace(arg,("**"+str(ctx.author.name)+"**"),1)
await event.edit(content=mess)
channel_id = ctx.channel.id
raw_data = Path("./data.json").read_text()
print(raw_data)
Current_games = json.loads(raw_data)
print("current_Games")
print(Current_games)
message_id = Current_games[str(channel_id)]["message_id"]
print(message_id)
msg = await ctx.fetch_message(message_id)
mess = msg.content
author_id = Current_games[str(channel_id)]["Author"]
print(message_id)
user_to_register = ctx.author.name
if arg2 is not None:
if ctx.author.mention == author_id:
user_to_register = arg2.name
else:
await ctx.channel.send("Musisz być właścicielem zapisów, żeby kogoś wpisać!",delete_after=5)
if mess.find(str(user_to_register))>-1:
await ctx.channel.send("Już jesteś zapisany!",delete_after=5)
else:
mess = mess.replace(arg1,("**"+str(user_to_register)+"**"),1)
await msg.edit(content=mess)
Current_games[str(channel_id)]["Players"].append(str(user_to_register))
with open('data.json', 'w') as f:
json.dump(Current_games, f)

else:
await ctx.message.delete()
await ctx.channel.send("Podaj numer slota",delete_after=5)
Expand Down
40 changes: 19 additions & 21 deletions cogs/reminder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,39 @@
import nextcord
import nextcord.utils
import linecache as lc

import json
from pathlib import Path
from nextcord.ext import tasks, commands

intents = nextcord.Intents.all()
intents.members = True
intents.messages = True


class Reminder(commands.Cog):

def __init__(self,client):
self.client=client
#register
@commands.command(name='reminder',aliases=['powiadom','alert','remind','przypomnij'])
#@commands.has_role("Admin")
#@commands.has_role("MM","Admin")
@commands.has_any_role("Moderator","Admin","Technik")
async def remind(self, ctx):
await ctx.message.delete()
#if ctx.author.guild_permissions.administrator:
messages = await ctx.channel.history(limit=400).flatten()
users_in_message = []
users_to_ping = "Przypominam o misji!\n"
for event in messages:
if "Event" in event.content:
mess = event.content
mess = mess.replace("**","")
print(mess)
mess = mess.split()
for word in mess:
if nextcord.utils.get(ctx.guild.members, name=word) != None:
user = nextcord.utils.get(ctx.guild.members, name=word)
users_in_message.append(user.mention)
print(user)
for user in users_in_message:
users_to_ping += str(f"{user}")
await ctx.channel.send(content = users_to_ping)
channel_id = ctx.channel.id
raw_data = Path("./data.json").read_text()
print(raw_data)
Current_games = json.loads(raw_data)
print("current_Games")
print(Current_games)
Players = Current_games[str(channel_id)]["Players"]
for word in Players:
if nextcord.utils.get(ctx.guild.members, name=word) != None:
user = nextcord.utils.get(ctx.guild.members, name=word)
users_in_message.append(user.mention)
print(user)
for user in users_in_message:
users_to_ping += str(f"{user}")
await ctx.channel.send(content = users_to_ping)



Expand Down
60 changes: 37 additions & 23 deletions cogs/remove.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import nextcord
import json

from nextcord.ext import commands

from pathlib import Path
intents = nextcord.Intents.all()
intents.members = True
intents.messages = True
Expand All @@ -12,29 +13,42 @@ def __init__(self,client):
self.client=client
#remove
@commands.command(name='rm',aliases=['wypis','remove'])
async def rm(self, ctx,*,word: str):
async def rm(self, ctx,word: str,arg2: nextcord.User = None):
print("remove")
messages = await ctx.channel.history(limit=200).flatten()
for event in messages:
if "Event" in event.content:
check_slot = event.content
if word.find("slot") == -1:
await ctx.channel.send("Musisz nazwać slota z którego chcesz się wypisać np. slot5",delete_after=5)
break
elif check_slot.find(word+" ")>-1:
await ctx.channel.send("Taki slot już istnieje",delete_after=5)
break
elif word.find("slot")>-1:
if word.find(" ")>-1:
await ctx.channel.send("Bez spacji!",delete_after=5)
else:
text = event.content
check_position = text.find("Created by: ")
check_text = text[check_position:]
text = text[:check_position]
text = text.replace(str(ctx.author.mention),word,1) #this line is temporary
text = text.replace("**"+str(ctx.author.name)+"**",word,1)
await event.edit(content=text+check_text)
channel_id = ctx.channel.id
raw_data = Path("./data.json").read_text()
print(raw_data)
Current_games = json.loads(raw_data)
print("current_Games")
print(Current_games)
message_id = Current_games[str(channel_id)]["message_id"]
print(message_id)
msg = await ctx.fetch_message(message_id)
print(msg)
mess = msg.content
check_slot = msg.content
author_id = Current_games[str(channel_id)]["Author"]
print(message_id)
user_to_remove = ctx.author.name
if arg2 is not None:
if ctx.author.mention == author_id:
user_to_remove = arg2.name
else:
await ctx.channel.send("Musisz być właścicielem zapisów, żeby kogoś wpisać!",delete_after=5)
if word.find("slot") == -1:
await ctx.channel.send("Musisz nazwać slota z którego chcesz się wypisać np. slot5",delete_after=5)
elif check_slot.find(word+" ")>-1:
await ctx.channel.send("Taki slot już istnieje",delete_after=5)
elif word.find("slot")>-1:
if word.find(" ")>-1:
await ctx.channel.send("Bez spacji!",delete_after=5)
else:
text = msg.content
text = text.replace("**"+str(user_to_remove)+"**",word,1)
await msg.edit(content=text)
Current_games[str(channel_id)]["Players"].remove(str(user_to_remove))
with open('data.json', 'w') as f:
json.dump(Current_games, f)
await ctx.message.delete()


Expand Down
1 change: 1 addition & 0 deletions data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"990936184937119815": {"message_id": "1012782819140976725", "Channel_name": "xd", "Author": "<@!276390407565344779>", "Author_name": "Setler", "Players": ["Setler", "TheAdaamek", "Apollo", "MiszczuZPolski", "Ruku"]}}
4 changes: 1 addition & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import nextcord
import os

import json
from nextcord.ext import commands

#import token from file
from apikeys import TOKEN
from settings import logs_channel
intents = nextcord.Intents.all()
intents.members = True
intents.messages = True


def save_event(**event):
Expand Down
3 changes: 2 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
join_leave_channel = 982713233758634004
logs_channel = 989940887259840562
logs_channel = 989940887259840562
activity_channel = 1008094444660203540

0 comments on commit a18dd2a

Please sign in to comment.