Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Creadeyh committed May 3, 2023
2 parents 85f8ca9 + c66d677 commit 630bfa9
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 96 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Folders
.vscode/
venv/

# Files
extensions/__pycache__
Expand Down
81 changes: 5 additions & 76 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import json
import asyncio
import os
from datetime import datetime

#region Inits

Expand Down Expand Up @@ -40,7 +40,7 @@
@pycord_bot.event
async def on_ready():
await pycord_bot.change_presence(activity=pycord.Game("Egg Inc with Wall-Egg | /help"))
print("Bot is ready")
print(f"{datetime.now().isoformat()} Bot is ready")

async def reload_extensions():
bot.reload("extensions.commands", None, pycord_bot, db_connection)
Expand All @@ -50,10 +50,6 @@ async def reload_extensions():
# FYI: Event is always fired for every guild at bot startup
@bot.event
async def on_guild_create(guild: interactions.Guild):

# TODO remove after 2.0
if utils.read_config("BOT_VERSION") in ["1.3.7", "1.3.7.1"]:
return

if int(guild.id) not in db_connection.get_all_guild_ids():
# New guild registration
Expand Down Expand Up @@ -172,80 +168,13 @@ async def remove_from_server(ctx, id):
async def update_data_version(ctx: pycord_commands.Context):
with open("config.json", "r") as f:
config = json.load(f)

# Pre 1.3.7
if "BOT_VERSION" not in config.keys():

# Added database connection
config["DB_HOSTNAME"] = "localhost"
config["DB_PORT"] = 27017
config["DB_NAME"] = "test_database"

# Added version for update checks
config["BOT_VERSION"] = "1.3.7"

with open("config.json", "w") as f:
json.dump(config, f, indent=4)
await ctx.send("Successfully updated data to 1.3.7 :white_check_mark:")

elif config["BOT_VERSION"] == "1.3.7":
config.pop("DB_HOSTNAME")
config.pop("DB_PORT")
config["DB_STRING"] = "mongodb://localhost:27017"
config["BOT_VERSION"] = "1.3.7.1"

with open("config.json", "w") as f:
json.dump(config, f, indent=4)
await ctx.send("Successfully updated data to 1.3.7.1 :white_check_mark:")

elif config["BOT_VERSION"] == "1.3.7.1":
config["BOT_VERSION"] = "2.0.0"

# Removed embed support
for guild_id in config["guilds"].keys():
config["guilds"][guild_id].pop("USE_EMBEDS", None)

# Transfer guild config to DB
for guild_id, config_dic in config["guilds"].items():
insert = config_dic.copy()
insert["guild_id"] = int(guild_id)
db_connection.guild_config.insert_one(insert)
config.pop("guilds", None)
if config["BOT_VERSION"] == "2.0.0":
config["BOT_VERSION"] = "2.1.0"

with open("config.json", "w") as f:
json.dump(config, f, indent=4)

# Transfer data files from JSON to DB
with open("data/alt_index.json", "r") as f:
alt_index = json.load(f)
db_connection.alt_index.insert_one({
"guild_id": db_connection.get_all_guild_ids()[0],
"data": alt_index
})
os.remove("data/alt_index.json")
with open("data/running_coops.json", "r") as f:
running_coops = json.load(f)
for contract_id, data in running_coops.items():
insert = {
"guild_id": db_connection.get_all_guild_ids()[0],
"contract_id": contract_id
}
insert.update(data.copy())
db_connection.running_coops.insert_one(insert)
os.remove("data/running_coops.json")
with open("data/participation_archive.json", "r") as f:
participation_archive = json.load(f)
for contract_id, data in participation_archive.items():
insert = {
"guild_id": db_connection.get_all_guild_ids()[0],
"contract_id": contract_id
}
insert["data"] = data.copy()
db_connection.participation_archive.insert_one(insert)
os.remove("data/participation_archive.json")

await ctx.send("Successfully updated data to 2.0.0 :white_check_mark:")

await ctx.send("Successfully updated data to 2.1.0 :white_check_mark:")
else:
await ctx.send("No data update is needed")

Expand Down
15 changes: 15 additions & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## v2.1.0

**New features:**

- Added a grade option when creating a coop, so your teammates know which grade is the coop in

**Changes:**

- Because of the new possibility to replay leggacy contracts for points in EggInc, you can now remove yourself from the Already done list at any point, even if you have completed it in an earlier instance

**Backend changes:**

- Updated discord-py-interactions from 4.3.1 to 4.4.0
- Added requirements.txt for dependency listing

## v2.0.1

