This commit is contained in:
ian 2024-03-12 15:59:59 -06:00
parent 82531f9645
commit ad72aeb8ce
4 changed files with 11 additions and 24 deletions

View File

@ -10,7 +10,7 @@ services:
- DB_HOST=mongodb - DB_HOST=mongodb
- DB_USERNAME=${DB_USERNAME} - DB_USERNAME=${DB_USERNAME}
- DB_PASSWORD=${DB_PASSWORD} - DB_PASSWORD=${DB_PASSWORD}
mongodb: mongodb:
image: mongo image: mongo
restart: always restart: always

View File

@ -1,21 +1,10 @@
class storage: class storage:
def __init__(self, serverid, db): def __init__(self, serverid, db):
self.serverdb = db[str(serverid)] self.serverdb = db[str(serverid)]
self.defaultdb = db["default"]
def db(self, module, key, value=None): def db(self, module, key, value=None):
r = self.serverdb.find_one({"module": module})[key] try : r = self.serverdb.find_one({"module": module}).get(key)
except AttributeError: r = None
if value is not None: if value is not None:
self.serverdb.update_one({"module": module}, {"$set": {key: value}}) self.serverdb.update_one({"module": module}, {"$set": {key: value}}, upsert=True)
return r return r
def update_guild(self):
for doc in self.defaultdb.find():
existing_doc = self.serverdb.find_one({"module": doc["module"]})
if existing_doc:
for key, value in doc.items():
if key not in existing_doc:
existing_doc[key] = value
self.serverdb.replace_one({"module": doc["module"]}, existing_doc)
else:
self.serverdb.insert_one(doc)

View File

@ -27,11 +27,10 @@ async def on_ready():
print("Logged in as") print("Logged in as")
print(bot.user.name) print(bot.user.name)
print(bot.user.id) print(bot.user.id)
print("------") # logging.info(f"Logged in as {bot.user} (ID: {bot.user.id})")
logging.info(f"Logged in as {bot.user} (ID: {bot.user.id})") # for guild in bot.guilds:
for guild in bot.guilds: # logging.info(f"Added {guild.name} (ID: {guild.id})")
logging.info(f"Added {guild.name} (ID: {guild.id})") # storage(guild.id, bot.db).update_guild()
storage(guild.id, bot.db).update_guild()
bot.run(env["BOT_TOKEN"]) bot.run(env["BOT_TOKEN"])

View File

@ -8,8 +8,7 @@ class Counting(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_message(self, message): async def on_message(self, message):
db = storage(message.guild.id, self.bot.db).db db = storage(message.guild.id, self.bot.db).db
count = int(db('counting', 'count')) count = int(db('counting', 'count') or 0)
print(count, message.content[0])
if message.author.bot or message.channel.id != db('counting', 'channel') or not message.content.isdigit(): if message.author.bot or message.channel.id != db('counting', 'channel') or not message.content.isdigit():
return return
@ -30,7 +29,7 @@ class Counting(commands.Cog):
async def channel(self, ctx): async def channel(self, ctx):
db = storage(ctx.guild.id, self.bot.db).db db = storage(ctx.guild.id, self.bot.db).db
db('counting', 'channel', ctx.channel.id) db('counting', 'channel', ctx.channel.id)
await ctx.send("counting channel set") await ctx.send(f"counting channel set to {db('counting', 'channel')}")