mirror of https://github.com/stijndcl/didier
				
				
				
			Remove broken libraries & functionality, format slash command usage
							parent
							
								
									06dc3d3fb9
								
							
						
					
					
						commit
						7ad2bf351e
					
				|  | @ -1,82 +0,0 @@ | |||
| from discord.ext import ipc | ||||
| from functions.database import custom_commands | ||||
| import json | ||||
| from quart import Quart, jsonify, request | ||||
| from quart_cors import cors | ||||
| from time import time | ||||
| 
 | ||||
| 
 | ||||
| app = Quart(__name__) | ||||
| # TODO allow_origin=re.compile(r"http://localhost:.*") | ||||
| #      needs higher Python & Quart version | ||||
| app = cors(app, allow_origin="*") | ||||
| app.config.from_object(__name__) | ||||
| 
 | ||||
| 
 | ||||
| ipc_client = ipc.Client(secret_key="SOME_SECRET_KEY") | ||||
| 
 | ||||
| 
 | ||||
| @app.route("/ping", methods=["GET"]) | ||||
| async def ping(): | ||||
|     """ | ||||
|     Send a ping request, monitors bot latency and endpoint time | ||||
|     """ | ||||
|     latency = await ipc_client.request("get_bot_latency") | ||||
| 
 | ||||
|     return jsonify({"bot_latency": latency, "response_sent": time()}) | ||||
| 
 | ||||
| 
 | ||||
| @app.route("/dm", methods=["POST"]) | ||||
| async def send_dm(): | ||||
|     """ | ||||
|     Send a DM to the given user | ||||
|     """ | ||||
|     data = json.loads((await request.body).decode('UTF-8')) | ||||
| 
 | ||||
|     dm = await ipc_client.request( | ||||
|         "send_dm", | ||||
|         user=int(data["userid"]), | ||||
|         message=data.get("message") | ||||
|     ) | ||||
| 
 | ||||
|     return jsonify({"response": dm}) | ||||
| 
 | ||||
| 
 | ||||
| @app.route("/custom", methods=["GET"]) | ||||
| async def get_all_custom_commands(): | ||||
|     """ | ||||
|     Return a list of all custom commands in the bot | ||||
|     """ | ||||
|     commands = custom_commands.get_all() | ||||
| 
 | ||||
|     return jsonify(commands) | ||||
| 
 | ||||
| 
 | ||||
| @app.route("/custom/<command_id>") | ||||
| async def get_custom_command(command_id): | ||||
|     try: | ||||
|         command_id = int(command_id) | ||||
|     except ValueError: | ||||
|         # Id is not an int | ||||
|         return unprocessable_entity("Parameter id was not a valid integer.") | ||||
| 
 | ||||
|     command = custom_commands.get_by_id(command_id) | ||||
| 
 | ||||
|     if command is None: | ||||
|         return page_not_found("") | ||||
| 
 | ||||
|     return jsonify(command) | ||||
| 
 | ||||
| 
 | ||||
| @app.errorhandler(404) | ||||
| def page_not_found(e): | ||||
|     return jsonify({"error": "No resource could be found matching the given URL."}), 404 | ||||
| 
 | ||||
| 
 | ||||
| @app.errorhandler(422) | ||||
| def unprocessable_entity(e): | ||||
|     return jsonify({"error": e}), 422 | ||||
| 
 | ||||
| 
 | ||||
| if __name__ == "__main__": | ||||
|     app.run() | ||||
|  | @ -1,11 +1,10 @@ | |||
| from dislash import SlashInteraction | ||||
| from discord import Interaction | ||||
| 
 | ||||
