root 3 лет назад
Родитель
Сommit
b82a140936

+ 1 - 0
bot/commands/admin.py

@@ -19,6 +19,7 @@ class Admin(commands.Cog):
 		brief="Set channel specific settings via the webgui",
 		help="Sends a single-use time based token to the webportal"
 	)
+	@commands.has_guild_permissions(administrator=True)
 	async def chanset(self, ctx: commands.Context):
 		record = await get_active_token(self.bot.pg, ctx.guild.id)
 

+ 1 - 0
bot/commands/general.py

@@ -51,6 +51,7 @@ class General(commands.Cog):
 			else:
 				await ctx.send("What would you like me to say?")
 		elif channel:
+			
 			await channel.send(message)
 		elif user:
 			await user.send(message)

+ 2 - 8
bot/events/general.py

@@ -1,8 +1,7 @@
 import logging, discord, asyncpg, random
 from discord.ext import commands
 from query.guild import update_guild
-from query.channel import insert_channel
-from query.channel_settings import get_interact
+from query.channel import insert_channel, get_interact
 from query.channel_user import upsert_total_messages
 from query.user import create_user
 from query.settings import get_crew_channel
@@ -62,12 +61,7 @@ class General(commands.Cog):
 
 		# Respond when mentioned
 		if self.bot.user.mentioned_in(message):
