Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
runarsf committed Jul 11, 2020
1 parent 83ef33d commit cbb397b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 43 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ logs
*.log
runners
*.db
*.sqlite
.vscode
secrets.json
.env
.env
*.env
12 changes: 4 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
FROM python:3

#RUN apt-get update \
# && apt-get install -y git python3 python3-pip

COPY ./src/requirements.txt /app/requirements.txt
RUN python3 -m pip install -U -r /app/requirements.txt

WORKDIR /app

CMD ["python3", "-u", "rufus.py"]
#CMD ["python3", "-u", "-m", "trace", "--trace", "./rufus.py", "|", "grep", "rufus.py"]
COPY ./src/requirements.txt requirements.txt
RUN python -m pip install -U -r requirements.txt

CMD [ "python", "-u", "rufus.py" ]
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ services:
context: .
dockerfile: Dockerfile
environment:
TZ: Europe/Oslo
DOCKER_MODE: 'true'
TZ: "${TZ:-Europe/Oslo}"
DOCKERIZED: 'true'
GIT_PYTHON_REFRESH: quiet
volumes:
- "./src:/app"
#- ./logs:/app/logs
network_mode: host
#network_mode: host
#ports:
#- 8080:8080
2 changes: 1 addition & 1 deletion src/cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ async def _leave(self, ctx):
await server.leave()

