redis connection + queue
This commit is contained in:
parent
889d15f05b
commit
11c95e93f2
20
__main__.py
20
__main__.py
@ -2,8 +2,9 @@ from toml import TomlDecodeError
|
|||||||
from discord import Bot, Intents
|
from discord import Bot, Intents
|
||||||
|
|
||||||
from config import Config
|
from config import Config
|
||||||
from logger import LoggerGetter
|
from logger import Logger
|
||||||
from cogs.misc import Greetings
|
from framework import Redis
|
||||||
|
from cogs import Greetings
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -11,20 +12,21 @@ if __name__ == "__main__":
|
|||||||
try:
|
try:
|
||||||
config = Config("config.toml")
|
config = Config("config.toml")
|
||||||
print(config)
|
print(config)
|
||||||
except KeyError as error:
|
|
||||||
print("Config/KeyError : %s" % error)
|
|
||||||
exit(1)
|
|
||||||
except TomlDecodeError as error:
|
except TomlDecodeError as error:
|
||||||
print("Config/DecodeError : %s" % error)
|
print("Config/DecodeError : %s" % error)
|
||||||
|
exit(1)
|
||||||
|
except KeyError as error:
|
||||||
|
print("Config/KeyError : %s" % error)
|
||||||
exit(2)
|
exit(2)
|
||||||
|
|
||||||
# Set Logger
|
# Set Logger
|
||||||
logger = LoggerGetter(config.logging)()
|
logger = Logger(config.logging)()
|
||||||
|
|
||||||
|
# Redis Client
|
||||||
|
redis = Redis(config.redis)
|
||||||
|
|
||||||
# Bot
|
# Bot
|
||||||
intents = Intents.default()
|
intents = Intents.default()
|
||||||
bot = Bot(intents=intents)
|
bot = Bot(intents=intents)
|
||||||
|
bot.add_cog(Greetings(bot, logger, redis))
|
||||||
bot.add_cog(Greetings(bot, logger))
|
|
||||||
|
|
||||||
bot.run(config.discord.token)
|
bot.run(config.discord.token)
|
||||||
|
1
cogs/__init__.py
Normal file
1
cogs/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
from .misc import Greetings
|
22
cogs/misc.py
22
cogs/misc.py
@ -2,15 +2,25 @@ from discord import Member, Bot, Cog, ApplicationContext
|
|||||||
from discord.commands import slash_command
|
from discord.commands import slash_command
|
||||||
from logging import Logger
|
from logging import Logger
|
||||||
|
|
||||||
|
from framework.redis import Redis
|
||||||
|
|
||||||
|
|
||||||
class Greetings(Cog):
|
class Greetings(Cog):
|
||||||
def __init__(self, bot: Bot, logger: Logger):
|
def __init__(self, bot: Bot, logger: Logger, redis: Redis):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
|
self.redis = redis
|
||||||
|
|
||||||
@slash_command(name="hello")
|
@slash_command()
|
||||||
async def hello(self, context: ApplicationContext, *, member: Member = None):
|
async def redis_set(self, context: ApplicationContext, key: str, value: str):
|
||||||
self.logger.info("Say Hello")
|
self.logger.info(f"redis set {value} at {key}")
|
||||||
|
await self.redis.set(key, value)
|
||||||
|
|
||||||
member = member or context.author
|
await context.respond(f"redis set {value} at {key}")
|
||||||
await context.send(f"Hello {member.name}~")
|
|
||||||
|
@slash_command()
|
||||||
|
async def redis_get(self, context: ApplicationContext, key: str):
|
||||||
|
self.logger.info(f"redis get {key}")
|
||||||
|
value = await self.redis.get(key)
|
||||||
|
|
||||||
|
await context.respond(f"redis get {key}: {value}")
|
||||||
|
0
cogs/music.py
Normal file
0
cogs/music.py
Normal file
14
config.py
14
config.py
@ -1,21 +1,29 @@
|
|||||||
import toml
|
import toml
|
||||||
|
|
||||||
|
|
||||||
class DiscordConfig():
|
class DiscordConfig:
|
||||||
def __init__(self, discord_config: any) -> None:
|
def __init__(self, discord_config: any) -> None:
|
||||||
self.token: str = discord_config["token"]
|
self.token: str = discord_config["token"]
|
||||||
|
|
||||||
|
|
||||||
class LoggingConfig():
|
class LoggingConfig:
|
||||||
def __init__(self, logging_config: any) -> None:
|
def __init__(self, logging_config: any) -> None:
|
||||||
self.level: str = logging_config["level"]
|
self.level: str = logging_config["level"]
|
||||||
|
|
||||||
|
|
||||||
class Config():
|
class RedisConfig:
|
||||||
|
def __init__(self, redis_config: any) -> None:
|
||||||
|
self.host: str = redis_config["host"]
|
||||||
|
self.port: int = redis_config["port"]
|
||||||
|
self.password: str = redis_config["password"]
|
||||||
|
|
||||||
|
|
||||||
|
class Config:
|
||||||
def __init__(self, config_path: str) -> None:
|
def __init__(self, config_path: str) -> None:
|
||||||
self._config = toml.load(config_path)
|
self._config = toml.load(config_path)
|
||||||
self.discord = DiscordConfig(self._config["discord"])
|
self.discord = DiscordConfig(self._config["discord"])
|
||||||
self.logging = LoggingConfig(self._config["logging"])
|
self.logging = LoggingConfig(self._config["logging"])
|
||||||
|
self.redis = RedisConfig(self._config["redis"])
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return str(self._config)
|
return str(self._config)
|
||||||
|
8
docker-compose-dev.yml
Normal file
8
docker-compose-dev.yml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
version: "3.9"
|
||||||
|
services:
|
||||||
|
redis:
|
||||||
|
container_name: redis
|
||||||
|
image: redis:alpine
|
||||||
|
command: /bin/sh -c "redis-server --requirepass dev"
|
||||||
|
ports:
|
||||||
|
- 127.0.0.1:6379:6379
|
2
entity/__init__.py
Normal file
2
entity/__init__.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
from .queue import Queue
|
||||||
|
from .entry import Entry
|
6
entity/entry.py
Normal file
6
entity/entry.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
class Entry:
|
||||||
|
def __init__(self, entry_info) -> None:
|
||||||
|
self.title: str = entry_info["title"]
|
||||||
|
self.artist: str = entry_info["artist"]
|
||||||
|
self.thumbnail: str = entry_info["thumbnail"]
|
||||||
|
self.requester: str = entry_info["requester"]
|
23
entity/queue.py
Normal file
23
entity/queue.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
from entry import Entry
|
||||||
|
|
||||||
|
|
||||||
|
class Queue:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self._queue: list[Entry] = []
|
||||||
|
self.cursor = 0
|
||||||
|
|
||||||
|
def append(self, entry: Entry) -> None:
|
||||||
|
self._queue.append(entry)
|
||||||
|
|
||||||
|
def remove(self, index: int) -> Entry | None:
|
||||||
|
if 0 < index < len(self._queue):
|
||||||
|
return self._queue.pop()
|
||||||
|
|
||||||
|
def move(self, frm: int, to: int) -> None:
|
||||||
|
if 0 < frm < len(self._queue) and 0 < to < len(self._queue) and frm != to:
|
||||||
|
self._queue.insert(to, self._queue.pop(frm))
|
||||||
|
|
||||||
|
def __getitem__(self, index: int) -> Entry | None:
|
||||||
|
if index < 0 or index > len(self._queue):
|
||||||
|
return None
|
||||||
|
return self._queue[index]
|
1
framework/__init__.py
Normal file
1
framework/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
from .redis import Redis
|
24
framework/redis.py
Normal file
24
framework/redis.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import redis.asyncio as redis
|
||||||
|
import pickle
|
||||||
|
|
||||||
|
from config import RedisConfig
|
||||||
|
|
||||||
|
|
||||||
|
class Redis:
|
||||||
|
def __init__(self, config: RedisConfig) -> None:
|
||||||
|
self.client = redis.Redis(
|
||||||
|
host=config.host,
|
||||||
|
port=config.port,
|
||||||
|
password=config.password,
|
||||||
|
auto_close_connection_pool=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def set(self, key: str, value: any) -> None:
|
||||||
|
await self.client.set(key, pickle.dumps(value))
|
||||||
|
|
||||||
|
async def get(self, key: str) -> any:
|
||||||
|
value = await self.client.get(key)
|
||||||
|
return pickle.loads(value)
|
||||||
|
|
||||||
|
async def close(self) -> None:
|
||||||
|
await self.client.close()
|
@ -1,11 +1,10 @@
|
|||||||
import logging
|
import logging
|
||||||
from logging import Logger
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from config import LoggingConfig
|
from config import LoggingConfig
|
||||||
|
|
||||||
|
|
||||||
class LoggerGetter:
|
class Logger:
|
||||||
def __init__(self, config: LoggingConfig) -> None:
|
def __init__(self, config: LoggingConfig) -> None:
|
||||||
self._logger = logging.getLogger("discord")
|
self._logger = logging.getLogger("discord")
|
||||||
self._logger.setLevel(config.level)
|
self._logger.setLevel(config.level)
|
||||||
@ -15,5 +14,5 @@ class LoggerGetter:
|
|||||||
)
|
)
|
||||||
self._logger.addHandler(handler)
|
self._logger.addHandler(handler)
|
||||||
|
|
||||||
def __call__(self) -> Logger:
|
def __call__(self):
|
||||||
return self._logger
|
return self._logger
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
py-cord==2.4.1
|
py-cord==2.4.1
|
||||||
PyNaCl==1.5.0
|
PyNaCl==1.5.0
|
||||||
toml==0.10.2
|
toml==0.10.2
|
||||||
|
redis==4.5.4
|
||||||
|
Loading…
x
Reference in New Issue
Block a user