-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommandhandler.py
More file actions
171 lines (136 loc) · 5.58 KB
/
Copy pathcommandhandler.py
File metadata and controls
171 lines (136 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
This module defines a Commandhandler cog that handles slash commands to
upload, download, delete, and list files.
Classes:
Commandhandler: A Discord Cog that defines slash commands to
upload, download, delete, and list files.
Functions:
setup: A function that sets up the Commandhandler cog.
"""
import discord
from discord.ext import commands
from handlers.download_file import DownloadService
from handlers.list_files import FileListService
from handlers.upload_file import UploadService
from handlers.delete_file import DeleteService
class Commandhandler(commands.Cog):
"""A Discord Cog that defines slash commands to upload, download, delete, and list files."""
def __init__(self, bot: commands.Bot):
"""Initializes the Commandhandler cog.
Args:
- bot (commands.Bot): A Discord bot object.
"""
self.bot = bot
self.upload_file = UploadService()
self.download_file = DownloadService()
self.delete_file = DeleteService()
self.list_files = FileListService(bot)
self.config = bot.config
async def userauth(self, ctx: commands.Context):
"""
Checks if the user which send a command is authenticated in the config file
Args:
- ctx (commands.context): Message context
Returns:
- bool: True if the user is authenticated, False if not
"""
usernames = self.config["AUTH"]["usernames"]
usernames = [username.strip() for username in usernames.split(", ")]
if str(ctx.author) in usernames:
return True
else:
embed = discord.Embed(title="Authentication Error",
description="You are not authorized to use this command.",
color=discord.Color.red())
embed.set_footer(text="Users can be authenticated in the config")
await ctx.reply(embed=embed)
return False
@commands.hybrid_command(
name = "upload-file",
description = "Uploads a file to discord",
with_app_command = True)
async def upload(self, ctx: commands.Context, filepath: str):
"""A Discord slash command to upload files.
Args:
- ctx (commands.Context): A context object that represents the invocation
context of the command.
- filepath (str): A string that represents the path of the file to be uploaded.
"""
if await self.userauth(ctx):
await self.upload_file.main(ctx, filepath)
@commands.hybrid_command(
name = "download-file",
description = "Downloads a file from discord with the file-id/channel_name/filename",
with_app_command = True)
async def download (self, ctx: commands.Context, file_selector: str):
"""A Discord slash command to download files.
Args:
- ctx (commands.Context): A context object that represents the invocation
context of the command.
- file_selector (str): A string that represents the file identifier
or the file name to be downloaded.
"""
if await self.userauth(ctx):
await self.download_file.main(ctx, file_selector)
@commands.hybrid_command(
name = "delete-file",
description = "Deletes a selected file from Discord",
with_app_command = True)
async def delete (self, ctx: commands.Context, file_selector: str):
"""A Discord slash command to delete files.
Args:
- ctx (commands.Context): A context object that represents the invocation context
of the command.
- file_selector (str): A string that represents the file identifier or the file name
to be deleted.
"""
if await self.userauth(ctx):
await self.delete_file.main(ctx, file_selector)
@commands.hybrid_command(
name = "list-files",
description = "List all files which are uploaded",
with_app_command = True)
async def file_list (self, ctx: commands.Context):
"""A Discord slash command to list uploaded files.
Args:
- ctx (commands.Context): A context object that represents the invocation context
of the command.
"""
await self.list_files.main(ctx)
@commands.hybrid_command(
name="help",
description="Displays an embed with a list of all available commands and their descriptions.",
with_app_command=True
)
async def help(self, ctx: commands.Context):
"""A Discord slash command that displays an embed with a list of all available commands and their descriptions.
Args:
ctx (commands.Context): A context object that represents the invocation context of the command.
"""
embed = discord.Embed(title="Available Commands", color=0xFFA500)
embed.add_field(
name="upload-file",
value="Uploads a file to Discord",
inline=False
)
embed.add_field(
name="download-file",
value="Downloads a file from Discord using the file-id/channel_name/filename",
inline=False
)
embed.add_field(
name="delete-file",
value="Deletes a selected file from Discord",
inline=False
)
embed.add_field(
name="list-files",
value="Lists all files that have been uploaded to Discord",
inline=False
)
await ctx.send(embed=embed)
async def setup(bot: commands.Bot) -> None:
"""Cogs setup func"""
await bot.add_cog(
Commandhandler(bot)
)