first commit
This commit is contained in:
commit
889d15f05b
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
__pycache__
|
||||||
|
config.toml
|
16
.vscode/launch.json
vendored
Normal file
16
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Python: Current File",
|
||||||
|
"type": "python",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}",
|
||||||
|
"console": "integratedTerminal",
|
||||||
|
"justMyCode": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"python.formatting.provider": "black"
|
||||||
|
}
|
30
__main__.py
Normal file
30
__main__.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
from toml import TomlDecodeError
|
||||||
|
from discord import Bot, Intents
|
||||||
|
|
||||||
|
from config import Config
|
||||||
|
from logger import LoggerGetter
|
||||||
|
from cogs.misc import Greetings
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Read Config
|
||||||
|
try:
|
||||||
|
config = Config("config.toml")
|
||||||
|
print(config)
|
||||||
|
except KeyError as error:
|
||||||
|
print("Config/KeyError : %s" % error)
|
||||||
|
exit(1)
|
||||||
|
except TomlDecodeError as error:
|
||||||
|
print("Config/DecodeError : %s" % error)
|
||||||
|
exit(2)
|
||||||
|
|
||||||
|
# Set Logger
|
||||||
|
logger = LoggerGetter(config.logging)()
|
||||||
|
|
||||||
|
# Bot
|
||||||
|
intents = Intents.default()
|
||||||
|
bot = Bot(intents=intents)
|
||||||
|
|
||||||
|
bot.add_cog(Greetings(bot, logger))
|
||||||
|
|
||||||
|
bot.run(config.discord.token)
|
16
cogs/misc.py
Normal file
16
cogs/misc.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from discord import Member, Bot, Cog, ApplicationContext
|
||||||
|
from discord.commands import slash_command
|
||||||
|
from logging import Logger
|
||||||
|
|
||||||
|
|
||||||
|
class Greetings(Cog):
|
||||||
|
def __init__(self, bot: Bot, logger: Logger):
|
||||||
|
self.bot = bot
|
||||||
|
self.logger = logger
|
||||||
|
|
||||||
|
@slash_command(name="hello")
|
||||||
|
async def hello(self, context: ApplicationContext, *, member: Member = None):
|
||||||
|
self.logger.info("Say Hello")
|
||||||
|
|
||||||
|
member = member or context.author
|
||||||
|
await context.send(f"Hello {member.name}~")
|
21
config.py
Normal file
21
config.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import toml
|
||||||
|
|
||||||
|
|
||||||
|
class DiscordConfig():
|
||||||
|
def __init__(self, discord_config: any) -> None:
|
||||||
|
self.token: str = discord_config["token"]
|
||||||
|
|
||||||
|
|
||||||
|
class LoggingConfig():
|
||||||
|
def __init__(self, logging_config: any) -> None:
|
||||||
|
self.level: str = logging_config["level"]
|
||||||
|
|
||||||
|
|
||||||
|
class Config():
|
||||||
|
def __init__(self, config_path: str) -> None:
|
||||||
|
self._config = toml.load(config_path)
|
||||||
|
self.discord = DiscordConfig(self._config["discord"])
|
||||||
|
self.logging = LoggingConfig(self._config["logging"])
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return str(self._config)
|
19
logger.py
Normal file
19
logger.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import logging
|
||||||
|
from logging import Logger
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from config import LoggingConfig
|
||||||
|
|
||||||
|
|
||||||
|
class LoggerGetter:
|
||||||
|
def __init__(self, config: LoggingConfig) -> None:
|
||||||
|
self._logger = logging.getLogger("discord")
|
||||||
|
self._logger.setLevel(config.level)
|
||||||
|
handler = logging.StreamHandler(sys.stdout)
|
||||||
|
handler.setFormatter(
|
||||||
|
logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s")
|
||||||
|
)
|
||||||
|
self._logger.addHandler(handler)
|
||||||
|
|
||||||
|
def __call__(self) -> Logger:
|
||||||
|
return self._logger
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
py-cord==2.4.1
|
||||||
|
PyNaCl==1.5.0
|
||||||
|
toml==0.10.2
|
Loading…
x
Reference in New Issue
Block a user