| from data import constants | ||||
| from data.snipe import Snipe, Action, should_snipe | ||||
| import datetime | ||||
| import discord | ||||
| from discord.ext import commands | ||||
| from dislash.application_commands.errors import InteractionCheckFailure | ||||
| from functions import checks, easterEggResponses, stringFormatters | ||||
| from functions.database import stats, muttn, custom_commands, commands as command_stats | ||||
| import pytz | ||||
|  | @ -86,6 +85,7 @@ class Events(commands.Cog): | |||
|         Logs commands in your terminal. | ||||
|         :param ctx: Discord Context | ||||
|         """ | ||||
|         print("a") | ||||
|         print(stringFormatters.format_command_usage(ctx)) | ||||
| 
 | ||||
|         command_stats.invoked(command_stats.InvocationType.TextCommand) | ||||
|  | @ -123,24 +123,27 @@ class Events(commands.Cog): | |||
|             await self.sendErrorEmbed(err, "Command", usage) | ||||
| 
 | ||||
|     @commands.Cog.listener() | ||||
|     async def on_slash_command(self, interaction: SlashInteraction): | ||||
|     async def on_interaction(self, interaction: Interaction): | ||||
|         """ | ||||
|         Function called whenever someone uses a slash command | ||||
|         """ | ||||
|         if not interaction.is_command(): | ||||
|             return | ||||
| 
 | ||||
|         print(stringFormatters.format_slash_command_usage(interaction)) | ||||
| 
 | ||||
|         command_stats.invoked(command_stats.InvocationType.SlashCommand) | ||||
| 
 | ||||
|     @commands.Cog.listener() | ||||
|     async def on_slash_command_error(self, interaction, err): | ||||
|     async def on_application_command_error(self, ctx: discord.ApplicationContext, err): | ||||
|         # Debugging Didier shouldn't spam the error logs | ||||
|         if self.client.user.id != int(constants.didierId): | ||||
|             raise err | ||||
| 
 | ||||
|         if isinstance(err, InteractionCheckFailure): | ||||
|             return await interaction.reply("Je hebt geen toegang tot dit commando.", ephemeral=True) | ||||
|         if isinstance(err, commands.CheckFailure): | ||||
|             return await ctx.respond("Je hebt geen toegang tot dit commando.", ephemeral=True) | ||||
| 
 | ||||
|         usage = stringFormatters.format_slash_command_usage(interaction) | ||||
|         usage = stringFormatters.format_slash_command_usage(ctx.interaction) | ||||
|         await self.sendErrorEmbed(err, "Slash Command", usage) | ||||
| 
 | ||||
|     @commands.Cog.listener() | ||||
|  |  | |||
							
								
								
									
										27
									
								
								cogs/ipc.py
								
								
								
								
							
							
						
						
									
										27
									
								
								cogs/ipc.py
								
								
								
								
							|  | @ -1,27 +0,0 @@ | |||
| from discord.ext import commands, ipc | ||||
| 
 | ||||
| 
 | ||||
| class IPC(commands.Cog): | ||||
|     def __init__(self, client): | ||||
|         self.client = client | ||||
| 
 | ||||
|     @ipc.server.route() | ||||
|     async def send_dm(self, data): | ||||
|         print("got here") | ||||
|         user = self.client.get_user(data.user) | ||||
|         await user.send(data.message) | ||||
|         print("sent") | ||||
|         return True | ||||
| 
 | ||||
|     @ipc.server.route() | ||||
|     async def get_bot_latency(self, data): | ||||
|         """ | ||||
|         Get Didier's latency | ||||
|         """ | ||||
| 
 | ||||
|         return self.client.latency * 1000 | ||||
| 
 | ||||
| 
 | ||||
| def setup(client): | ||||
|     # client.add_cog(IPC(client)) | ||||
|     pass | ||||
|  | @ -1,88 +0,0 @@ | |||
| import datetime | ||||
| import json | ||||
| 
 | ||||
| from discord.ext import commands | ||||
| from dislash import SlashInteraction, slash_command, Option, OptionType, check | ||||
| from functions.checks import isMe | ||||
| from functions.timeFormatters import fromString | ||||
| from startup.didier import Didier | ||||
| 
 | ||||
| 
 | ||||
| class DBSlash(commands.Cog): | ||||
|     def __init__(self, client: Didier): | ||||
|         self.client: Didier = client | ||||
| 
 | ||||
|     @slash_command(name="db") | ||||
|     @check(isMe) | ||||
|     async def _db_slash(self, interaction: SlashInteraction): | ||||
|         pass | ||||
| 
 | ||||
|     @_db_slash.sub_command_group(name="add") | ||||
|     async def _add_slash(self, interaction: SlashInteraction): | ||||
|         pass | ||||
| 
 | ||||
|     @_add_slash.sub_command( | ||||
|         name="deadline", | ||||
|         options=[ | ||||
|             Option( | ||||
|                 "year", | ||||
|                 description="Year (1-based)", | ||||
|                 type=OptionType.INTEGER, | ||||
|                 required=True | ||||
|             ), | ||||
|             Option( | ||||
|               "course", | ||||
|               description="Course (abbreviated)", | ||||
|               type=OptionType.STRING, | ||||
|               required=True | ||||
|             ), | ||||
|             Option( | ||||
|               "name", | ||||
|               description="Name of the deadline/project", | ||||
|               type=OptionType.STRING, | ||||
|               required=True | ||||
|             ), | ||||
|             Option( | ||||
|                 "date", | ||||
|                 description="Date (DD/MM)", | ||||
|                 type=OptionType.STRING, | ||||
|                 required=True | ||||
|             ), | ||||
|             Option( | ||||
|                 "time", | ||||
|                 description="Timestamp (HH:MM or HH:MM:SS)", | ||||
|                 type=OptionType.STRING, | ||||
|                 required=False | ||||
|             ) | ||||
|         ] | ||||
|     ) | ||||
|     async def _add_deadline_slash(self, interaction: SlashInteraction, year: int, course: str, name: str, date: str, time: str = "00:00:00"): | ||||
|         with open("files/deadlines.json", "r") as f: | ||||
|             deadlines = json.load(f) | ||||
| 
 | ||||
|         date += "/" + str(datetime.datetime.now().year) | ||||
| 
 | ||||
|         # Fix format | ||||
|         if time.count(":") == 1: | ||||
|             time += ":00" | ||||
| 
 | ||||
|         dt = fromString(f"{date} {time}", formatString="%d/%m/%Y %H:%M:%S", tzinfo=None) | ||||
| 
 | ||||
|         # Add year & course if necessary | ||||
|         if str(year) not in deadlines: | ||||
|             deadlines[str(year)] = {} | ||||
| 
 | ||||
|         if course not in deadlines[str(year)]: | ||||
|             deadlines[str(year)][course] = {} | ||||
| 
 | ||||
|         deadlines[str(year)][course][name] = round(dt.timestamp()) | ||||
| 
 | ||||
|         with open("files/deadlines.json", "w") as f: | ||||
|             json.dump(deadlines, f) | ||||
| 
 | ||||
|         await interaction.reply("Addition successful", ephemeral=True) | ||||
| 
 | ||||
| 
 | ||||
| def setup(client: Didier): | ||||
|     # client.add_cog(DBSlash(client)) | ||||
|     pass | ||||
|  | @ -1,12 +1,9 @@ | |||
| import discord | ||||
| from dotenv import load_dotenv | ||||
| from functions.prefixes import get_prefix | ||||
| from settings import STATUS_MESSAGE, TOKEN | ||||
| from startup.didier import Didier | ||||
| 
 | ||||
| if __name__ == "__main__": | ||||
|     load_dotenv(verbose=True) | ||||
| 
 | ||||
|     # Activities | ||||
|     activity = discord.Activity(type=discord.ActivityType.playing, name=STATUS_MESSAGE) | ||||
|     status = discord.Status.online | ||||
|  |  | |||
|  | @ -1,7 +1,7 @@ | |||
| import traceback | ||||
| 
 | ||||
| from discord import Interaction | ||||
| from discord.ext.commands import Context | ||||
| from dislash import SlashInteraction | ||||
| 
 | ||||
| 
 | ||||
| def title_case(string): | ||||
|  | @ -42,16 +42,15 @@ def format_command_usage(ctx: Context) -> str: | |||
|     return f"{ctx.author.display_name} in {_format_error_location(ctx)}: {ctx.message.content}" | ||||
| 
 | ||||
| 
 | ||||
| def format_slash_command_usage(interaction: SlashInteraction) -> str: | ||||
| def format_slash_command_usage(interaction: Interaction) -> str: | ||||
|     # Create a string with the options used | ||||
|     # TODO look into the format used by the lib because it's terrible | ||||
|     options = " ".join(list(map( | ||||
|         lambda option: f"{option.name}: \"{option.value}\"", | ||||
|         interaction.data.options.values() | ||||
|         lambda o: f"{o['name']}: \"{o['value']}\"", | ||||
|         interaction.data.get("options", []) | ||||
|     ))) | ||||
| 
 | ||||
|     command = f"{interaction.slash_command.name} {options or ''}" | ||||
|     return f"{interaction.author.display_name} in {_format_error_location(interaction)}: /{command}" | ||||
|     command = f"{interaction.data['name']} {options or ''}" | ||||
|     return f"{interaction.user.display_name} in {_format_error_location(interaction)}: /{command}" | ||||
| 
 | ||||
| 
 | ||||
| def get_edu_year(index: int) -> str: | ||||
|  |  | |||
|  | @ -13,8 +13,6 @@ tabulate==0.8.7 | |||
| yarl==1.4.2 | ||||
| feedparser==6.0.2 | ||||
| googletrans==4.0.0rc1 | ||||
| quart==0.15.1 | ||||
| Quart-CORS==0.5.0 | ||||
| attrs~=21.2.0 | ||||
| dacite~=1.6.0 | ||||
| pytest==6.2.4 | ||||
|  |  | |||
|  | @ -4,7 +4,7 @@ from dotenv import load_dotenv | |||
| import os | ||||
| 
 | ||||
| 
 | ||||
| load_dotenv() | ||||
| load_dotenv(verbose=True) | ||||
| 
 | ||||
| 
 | ||||
| def _to_bool(value: str) -> bool: | ||||
|  | @ -31,7 +31,6 @@ DB_NAME = os.getenv("DBNAME", "") | |||
| 
 | ||||
| # Discord-related | ||||
| TOKEN = os.getenv("TOKEN", "") | ||||
| HOST_IPC = _to_bool(os.getenv("HOSTIPC", "false")) | ||||
| READY_MESSAGE = os.getenv("READYMESSAGE", "I'M READY I'M READY I'M READY I'M READY")  # Yes, this is a Spongebob reference | ||||
| STATUS_MESSAGE = os.getenv("STATUSMESSAGE", "with your Didier Dinks.") | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,6 +1,5 @@ | |||
| from data.snipe import Snipe | ||||
| from discord.ext import commands | ||||
| from dislash import InteractionClient | ||||
| import os | ||||
| from startup.init_files import check_all | ||||
| from typing import Dict | ||||
|  | @ -10,9 +9,6 @@ class Didier(commands.Bot): | |||
|     """ | ||||
|     Main Bot class for Didier | ||||
|     """ | ||||
|     # Reference to interactions client | ||||
|     interactions: InteractionClient | ||||
| 
 | ||||
|     # Dict to store the most recent Snipe info per channel | ||||
|     snipe: Dict[int, Snipe] = {} | ||||
| 
 | ||||
|  | @ -20,7 +16,7 @@ class Didier(commands.Bot): | |||
|         super().__init__(*args, **kwargs) | ||||
| 
 | ||||
|         # Cogs that should be loaded before the others | ||||
|         self._preload = ("ipc", "utils", "failedchecks", "events",) | ||||
|         self._preload = ("utils", "failedchecks", "events",) | ||||
| 
 | ||||
|         # Remove default help command | ||||
|         self.remove_command("help") | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue