Skip to content

Commit c3443d3

Browse files
committed
Add whitelist command
1 parent 6d9eb1d commit c3443d3

19 files changed

+69
-39
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Command | Alias | Usage | Description
8282
.macro | .m | .macro <macro_name> | Executes macro from "macros" directory
8383
.interactive | .i | .interactive | Enables interactive mode where all messages are sent to terminal
8484
.exec | .e | .exec <command...> | Execute single command
85+
.whitelist | - | .whitelist add/remove <user_tag> | Add/Remove user to/from whitelist. **Bot owner can execute all commands without being on the whitelist**
8586

8687
### Shortcuts
8788
Shortcut | Description

bashbot/bot.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from bashbot.command.select import SelectCommand
1818
from bashbot.command.submit import SubmitCommand
1919
from bashbot.command.help import HelpCommand
20+
from bashbot.command.whitelist import WhitelistCommand
2021
from bashbot.core.exceptions import SessionDontExistException, ArgumentFormatException, TerminalNotFoundException, \
2122
MacroNotFoundException
2223
from bashbot.core.settings import settings
@@ -46,6 +47,7 @@ def __init__(self, command_prefix, **options):
4647
self.add_cog(InteractiveCommand())
4748
self.add_cog(SubmitCommand())
4849
self.add_cog(ExecCommand())
50+
self.add_cog(WhitelistCommand())
4951

5052
self.remove_command("help")
5153
self.add_cog(HelpCommand())
@@ -81,10 +83,27 @@ async def on_message(self, message: Message):
8183
if message.author.bot:
8284
return
8385

84-
if isinstance(message.channel, DMChannel) and settings().get('discord.disable_dm'):
85-
embed = Embed(title=f'Using bot on DM is disabled', description='discord.disable_dm = true')
86-
await message.channel.send(embed=embed)
87-
return
86+
is_owner = await self.is_owner(message.author)
87+
if not is_owner:
88+
if settings().get('discord.enable_users_whitelist'):
89+
users_whitelist = settings().get('discord.users_whitelist', [])
90+
91+
if message.author.id not in users_whitelist:
92+
first_prefix = settings().get('commands.prefixes')[0]
93+
embed = Embed(
94+
title=f'Only whitelisted users can execute commands',
95+
description=f'{first_prefix}.whitelist add {message.author.mention}'
96+
)
97+
await message.channel.send(embed=embed)
98+
return
99+
100+
if isinstance(message.channel, DMChannel) and settings().get('discord.disable_dm'):
101+
embed = Embed(
102+
title=f'Using bot on DM is disabled',
103+
description='discord.disable_dm = true'
104+
)
105+
await message.channel.send(embed=embed)
106+
return
88107

89108
terminal = sessions().by_channel(message.channel)
90109

bashbot/command/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,3 @@ async def predicate(ctx):
1313

1414
return commands.check(predicate)
1515

16-
17-
def has_permission(name):
18-
async def predicate(ctx):
19-
return True
20-
21-
return commands.check(predicate)

bashbot/command/about.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from discord.ext import commands
33
from discord.ext.commands import Context
44

5-
from bashbot.command import has_permission
65
from bashbot.constants import REPOSITORY_URL, REPOSITORY_AUTHOR, THUMBNAIL_URL, EMBED_COLOR
76
from bashbot.core.settings import settings
87
from bashbot.core.updater import updater
@@ -13,7 +12,6 @@ class AboutCommand(commands.Cog):
1312
name='.about',
1413
description='Shows information about project'
1514
)
16-
@has_permission('info.about')
1715
async def about(self, ctx: Context):
1816
embed = Embed(title='About BashBot', description='BashBot is a Discord bot that allows terminal access via chat.', color=EMBED_COLOR)
1917
embed.add_field(name='Github', value=REPOSITORY_URL, inline=False)

bashbot/command/close.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from discord.ext import commands
22

3-
from bashbot.command import has_permission, session_exists
3+
from bashbot.command import session_exists
44
from bashbot.core.exceptions import SessionDontExistException
55
from bashbot.core.settings import settings
66
from bashbot.terminal.sessions import sessions
@@ -12,7 +12,6 @@ class CloseCommand(commands.Cog):
1212
aliases=['.c'],
1313
description='Closes current terminal session'
1414
)
15-
@has_permission('session.close')
1615
@session_exists()
1716
async def close(self, ctx):
1817
terminal = sessions().by_channel(ctx.message.channel)

bashbot/command/controls.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from discord import Message
22
from discord.ext import commands
33