**Changes:**
Expand Down
1 change: 1 addition & 0 deletions doc/db structures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ running_coops collection, document structure (1 doc per contract):
{
"code" : [coop-code],
"creator" : [discord-id],
"grade" : "AAA"/"AA"/"A"/"B"/"C",
"channel_id": [channel-id],
"message_id": [message-id],
"locked": true/false,
Expand Down
7 changes: 3 additions & 4 deletions extensions/checks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import discord as pycord
import interactions
from interactions import CommandContext
from interactions import CommandContext, User

import extensions.db_connection as db

Expand Down Expand Up @@ -65,8 +64,8 @@ def check_context_menu_coop_message(db_connection: db.DatabaseConnection, guild_
#region Permissions checks

async def check_is_owner(ctx: CommandContext):
info = await ctx.client.get_current_bot_information()
return int(ctx.author.user.id) == int(info["owner"]["id"])
owner: User = ctx.client.me.owner
return int(ctx.author.user.id) == int(owner.id)

def check_is_admin(ctx: CommandContext):
# 8 is the administrator permission
Expand Down
10 changes: 0 additions & 10 deletions extensions/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,6 @@ async def contract_already_done_event(self, ctx: ComponentContext):

if author_id in contract_dic["already_done"]:
# Removes the player from already done

archive = self.db_connection.get_contract_archive(int(interac_guild.id), contract_id)
for date, occurrence in archive["data"].items():
if (
date != contract_dic["date"] and
str(author_id) in occurrence["participation"].keys() and
occurrence["participation"][str(author_id)] != ParticipationEnum.NO.value
):
await ctx_send.send("I know from a trusted source you have done this contract :smile:", ephemeral=True)
return

# Updates DB
self.db_connection.add_member_remaining(int(interac_guild.id), contract_id, author_id)
Expand Down
22 changes: 19 additions & 3 deletions extensions/coop.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from interactions.ext.wait_for import *

import extensions.db_connection as db, extensions.checks as checks, extensions.utils as utils
from extensions.enums import ParticipationEnum, CoopStatusEnum
from extensions.enums import ParticipationEnum, CoopStatusEnum, CoopGradeEnum

import uuid

Expand All @@ -31,16 +31,30 @@ def __init__(self, bot, pycord_bot, db_connection):
type=interactions.OptionType.STRING,
required=True
),
interactions.Option(
name="grade",
description="The grade of the coop (i.e. your grade as coop creator)",
type=interactions.OptionType.STRING,
required=True,
choices=[
interactions.Choice(name=CoopGradeEnum.AAA.value, value=CoopGradeEnum.AAA.value),
interactions.Choice(name=CoopGradeEnum.AA.value, value=CoopGradeEnum.AA.value),
interactions.Choice(name=CoopGradeEnum.A.value, value=CoopGradeEnum.A.value),
interactions.Choice(name=CoopGradeEnum.B.value, value=CoopGradeEnum.B.value),
interactions.Choice(name=CoopGradeEnum.C.value, value=CoopGradeEnum.C.value)
]
),
interactions.Option(
name="locked",
description="Whether or not the coop is locked at creation. Prevents people from joining",
type=interactions.OptionType.BOOLEAN,
required=False
)
])
async def add_coop(self, ctx: CommandContext, coop_code: str, locked: bool=False):
async def add_coop(self, ctx: CommandContext, coop_code: str, grade: str, locked: bool=False):
await ctx.defer(ephemeral=True)

grade = CoopGradeEnum(grade)
interac_guild = await ctx.get_guild()

if not (contract_id := checks.check_contract_channel(self.db_connection, int(interac_guild.id), int(ctx.channel_id))):
Expand Down Expand Up @@ -81,6 +95,7 @@ async def add_coop(self, ctx: CommandContext, coop_code: str, locked: bool=False
})
to_pin = await coop_channel.send(":x: Do not share the coop code with anyone outside of this coop\n" +
":x: Do not make the coop public unless told by a coop organizer\n\n" +
f"Coop grade: **{grade.value}**\n" +
f"Coop code: `{coop_code}`"
)
await to_pin.pin()
Expand All @@ -96,7 +111,8 @@ async def add_coop(self, ctx: CommandContext, coop_code: str, locked: bool=False
coop_code,
ctx_author.id,
coop_channel.id,
locked
locked,
grade
)
self.db_connection.remove_member_remaining(int(interac_guild.id), contract_id, ctx_author.id)

Expand Down
4 changes: 3 additions & 1 deletion extensions/db_connection.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure

from extensions.enums import ParticipationEnum, CoopStatusEnum
from extensions.enums import ParticipationEnum, CoopStatusEnum, CoopGradeEnum

from typing import *

Expand Down Expand Up @@ -257,11 +257,13 @@ def create_coop_record(
creator_id: int,
channel_id: int,
locked: bool,
grade: CoopGradeEnum,
message_id: int = None
):
new_dic = {
"code": coop_code,
"creator": creator_id,
"grade": grade.value,
"channel_id": channel_id,
"message_id": message_id,
"locked": locked,
Expand Down
13 changes: 12 additions & 1 deletion extensions/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,15 @@ def _generate_next_value_(name, start, count, last_values):

RUNNING = False
COMPLETED = auto()
FAILED = auto()
FAILED = auto()

class CoopGradeEnum(Enum):

def _generate_next_value_(name, start, count, last_values):
return name.upper()

AAA = auto()
AA = auto()
A = auto()
B = auto()
C = auto()
2 changes: 1 addition & 1 deletion extensions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async def generate_coop_message_content_component(

content = "> -------------------------\n"
if not is_failed:
content = content + f"> **Coop {coop_nb} - {len(other_members_mentions)+1}/{contract_size}{' FULL' if (len(other_members_mentions)+1) == contract_size else ''}**\n> \n"
content = content + f"> **Coop {coop_nb} ({coop_dic['grade']}) - {len(other_members_mentions)+1}/{contract_size}{' FULL' if (len(other_members_mentions)+1) == contract_size else ''}**\n> \n"
content = content + f"> **Members:**\n> - {creator_mention} (Creator)\n"
for mention in other_members_mentions:
content = content + f"> - {mention}\n"
Expand Down
Binary file added requirements.txt
Binary file not shown.

0 comments on commit 630bfa9

Please sign in to comment.