@commands.command(name='nick', aliases=['nickname', 'changenick'])
@commands.has_permissions(manage_server=True)
@commands.has_permissions(manage_guild=True)
@commands.guild_only()
async def _nickname(self, ctx, nickname, *members: discord.Member):
""" Change member(s) nickname(s).
Expand Down
12 changes: 12 additions & 0 deletions src/cogs/owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import inspect
import config as c

#from phue import Bridge
from discord.ext import commands
from importlib import reload

Expand Down Expand Up @@ -52,6 +53,17 @@ async def debug(self, ctx, *, code : str):

await ctx.send(python.format(result))

#@commands.command('hue', aliases=['lights'])
#@commands.is_owner()
#async def hue(self, ctx, *, subcommand):
# if subcommand:
# if subcommand == 'on' or subcommand == 'off':
# b = Bridge(c.data['hue_ip'])
# b.connect()
# b.get_api()
# b.set_light(1, 'bri', 254)
# await ctx.send(b.get_light(1, 'on'))

@commands.command(name='load', hidden=True, aliases=['l', 'lo'])
@commands.is_owner()
async def _load(self, ctx, *, cog: str):
Expand Down
16 changes: 15 additions & 1 deletion src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,28 @@
data = json.load(json_data_file)

prefixes = ['rufus ', 'r ']
game: str = 'in his container'
description: str = f'prefixes: {str(prefixes).strip("[]")}'
dockerStatus: bool = os.getenv('DOCKER_MODE', False)
srcDir: str = (os.path.dirname(os.path.realpath(__file__)))

owner_id: int = 177098893069254656
dev_id = [324943524132814849, 359319833680281601]

scoped_prefixes = {
'guild': [
'r ',
'rufus '
],
'direct': [
''
]
}

scoped_games = {
'default': 'with algorithms',
'docker': 'with containers'
}

rages = ['🚩', '😡', '😠', '🇮🇳', '🚫', '⛔', '🙅', '🚯', '👿', '🤐', '😱', '😳', '😭', '😢']
swears = [
[99, 117, 109, 98, 117, 98, 98, 108, 101],
Expand Down
1 change: 1 addition & 0 deletions src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ whois
BeautifulSoup4
pillow
pytz
phue
94 changes: 65 additions & 29 deletions src/rufus.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
from discord.ext import commands
from cogs.utils import rules

startup_extensions = ['cogs.owner',
'cogs.commands',
'cogs.admin',
'cogs.dev',
'cogs.osu',
'cogs.memes',
'cogs.runners',
'cogs.help'
]
COGS = ['cogs.owner',
'cogs.commands',
'cogs.admin',
'cogs.dev',
'cogs.osu',
'cogs.memes',
'cogs.runners',
'cogs.help'
]

# DEBUG, INFO, WARNING, ERROR, CRITICAL, EXCEPTION
logging.basicConfig(stream=sys.stdout,
Expand All @@ -37,32 +37,57 @@
)
log = logging.getLogger(__name__)

#def get_prefix(_bot, message):
# if not message.guild:
# return ''
# return commands.when_mentioned_or(*c.prefixes)(_bot, message)

def get_prefix(_bot, message):
if not message.guild:
return ''
return commands.when_mentioned_or(*c.prefixes)(_bot, message)
"""
Return the prefix used in the specific channel.
"""
if message.guild:
scope = 'guild'
else:
scope = 'direct'
return commands.when_mentioned_or(*c.scoped_prefixes[scope])(_bot, message)

bot = commands.Bot(command_prefix=get_prefix, description=c.description)
bot = commands.Bot(command_prefix=get_prefix,
description=c.description,
case_insensitive=False)

@bot.event
async def on_ready():
joined = []
"""
Bot is ready.
"""
joined_guilds = []
for guild in bot.guilds:
joined.append(f' - {str(guild.name)} :: {str(guild.id)}')
print()
print('Logged in as:')
print(f'{bot.user.name} - {bot.user.id}')
print(f'Version: {discord.__version__}')
print(f'\nBot currently running on {len(bot.guilds)} server(s):')
print('-' * len(max(joined, key=len)))
print('\n'.join(joined))
print('-' * len(max(joined, key=len)))
print()

await bot.change_presence(status=discord.Status.online, activity=discord.Game(c.game))
#joined_guilds.append(f' - {str(guild.name)}'.ljust(6 + len(max([guild.name for guild in bot.guilds]))) + str(guild.id))
joined_guilds.append(f' - {str(guild.id).ljust(20)} {guild.name}')

ready_message = [
'Logged in as:',
f'{bot.user.name}: {bot.user.id}',
f'Discord Version: {discord.__version__}',
f'\nBot currently running on {len(bot.guilds)} server(s):',
'\n'.join(joined_guilds)
]
#dashes = '-' * len(max(ready_message, key=len))
#ready_message = [f'\n{dashes}'] + ready_message + [dashes]
log.info('\n'.join(ready_message))

if os.getenv('DOCKERIZED'):
game_scope = 'docker'
else:
game_scope = 'default'
await bot.change_presence(status=discord.Status.online, activity=discord.Game(c.scoped_games[game_scope]))

@bot.event
async def on_message(message):
"""
When a message is sent in a channel the bot is a member of.
"""
if message.author == bot.user or message.author.bot:
return
try:
Expand All @@ -86,8 +111,18 @@ async def on_message(message):
log.info(message)
await bot.process_commands(message)

@bot.event
async def on_error(error):
"""
Whan an error occurs.
"""
log.error('An unexpected error occurred: %s', error)

@bot.event
async def on_command_error(self, exception):
"""
When a command fails.
"""
log.info(exception)
if isinstance(exception, commands.errors.MissingPermissions):
exception = f'Sorry {self.message.author.name}, you don\'t have permissions to do that!'
Expand All @@ -99,7 +134,7 @@ async def on_command_error(self, exception):

error_embed = discord.Embed(title='',
timestamp=datetime.datetime.utcnow(),
description=f'```python\n{exception}```',
description=f'```css\n{exception}```',
color=discord.Color.from_rgb(200, 0, 0))
error_embed.set_author(name='Woops!',
icon_url=str(self.message.author.avatar_url))
Expand All @@ -122,7 +157,8 @@ def check_reaction(reaction, user):


if __name__ == '__main__':
for extension in startup_extensions:
bot.load_extension(extension)
for cog in COGS:
bot.load_extension(cog)

log.debug('Starting bot.')
bot.run(c.data["botToken"], bot=True, reconnect=True)

0 comments on commit cbb397b

Please sign in to comment.