-			#print("mentioned in ")
-			#print(message.channel)
-			#interact = get_interact()
-			#print(interact)
-			#if interact:
-			if get_interact(self.bot.pg, message.channel.id):
+			if await get_interact(self.bot.pg, message.channel.id):
 				messages = [
 					f"Hello {message.author.mention}. <3",
 					f"How are you today {message.author.mention}?",

+ 4 - 1
bot/query/channel.py

@@ -1,2 +1,5 @@
 async def insert_channel(pg, channel_id, guild_id):
-	await pg.execute("INSERT INTO channel(channel_id, guild) VALUES($1, $2)", channel_id, guild_id)
+	await pg.execute("INSERT INTO channel(channel_id, guild) VALUES($1, $2)", channel_id, guild_id)
+
+async def get_interact(pg, channel_id):
+	return await pg.fetchval("SELECT interact FROM channel WHERE channel_id=$1::bigint", channel_id)

+ 0 - 2
bot/query/channel_settings.py

@@ -1,2 +0,0 @@
-async def get_interact(pg, channel_id):
-	await pg.fetchrow("SELECT interact FROM channel_settings WHERE channel_id=$1::bigint", channel_id)

+ 6 - 6
webgui/config/forms.py

@@ -1,14 +1,14 @@
 from django import forms
-from .models import ChannelSettings
+from stats.models import Channel
 
 class ChannelSettingsForm(forms.ModelForm):
     class Meta:
-        model = ChannelSettings
+        model = Channel
         fields = ['interact']
-        #fields='__all__'
+        #fields = '__all__'
         #exclude = ['id'] #Does not need "fields = '__all__'".
-        # labels = {
-        #     'interact': 'Interact',
+        labels = {
+            'interact': 'Interact',
         #     'slug': '<i class="linkify icon"></i>Slug',
         #     'nickname': '<i class="id badge icon"></i>Nickname',
         #     'username': '<i class="id card icon"></i>Username',
@@ -20,7 +20,7 @@ class ChannelSettingsForm(forms.ModelForm):
         #     'services': '<i class="lightbulb outline icon"></i>Network services',
         #     'enabled': '<i class="power off icon"></i>Enabled',
         #     # 'mute': '<i class="comment slash icon"></i>Mute',
-        #}
+        }
         
         widgets={
             'interact': forms.CheckboxInput(attrs={

+ 24 - 18
webgui/config/models.py

@@ -2,31 +2,33 @@ from django.db import models
 
 # Create your models here.
 
-class ChannelSettings(models.Model):
-	id = models.AutoField(
-		primary_key = True,
-	)
-	channel = models.ForeignKey(
-		"stats.Channel",
-		on_delete = models.CASCADE,
-		to_field = "channel_id",
-		db_column = "channel",
-	)
-	interact = models.BooleanField(
-		default = False,
-		help_text = "Allow the robot to speak and interact with users."
-	)
+#class ChannelSettings(models.Model):
+#	id = models.AutoField(
+#		primary_key = True,
+#	)
+#	channel = models.ForeignKey(
+#		"stats.Channel",
+#		on_delete = models.CASCADE,
+#		to_field = "channel_id",
+#		db_column = "channel",
+#	)
+#	interact = models.BooleanField(
+#		default = False,
+#		help_text = "Allow the robot to speak and interact with users."
+#	)
 
-	class Meta:
-		managed = False
-		db_table = "channel_settings"
-		#verbose_name_plural = "accounts"
+#	class Meta:
+#		managed = False
+#		db_table = "channel_settings"
+#		#verbose_name_plural = "accounts"
 
+#from stats import models as stats_models
 class GuildAccessToken(models.Model):
 	id = models.AutoField(
 		primary_key = True,
 	)
 	guild = models.ForeignKey(
+		#stats_models.Guild,
 		"stats.Guild",
 		on_delete = models.CASCADE,
 		to_field = "guild_id",
@@ -45,3 +47,7 @@ class GuildAccessToken(models.Model):
 	created = models.DateTimeField(
         auto_now_add = True,
     )
+
+	class Meta:
+		managed = False
+		db_table = "guild_access_token"

+ 13 - 2
webgui/config/templates/config/channel_settings.html

@@ -7,7 +7,7 @@
             <i class="save icon"></i>
             <div class="content">
                 <div class="header">
-                    Changes saved.
+                    Changes saved
                 </div>
                 <p>You may close the page or continue making changes.</p>
             </div>
@@ -18,7 +18,18 @@
         {% render_form form %}
 
         {{ channel_settings.interact }}
-        <button class="ui right floated inverted positive button" type="submit" value="Submit"><i class="save icon"></i>Save</button>
+        {% if token_expired %}
+            <div class="ui negative message">
+                <div class="header">
+                    Can not save change
+                </div>
+                <p>
+                  The token has expired. Ask {{ settings.APPLICATION_NAME }} for a new token on Discord.
+                </p>
+            </div>
+        {% else %}
+            <button class="ui right floated inverted positive button" type="submit" value="Submit"><i class="save icon"></i>Save</button>
+        {% endif %}
     </form>
     
 {% endblock %}

+ 12 - 6
webgui/config/views.py

@@ -3,29 +3,34 @@ from webgui.settings import APPLICATION_NAME
 from stats.models import Channel
 from .models import GuildAccessToken
 from .forms import ChannelSettingsForm
+import datetime
 
 def channel_settings(request, channel_id, token):
 	# Check if channel and token exists.
 	channel = get_object_or_404(Channel, channel_id=channel_id)
 	#settings = get_object(ChannelSettings, channel=channel_id)
-	token = get_object_or_404(GuildAccessToken, token=token)
+	token = get_object_or_404(GuildAccessToken, token="{"+token+"}")
 
 	# Check if token is valid.
-	print(key.created.replace(tzinfo=None))
+	#print(token.created.replace(tzinfo=None))
+	print(token.created.replace(tzinfo=None) + datetime.timedelta(hours=-1))
+	#print(token.created)
 	print(datetime.datetime.now() - datetime.timedelta(minutes=10))
-	if token.created.replace(tzinfo=None) + datetime.timedelta(hours=1)> datetime.datetime.now() - datetime.timedelta(minutes=10):
+	if token.created.replace(tzinfo=None) + datetime.timedelta(hours=-1) > datetime.datetime.now() - datetime.timedelta(minutes=10):
 		token_expired = False
 	else:
 		token_expired = True
 
 	updated = False
-	if request.method == 'POST':
-		form = ChannelSettingsForm(request.POST, instance=settings)
+	if request.method == 'POST' and not token_expired:
+		print("Token valid")
+		form = ChannelSettingsForm(request.POST, instance=channel)
 		if form.is_valid():
 			form.save()
 			updated = True
 	else:
-		form = ChannelSettingsForm(instance=settings)
+		print("Token expired")
+		form = ChannelSettingsForm(instance=channel)
 
 	context = {
 		'title': 'Channel settings',
@@ -34,5 +39,6 @@ def channel_settings(request, channel_id, token):
 		'keywords': 'settings, channel',
 		'form': form,
 		'updated': updated,
+		'token_expired': token_expired,
 	}
 	return render(request, 'config/channel_settings.html', context)