4-
from bashbot.command import session_exists, has_permission
4+
from bashbot.command import session_exists
55
from bashbot.terminal.sessions import sessions
66
from bashbot.terminal.terminal import Terminal
77

@@ -18,7 +18,6 @@ async def controls(self, ctx):
1818

1919
@controls.command()
2020
@session_exists()
21-
@has_permission('input.controls.manage')
2221
async def add(self, ctx, emoji_id, content):
2322
terminal: Terminal = sessions().by_channel(ctx.channel)
2423
message: Message = sessions().find_message(terminal)
@@ -28,7 +27,6 @@ async def add(self, ctx, emoji_id, content):
2827

2928
@controls.command()
3029
@session_exists()
31-
@has_permission('input.controls.manage')
3230
async def remove(self, ctx, emoji_id):
3331
terminal: Terminal = sessions().by_channel(ctx.channel)
3432
message: Message = sessions().find_message(terminal)

bashbot/command/exec.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from discord.ext import commands
66

7-
from bashbot.command import has_permission
87
from bashbot.core.settings import settings
98

109

@@ -15,7 +14,6 @@ class ExecCommand(commands.Cog):
1514
description='Execute single command',
1615
usage='<command...>'
1716
)
18-
@has_permission('session.open')
1917
async def exec(self, ctx, *, command):
2018
shell_path = settings().get('terminal.shell_path')
2119

bashbot/command/freeze.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from discord.ext import commands
22

3-
from bashbot.command import has_permission, session_exists
3+
from bashbot.command import session_exists
44
from bashbot.core.exceptions import SessionDontExistException
55
from bashbot.terminal.sessions import sessions
66
from bashbot.terminal.terminal import TerminalState
@@ -12,7 +12,6 @@ class FreezeCommand(commands.Cog):
1212
aliases=['.f'],
1313
description='Freezes current terminal session'
1414
)
15-
@has_permission('session.freeze')
1615
@session_exists()
1716
async def freeze(self, ctx):
1817
terminal = sessions().by_channel(ctx.message.channel)

bashbot/command/help.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from discord.ext import commands
33
from discord.ext.commands import Context
44

5-
from bashbot.command import has_permission
65
from bashbot.constants import THUMBNAIL_URL, EMBED_COLOR, EMBED_COLOR_ERROR
76
from bashbot.core.settings import settings
87

@@ -12,7 +11,6 @@ class HelpCommand(commands.Cog):
1211
name='.help',
1312
description='This command'
1413
)
15-
@has_permission('help')
1614
async def help(self, ctx: Context, command_name=None):
1715
if command_name:
1816
await self._help_for_command(ctx, command_name)

bashbot/command/here.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from discord.ext import commands
22
from discord.ext.commands import Context
33

4-
from bashbot.command import has_permission, session_exists
4+
from bashbot.command import session_exists
55
from bashbot.terminal.sessions import sessions
66

77

@@ -11,7 +11,6 @@ class HereCommand(commands.Cog):
1111
aliases=['.h'],
1212
description='Moves selected terminal below the user message'
1313
)
14-
@has_permission('session.here')
1514
@session_exists()
1615
async def here(self, ctx: Context):
1716
terminal = sessions().by_channel(ctx.channel)

bashbot/command/interactive.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from discord.ext import commands
22
from discord.ext.commands import Context
33

4-
from bashbot.command import has_permission, session_exists
4+
from bashbot.command import session_exists
55
from bashbot.core.exceptions import SessionDontExistException
66
from bashbot.terminal.sessions import sessions
77

@@ -12,7 +12,6 @@ class InteractiveCommand(commands.Cog):
1212
aliases=['.i'],
1313
description='Toggles interactive mode where all messages are sent to terminal'
1414
)
15-
@has_permission('session.interactive')
1615
@session_exists()
1716
async def interactive(self, ctx: Context):
1817
terminal = sessions().by_channel(ctx.message.channel)

bashbot/command/macro.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from discord.ext import commands
22
from discord.ext.commands import Context
33

4-
from bashbot.command import has_permission, session_exists
4+
from bashbot.command import session_exists
55
from bashbot.core.macros import execute_macro
66

77

@@ -12,7 +12,6 @@ class MacroCommand(commands.Cog):
1212
description='Executes macro from "macros" directory',
1313
usage='<macro_name>'
1414
)
15-
@has_permission('session.macro')
1615
@session_exists()
1716
async def macro(self, ctx: Context, macro_name: str):
1817
await execute_macro(ctx, macro_name)

bashbot/command/open.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from discord.ext import commands
22

3-
from bashbot.command import has_permission
43
from bashbot.core.exceptions import ArgumentFormatException
54
from bashbot.core.macros import execute_macro
65
from bashbot.core.settings import settings
@@ -16,7 +15,6 @@ class OpenCommand(commands.Cog):
1615
description='Opens new terminal session',
1716
usage='[name]'
1817
)
19-
@has_permission('session.open')
2018
async def open(self, ctx, name: str = None):
2119
if name and len(name) > 20:
2220
raise ArgumentFormatException('Session name length exceeds 20 characters limit')

bashbot/command/rename.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from discord.ext import commands
22

3-
from bashbot.command import has_permission, session_exists
3+
from bashbot.command import session_exists
44
from bashbot.terminal.sessions import sessions
55

66

@@ -10,7 +10,6 @@ class RenameCommand(commands.Cog):
1010
description='Changes session name',
1111
usage='<new_name>'
1212
)
13-
@has_permission('session.rename')
1413
@session_exists()
1514
async def rename(self, ctx, new_name):
1615
terminal = sessions().by_channel(ctx.message.channel)

bashbot/command/repeat.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from discord.ext import commands
22

3-
from bashbot.command import has_permission
43
from bashbot.core.exceptions import SessionDontExistException
54
from bashbot.terminal.sessions import sessions
65

@@ -12,7 +11,6 @@ class RepeatCommand(commands.Cog):
1211
description='Repeats string n times and sends to the current terminal session',
1312
usage='<string..>'
1413
)
15-
@has_permission('session.repeat')
1614
async def repeat(self, ctx, n: int, *, text):
1715
terminal = sessions().by_channel(ctx.message.channel)
1816
if not terminal:

bashbot/command/select.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from discord.ext import commands
22

3-
from bashbot.command import has_permission
43
from bashbot.core.exceptions import TerminalNotFoundException
54
from bashbot.terminal.sessions import sessions
65

@@ -12,7 +11,6 @@ class SelectCommand(commands.Cog):
1211
description='Sets terminal as selected',
1312
usage='[name]'
1413
)
15-
@has_permission('session.select')
1614
async def select(self, ctx, name):
1715
terminal = sessions().by_name(name)
1816
if not terminal:

bashbot/command/submit.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from discord.ext import commands
22
from discord.ext.commands import Context
33

4-
from bashbot.command import has_permission, session_exists
4+
from bashbot.command import session_exists
55
from bashbot.terminal.sessions import sessions
66

77

@@ -10,7 +10,6 @@ class SubmitCommand(commands.Cog):
1010
name='.submit',
1111
description='Toggles auto submit mode'
1212
)
13-
@has_permission('session.submit')
1413
@session_exists()
1514
async def submit(self, ctx: Context):
1615
terminal = sessions().by_channel(ctx.channel)

bashbot/command/whitelist.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from discord import User
2+
from discord.ext import commands
3+
4+
from bashbot.core.settings import settings
5+
6+
7+
class WhitelistCommand(commands.Cog):
8+
@commands.group(
9+
name='.whitelist',
10+
description='Manages users whitelist',
11+
usage='add/remove <user_tag>'
12+
)
13+
async def controls(self, ctx):
14+
if ctx.invoked_subcommand is None:
15+
pass
16+
17+
@controls.command()
18+
async def add(self, ctx, user: User):
19+
whitelist = settings().config['discord']['users_whitelist']
20+
if user.id not in whitelist:
21+
whitelist.append(user.id)
22+
settings().save()
23+
await ctx.send(f"Added user {user.mention} to whitelist")
24+
else:
25+
await ctx.send(f"User {user.mention} is already on the whitelist")
26+
27+
@controls.command()
28+
async def remove(self, ctx, user: User):
29+
whitelist = settings().config['discord']['users_whitelist']
30+
if user.id in whitelist:
31+
whitelist.remove(user.id)
32+
settings().save()
33+
await ctx.send(f"Removed user {user.mention} from whitelist")
34+
else:
35+
await ctx.send(f"User {user.mention} is not on the whitelist")

bashbot/core/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def load(self, path=DEFAULT_CONFIG_PATH):
2323
self.add_default('discord.token', 'TOKEN_HERE')
2424
self.add_default('discord.presence', '{prefix}.help')
2525
self.add_default('discord.disable_dm', True)
26+
self.add_default('discord.enable_users_whitelist', True)
27+
self.add_default('discord.users_whitelist', [])
2628

2729
# [terminal]
2830
self.add_default('terminal.template', '`| TTY #{name} | {state} |`\n```{content}```')

0 commit comments

Comments
 (0)