mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-20 18:06:08 -05:00
[V3 Everything] Package bot and write setup scripts (#964)
Ya'll are gonna hate me. * Initial modifications * Add initial setup.py * working setup py help * Modify setup file to package stuff * Move a bunch of shit and fix imports * Fix or skip tests * Must add init files for find_packages to work * Move main to scripts folder and rename * Add shebangs * Copy over translation files * WORKING PIP INSTALL * add dependency information * Hardcoded version for now, will need to figure out a better way to do this * OKAY ITS FINALLY FUCKING WORKING * Add this guy * Fix stuff * Change readme to rst * Remove double sentry opt in * Oopsie * Fix this thing * Aaaand fix test * Aaaand fix test * Fix core cog importing and default cog install path * Adjust readme * change instance name from optional to required * Ayyy let's do more dependency injection
This commit is contained in:
0
redbot/core/drivers/__init__.py
Normal file
0
redbot/core/drivers/__init__.py
Normal file
12
redbot/core/drivers/red_base.py
Normal file
12
redbot/core/drivers/red_base.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
class BaseDriver:
|
||||
def get_driver(self):
|
||||
raise NotImplementedError
|
||||
|
||||
async def get(self, identifiers: Tuple[str]):
|
||||
raise NotImplementedError
|
||||
|
||||
async def set(self, identifiers: Tuple[str], value):
|
||||
raise NotImplementedError
|
||||
49
redbot/core/drivers/red_json.py
Normal file
49
redbot/core/drivers/red_json.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from pathlib import Path
|
||||
from typing import Tuple
|
||||
|
||||
from ..json_io import JsonIO
|
||||
|
||||
from .red_base import BaseDriver
|
||||
|
||||
|
||||
class JSON(BaseDriver):
|
||||
def __init__(self, cog_name, *, data_path_override: Path=None,
|
||||
file_name_override: str="settings.json"):
|
||||
super().__init__()
|
||||
self.cog_name = cog_name
|
||||
self.file_name = file_name_override
|
||||
if data_path_override:
|
||||
self.data_path = data_path_override
|
||||
else:
|
||||
self.data_path = Path.cwd() / 'cogs' / '.data' / self.cog_name
|
||||
|
||||
self.data_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
self.data_path = self.data_path / self.file_name
|
||||
|
||||
self.jsonIO = JsonIO(self.data_path)
|
||||
|
||||
try:
|
||||
self.data = self.jsonIO._load_json()
|
||||
except FileNotFoundError:
|
||||
self.data = {}
|
||||
self.jsonIO._save_json(self.data)
|
||||
|
||||
def get_driver(self):
|
||||
return self
|
||||
|
||||
async def get(self, identifiers: Tuple[str]):
|
||||
partial = self.data
|
||||
for i in identifiers:
|
||||
partial = partial[i]
|
||||
return partial
|
||||
|
||||
async def set(self, identifiers, value):
|
||||
partial = self.data
|
||||
for i in identifiers[:-1]:
|
||||
if i not in partial:
|
||||
partial[i] = {}
|
||||
partial = partial[i]
|
||||
|
||||
partial[identifiers[-1]] = value
|
||||
await self.jsonIO._threadsafe_save_json(self.data)
|
||||
211
redbot/core/drivers/red_mongo.py
Normal file
211
redbot/core/drivers/red_mongo.py
Normal file
@@ -0,0 +1,211 @@
|
||||
import pymongo as m
|
||||
from .red_base import BaseDriver
|
||||
|
||||
|
||||
class RedMongoException(Exception):
|
||||
"""Base Red Mongo Exception class"""
|
||||
pass
|
||||
|
||||
|
||||
class MultipleMatches(RedMongoException):
|
||||
"""Raised when multiple documents match a single cog_name and
|
||||
cog_identifier pair."""
|
||||
pass
|
||||
|
||||
|
||||
class MissingCollection(RedMongoException):
|
||||
"""Raised when a collection is missing from the mongo db"""
|
||||
pass
|
||||
|
||||
|
||||
class Mongo(BaseDriver):
|
||||
def __init__(self, host, port=27017, admin_user=None, admin_pass=None,
|
||||
**kwargs):
|
||||
self.conn = m.MongoClient(host=host, port=port, **kwargs)
|
||||
|
||||
self.admin_user = admin_user
|
||||
self.admin_pass = admin_pass
|
||||
|
||||
self._db = self.conn.red
|
||||
if self.admin_user is not None and self.admin_pass is not None:
|
||||
self._db.authenticate(self.admin_user, self.admin_pass)
|
||||
|
||||
self._global = self._db.GLOBAL
|
||||
self._guild = self._db.GUILD
|
||||
self._channel = self._db.CHANNEL
|
||||
self._role = self._db.ROLE
|
||||
self._member = self._db.MEMBER
|
||||
self._user = self._db.USER
|
||||
|
||||
def get_global(self, cog_name, cog_identifier, _, key, *, default=None):
|
||||
doc = self._global.find(
|
||||
{"cog_name": cog_name, "cog_identifier": cog_identifier},
|
||||
projection=[key, ], batch_size=2)
|
||||
if doc.count() == 2:
|
||||
raise MultipleMatches("Too many matching documents at the GLOBAL"
|
||||
" level: ({}, {})".format(cog_name,
|
||||
cog_identifier))
|
||||
elif doc.count() == 1:
|
||||
return doc[0].get(key, default)
|
||||
return default
|
||||
|
||||
def get_guild(self, cog_name, cog_identifier, guild_id, key, *,
|
||||
default=None):
|
||||
doc = self._guild.find(
|
||||
{"cog_name": cog_name, "cog_identifier": cog_identifier,
|
||||
"guild_id": guild_id},
|
||||
projection=[key, ], batch_size=2)
|
||||
if doc.count() == 2:
|
||||
raise MultipleMatches("Too many matching documents at the GUILD"
|
||||
" level: ({}, {}, {})".format(
|
||||
cog_name, cog_identifier, guild_id))
|
||||
elif doc.count() == 1:
|
||||
return doc[0].get(key, default)
|
||||
return default
|
||||
|
||||
def get_channel(self, cog_name, cog_identifier, channel_id, key, *,
|
||||
default=None):
|
||||
doc = self._channel.find(
|
||||
{"cog_name": cog_name, "cog_identifier": cog_identifier,
|
||||
"channel_id": channel_id},
|
||||
projection=[key, ], batch_size=2)
|
||||
if doc.count() == 2:
|
||||
raise MultipleMatches("Too many matching documents at the CHANNEL"
|
||||
" level: ({}, {}, {})".format(
|
||||
cog_name, cog_identifier, channel_id))
|
||||
elif doc.count() == 1:
|
||||
return doc[0].get(key, default)
|
||||
return default
|
||||
|
||||
def get_role(self, cog_name, cog_identifier, role_id, key, *,
|
||||
default=None):
|
||||
doc = self._role.find(
|
||||
{"cog_name": cog_name, "cog_identifier": cog_identifier,
|
||||
"role_id": role_id},
|
||||
projection=[key, ], batch_size=2)
|
||||
if doc.count() == 2:
|
||||
raise MultipleMatches("Too many matching documents at the ROLE"
|
||||
" level: ({}, {}, {})".format(
|
||||
cog_name, cog_identifier, role_id))
|
||||
elif doc.count() == 1:
|
||||
return doc[0].get(key, default)
|
||||
return default
|
||||
|
||||
def get_member(self, cog_name, cog_identifier, user_id, guild_id, key, *,
|
||||
default=None):
|
||||
doc = self._member.find(
|
||||
{"cog_name": cog_name, "cog_identifier": cog_identifier,
|
||||
"user_id": user_id, "guild_id": guild_id},
|
||||
projection=[key, ], batch_size=2)
|
||||
if doc.count() == 2:
|
||||
raise MultipleMatches("Too many matching documents at the MEMBER"
|
||||
" level: ({}, {}, mid {}, sid {})".format(
|
||||
cog_name, cog_identifier, user_id,
|
||||
guild_id))
|
||||
elif doc.count() == 1:
|
||||
return doc[0].get(key, default)
|
||||
return default
|
||||
|
||||
def get_user(self, cog_name, cog_identifier, user_id, key, *,
|
||||
default=None):
|
||||
doc = self._user.find(
|
||||
{"cog_name": cog_name, "cog_identifier": cog_identifier,
|
||||
"user_id": user_id},
|
||||
projection=[key, ], batch_size=2)
|
||||
if doc.count() == 2:
|
||||
raise MultipleMatches("Too many matching documents at the USER"
|
||||
" level: ({}, {}, mid {})".format(
|
||||
cog_name, cog_identifier, user_id))
|
||||
elif doc.count() == 1:
|
||||
return doc[0].get(key, default)
|
||||
else:
|
||||
return default
|
||||
|
||||
def set_global(self, cog_name, cog_identifier, key, value, clear=False):
|
||||
filter = {"cog_name": cog_name, "cog_identifier": cog_identifier}
|
||||
data = {"$set": {key: value}}
|
||||
if self._global.count(filter) > 1:
|
||||
raise MultipleMatches("Too many matching documents at the GLOBAL"
|
||||
" level: ({}, {})".format(cog_name,
|
||||
cog_identifier))
|
||||
else:
|
||||
if clear:
|
||||
self._global.delete_one(filter)
|
||||
else:
|
||||
self._global.update_one(filter, data, upsert=True)
|
||||
|
||||
def set_guild(self, cog_name, cog_identifier, guild_id, key, value,
|
||||
clear=False):
|
||||
filter = {"cog_name": cog_name, "cog_identifier": cog_identifier,
|
||||
"guild_id": guild_id}
|
||||
data = {"$set": {key: value}}
|
||||
if self._guild.count(filter) > 1:
|
||||
raise MultipleMatches("Too many matching documents at the GUILD"
|
||||
" level: ({}, {}, {})".format(
|
||||
cog_name, cog_identifier, guild_id))
|
||||
else:
|
||||
if clear:
|
||||
self._guild.delete_one(filter)
|
||||
else:
|
||||
self._guild.update_one(filter, data, upsert=True)
|
||||
|
||||
def set_channel(self, cog_name, cog_identifier, channel_id, key, value,
|
||||
clear=False):
|
||||
filter = {"cog_name": cog_name, "cog_identifier": cog_identifier,
|
||||
"channel_id": channel_id}
|
||||
data = {"$set": {key: value}}
|
||||
if self._channel.count(filter) > 1:
|
||||
raise MultipleMatches("Too many matching documents at the CHANNEL"
|
||||
" level: ({}, {}, {})".format(
|
||||
cog_name, cog_identifier, channel_id))
|
||||
else:
|
||||
if clear:
|
||||
self._channel.delete_one(filter)
|
||||
else:
|
||||
self._channel.update_one(filter, data, upsert=True)
|
||||
|
||||
def set_role(self, cog_name, cog_identifier, role_id, key, value,
|
||||
clear=False):
|
||||
filter = {"cog_name": cog_name, "cog_identifier": cog_identifier,
|
||||
"role_id": role_id}
|
||||
data = {"$set": {key: value}}
|
||||
if self._role.count(filter) > 1:
|
||||
raise MultipleMatches("Too many matching documents at the ROLE"
|
||||
" level: ({}, {}, {})".format(
|
||||
cog_name, cog_identifier, role_id))
|
||||
else:
|
||||
if clear:
|
||||
self._role.delete_one(filter)
|
||||
else:
|
||||
self._role.update_one(filter, data, upsert=True)
|
||||
|
||||
def set_member(self, cog_name, cog_identifier, user_id, guild_id, key,
|
||||
value, clear=False):
|
||||
filter = {"cog_name": cog_name, "cog_identifier": cog_identifier,
|
||||
"guild_id": guild_id, "user_id": user_id}
|
||||
data = {"$set": {key: value}}
|
||||
if self._member.count(filter) > 1:
|
||||
raise MultipleMatches("Too many matching documents at the MEMBER"
|
||||
" level: ({}, {}, mid {}, sid {})".format(
|
||||
cog_name, cog_identifier, user_id,
|
||||
guild_id))
|
||||
else:
|
||||
if clear:
|
||||
self._member.delete_one(filter)
|
||||
else:
|
||||
self._member.update_one(filter, data, upsert=True)
|
||||
|
||||
def set_user(self, cog_name, cog_identifier, user_id, key, value,
|
||||
clear=False):
|
||||
filter = {"cog_name": cog_name, "cog_identifier": cog_identifier,
|
||||
"user_id": user_id}
|
||||
data = {"$set": {key: value}}
|
||||
if self._user.count(filter) > 1:
|
||||
raise MultipleMatches("Too many matching documents at the USER"
|
||||
" level: ({}, {}, mid {})".format(
|
||||
cog_name, cog_identifier, user_id))
|
||||
else:
|
||||
if clear:
|
||||
self._user.delete_one(filter)
|
||||
else:
|
||||
self._user.update_one(filter, data, upsert=True)
|
||||
Reference in New Issue
Block a user