Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@

# import
__pycache__

*.egg-info
# folders
_img_dices

# Pycharm
.idea

# env
.env
.env

# test
.pytest_cache
*.html
.env
6 changes: 4 additions & 2 deletions Dockerfile.armv7l
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
FROM arm32v7/python:3.10-buster as arch_armv7l
RUN useradd -ms /bin/bash bot
USER bot
ENV DISCORD_TOKEN=${DISCORD_TOKEN}
WORKDIR /ramoloss
COPY . .
RUN apt-get update && apt-get install libatlas-base-dev -y \
&& pip install --no-cache-dir -r requirements.txt
RUN apt-get update && apt-get install \
libatlas-base-dev -y && pip install --no-cache-dir -e /bot
CMD [ "python", "main.py"]

5 changes: 3 additions & 2 deletions Dockerfile.x86_64
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM python:3.10-buster as x86_64
RUN useradd -ms /bin/bash bot
USER bot
ENV DISCORD_TOKEN=${DISCORD_TOKEN}
WORKDIR /ramoloss
COPY . .
RUN apt-get update && pip install --no-cache-dir \
-r requirements.txt
RUN apt-get update && pip install --no-cache-dir -e bot/
CMD [ "python", "main.py"]

46 changes: 27 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ SHELL = /bin/bash
NAME ?= ramoloss
ARCH ?= $(shell uname -m)
VERSION ?= $(shell git describe --tag --abbrev=0)
SERVICES := bot db log

# docker-compose
DC := $(shell type -p docker-compose)
DC_BUILD_ARGS := --pull --force-rm
DC_RUN_ARGS := -d --no-build
DC_FILE := docker-compose.yml
DC_CONFIG_ARGS := -q
PLATFORM ?= linux/amd64

# docker
#REGISTRY ?= docker.io
REGISTRY ?= ghcr.io
REPOSITORY ?= ramoloss
REGISTRY_USERNAME ?= toto

# image
IMAGE_bot=${NAME}-bot:${VERSION}
IMAGE_REGISTRY_bot=${REGISTRY}/${REGISTRY_USERNAME}/${IMAGE_bot}

IMAGES = $(foreach srv, $(SERVICES), ${NAME}-${srv}:${VERSION})
IMAGES_REGISTRY=$(foreach im, $(IMAGE), ${REGISTRY}/${REGISTRY_USERNAME}/${im})

export


all:
@echo "Usage: NAME=ramoloss make deploy | build | \
@echo "Usage: VERSION=latest make deploy | build | \
up | down | test | check | push | pull "


Expand All @@ -33,29 +35,26 @@ check-var-%:
@: $(if $(value $*),,$(error $* is undefined))
@echo ${$*}

# for see configs use: `make DC_CONFIG_ARGS="" check-config`
check-config:
${DC} -f ${DC_FILE} config

check-config-quiet:
${DC} -f ${DC_FILE} config -q
${DC} -f ${DC_FILE} config ${DC_CONFIG_ARGS}

# build all or one service
build: check-config-quiet
echo ${VERSION}
build: check-config
${DC} -f ${DC_FILE} build ${DC_BUILD_ARGS}

build-%:
@echo "# start $*"
${DC} -f ${DC_FILE} build ${DC_BUILD_ARGS} $*

# up all or one service
up: check-config-quiet
up: check-config
@if [ -z "${DISCORD_TOKEN}" ] ; \
then echo "ERROR: DISCORD_TOKEN \
not defined" ; exit 1 ; fi
${DC} -f ${DC_FILE} up ${DC_RUN_ARGS}

up-%: check-config-quiet
up-%: check-config
${DC} -f ${DC_FILE} up ${DC_RUN_ARGS} $*

# down all or one service
Expand All @@ -65,27 +64,36 @@ down:
down-%:
${DC} -f ${DC_FILE} down $*

# test
test: test-container
# test container and app
test: test-container test-bot
test-%:
@echo "# test $*"
bash tests/test-$*.sh

# push
pytest:
docker exec


# push
push: push-bot

push-%:
@if [ -z "${REGISTRY}" -a -z "${REGISTRY_USERNAME}" ] ; \
then echo "ERROR: REGISTRY and REGISTRY_USERNAME \
not defined" ; exit 1 ; fi
docker tag ${IMAGE_$*} ${IMAGE_REGISTRY_$*}
docker push ${IMAGE_REGISTRY_$*}
docker tag ${NAME}-$*:${VERSION} ${REGISTRY}/${REGISTRY_LOGIN}/${NAME}-$*:${VERSION}
docker push ${REGISTRY}/${REGISTRY_LOGIN}/${NAME}-$*:${VERSION}

pull: pull-bot

pull-%:
@if [ -n "${REGISTRY_TOKEN}" -a -n "${REGISTRY_LOGIN}" ] ;\
then echo ${REGISTRY_TOKEN} | docker login ${REGISTRY} \
--username ${REGISTRY_LOGIN} --password-stdin ; fi
docker pull ${REGISTRY}/${REGISTRY_LOGIN}/${NAME}-$*:latest
docker pull ${REGISTRY}/${REGISTRY_LOGIN}/${NAME}-$*:${VERSION}
docker tag ${REGISTRY}/${REGISTRY_LOGIN}/${NAME}-$*:${VERSION} ${NAME}-$*:${VERSION}


deploy: VERSION=latest
deploy: pull up

4 changes: 3 additions & 1 deletion bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from ._bot import Ramoloss
from ._bot import Ramoloss

__all__ = ['Ramoloss']
14 changes: 9 additions & 5 deletions bot/_bot.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import os
from discord.ext.commands import Bot


class Ramoloss(Bot):
def __init__(self, config, token):
def __init__(self, config, token, **kwargs):
self.config = config
self.discord_token = token
os.environ["command_prefix"] = self.config["command_prefix"]
super().__init__(
command_prefix=self.config["command_prefix"],
description=self.config["description"]
)
description=self.config["description"],
**kwargs)

for extension in self.config["extensions"]:
self.load_extension(extension)
self.load_extension('cogs.' + extension)

async def on_ready(self):
print(f'Logged in as {self.user} with extensions: \n{" ".join(self.extensions).replace("cogs.", "")}')
print((f'Logged in as {self.user} with extensions:'
f'\n{" ".join(self.extensions).replace("cogs.", "")}'))

def run(self, *args, **kwargs):
super().run(self.discord_token, reconnect=True)
67 changes: 67 additions & 0 deletions bot/cogs/dev/owner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from discord.ext import commands


class Owner(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.path = 'cogs.'

@commands.command(name="load", hidden=True)
@commands.has_permissions()
@commands.is_owner()
async def load(self, ctx, *, cog: str = None):
cog = self.parse_args(cog)
for module in cog:
self.bot.load_extension(self.path + module)
await ctx.send("**`SUCCESS`**")

@commands.command(name="unload", hidden=True)
@commands.has_permissions()
@commands.is_owner()
async def unload(self, ctx, *, cog: str = None):
cog = self.parse_args(cog)
for module in cog:
self.bot.unload_extension(self.path + module)
await ctx.send("**`SUCCESS`**")

@commands.command(name="reload", hidden=True)
@commands.has_permissions()
@commands.is_owner()
async def reload(self, ctx, *, cog: str = None):
cog = self.parse_args(cog)
for module in cog:
self.bot.unload_extension(self.path + module)
self.bot.load_extension(self.path + module)
await ctx.send("**`SUCCESS`**")

def parse_args(self, cog):
if cog is None:
cog = [cg for cg in
self.bot.config["extensions"]
if 'user' in cg]
return cog

if isinstance(cog, str):
if ' ' in cog:
raise ValueError('One module at a time!')
if cog in ('owner', 'settings'):
cog = 'dev.' + cog
else:
if 'user' not in cog:
cog = 'user.' + cog
cog = [cog]
return cog

@reload.error
@load.error
@unload.error
async def error(self, ctx, error):
if isinstance(error, (commands.NotOwner,
commands.errors.CommandInvokeError)):
await ctx.send(f"```ERROR: {error.__cause__}```")
else:
print(error)


def setup(bot):
bot.add_cog(Owner(bot))
2 changes: 0 additions & 2 deletions cogs/settings.py → bot/cogs/dev/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def __init__(self, bot):
async def say_hello(self, ctx):
await ctx.channel.send(f'Hello {ctx.author.name}')


@commands.has_permissions(administrator=True)
@commands.command(name='set_prefix')
async def set_prefix(self, ctx, *, new_prefix: str):
Expand All @@ -22,6 +21,5 @@ async def pref_error(self, ctx, error):
await ctx.send(error)



def setup(bot):
bot.add_cog(Settings(bot))
1 change: 0 additions & 1 deletion cogs/dice.py → bot/cogs/user/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def _random_de(low=1, high=6, size=1):
size = int(size)
return np.random.randint(low=low, high=high + 1, size=size)


def _operator(self, values):
if len(values) < 3:
return values
Expand Down
1 change: 1 addition & 0 deletions cogs/john.py → bot/cogs/user/john.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ def is_john(message):
return True
return False


def setup(bot):
bot.add_cog(John(bot))
File renamed without changes.
Loading