Skip to content
Snippets Groups Projects
Commit 1db97183 authored by dnns01's avatar dnns01
Browse files

Add permissions to chat commands and show list of commands ordered alphabetically

parent 78a20875
No related branches found
No related tags found
No related merge requests found
# Generated by Django 4.0.7 on 2022-08-26 19:51
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('strolchibot', '0011_auto_20211220_2354'),
]
operations = [
migrations.AddField(
model_name='command',
name='permissions',
field=models.CharField(choices=[('EO', 'Everyone'), ('SUB', 'Subscriber'), ('MOD', 'Moderator')], default='EO', max_length=5),
),
]
......@@ -7,8 +7,11 @@ from .managers import TwitchUserManager
class Command(models.Model):
PERMISSION_CHOICES = (("EO", "Everyone"), ("SUB", "Subscriber"), ("MOD", "Moderator"))
command = models.CharField(max_length=20)
text = models.TextField(max_length=500)
permissions = models.CharField(max_length=5, choices=PERMISSION_CHOICES, default="EO")
active = models.BooleanField(default=True)
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
......
......@@ -22,7 +22,7 @@
<tr>
<td>{{ command.command }}</td>
<td>{{ command.text }}</td>
<td>Everyone</td>
<td>{{ command.get_permissions_display }}</td>
<td>
<label class="switch">
<input class="w3-switch" type="checkbox" {% if command.active %}checked{% endif %}
......
......@@ -207,7 +207,7 @@ def exchange_code(code):
@login_required(login_url="/login")
def commands(request: HttpRequest) -> HttpResponse:
return render(request, "commands/list.html",
{"title": "Commands", "commands": Command.objects.all(), "active": "commands"})
{"title": "Commands", "commands": Command.objects.all().order_by("command"), "active": "commands"})
@login_required(login_url="/login")
......
......@@ -10,6 +10,17 @@ import config
DB_PATH = "db.sqlite3"
def check_permissions(message, permissions):
if permissions == "EO":
return True
elif permissions == "SUB":
return message.author.is_subscriber or message.author.is_mod
elif permissions == "MOD":
return message.author.is_mod
return False
class Commands(commands.Cog):
def __init__(self, bot):
self.bot = bot
......@@ -27,13 +38,15 @@ class Commands(commands.Cog):
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('SELECT text from strolchibot_command where command = ? and active is true', (command,))
texts = c.fetchall()
c.execute('SELECT text, permissions from strolchibot_command where command = ? and active is true',
(command,))
eligible_commands = c.fetchall()
conn.close()
if len(texts) > 0:
text = random.choice(texts)[0]
text = self.process_variables(text, args)
await message.channel.send(text)
if len(eligible_commands) > 0:
cmd = random.choice(eligible_commands)
if check_permissions(message, cmd[1]):
text = self.process_variables(cmd[0], args)
await message.channel.send(text)
def process_variables(self, text, args):
variables = re.findall("\{[\w\d\s+-]+}", text)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment