Skip to content

Commit b0d8621

Browse files
committed
feat: Create basic controllers and implement initial functionality
- Added greet_user function in the greetUser.py to handle user greetings. - Implemented basic structure for Flask and Discord bot controllers. - Added initial documentation for the greet_user function.
1 parent f7d10c7 commit b0d8621

File tree

7 files changed

+187
-2
lines changed

7 files changed

+187
-2
lines changed

app/Commands/Context/greet.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import disnake
2+
from disnake.ext import commands
3+
from app.Controllers.Discord.greetUser import greet_user
4+
5+
class Greet(commands.Cog):
6+
"""
7+
A cog that contains a command to greet users.
8+
9+
This cog defines a command that greets the user who invoked it. The greeting message is generated
10+
using the greet_user function from the greetUser controller.
11+
12+
Attributes:
13+
bot (commands.Bot): The bot instance that the cog is attached to.
14+
"""
15+
16+
def __init__(self, bot):
17+
"""
18+
Initializes the Greet cog.
19+
20+
Args:
21+
bot (commands.Bot): The bot instance that the cog is attached to.
22+
"""
23+
self.bot = bot
24+
25+
@commands.command()
26+
async def hello(self, ctx):
27+
"""
28+
Command to greet the user who invoked it using the Controller: app/Controllers/Discord/greetUser.py.
29+
30+
This command uses the greet_user function to generate a greeting message for the user who invoked
31+
the command and sends it as a response in the Discord channel.
32+
33+
Args:
34+
ctx (commands.Context): The context in which the command was invoked.
35+
"""
36+
greeting = greet_user(ctx.author.name)
37+
await ctx.send(greeting)
38+
39+
def setup(bot):
40+
bot.add_cog(Greet(bot))

app/Commands/Context/ping.py

+24
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,35 @@
22
from disnake.ext import commands
33

44
class Ping(commands.Cog):
5+
"""
6+
A cog that contains a command to check the bot's latency.
7+
8+
This cog defines a command named 'ping' that responds with the bot's latency
9+
when invoked. The latency is calculated and returned in milliseconds.
10+
11+
Attributes:
12+
bot (commands.Bot): The bot instance that the cog is attached to.
13+
"""
14+
515
def __init__(self, bot):
16+
"""
17+
Initializes the Ping cog.
18+
19+
Args:
20+
bot (commands.Bot): The bot instance that the cog is attached to.
21+
"""
622
self.bot = bot
723

824
@commands.command()
925
async def ping(self, ctx):
26+
"""
27+
Command to check the bot's latency.
28+
29+
This command calculates the bot's latency and sends it as a response in the Discord channel.
30+
31+
Args:
32+
ctx (commands.Context): The context in which the command was invoked.
33+
"""
1034
latency = round(self.bot.latency * 1000) # Calculate bot's latency in milliseconds
1135
await ctx.send(f'Pong! Latency: {latency}ms')
1236

app/Commands/Slash/greet.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import disnake
2+
from disnake.ext import commands
3+
from app.Controllers.Discord.greetUser import greet_user
4+
5+
class GreetSlash(commands.Cog):
6+
"""
7+
A cog that contains a command to greet users.
8+
9+
This cog defines a command that greets the user who invoked it. The greeting message is generated
10+
using the greet_user function from the greetUser controller.
11+
12+
Attributes:
13+
bot (commands.Bot): The bot instance that the cog is attached to.
14+
"""
15+
16+
def __init__(self, bot):
17+
"""
18+
Initializes the Greet cog.
19+
20+
Args:
21+
bot (commands.Bot): The bot instance that the cog is attached to.
22+
"""
23+
self.bot = bot
24+
25+
@commands.slash_command(
26+
name="hello",
27+
description="Greet the user"
28+
)
29+
async def hello(self, ctx):
30+
"""
31+
Command to greet the user who invoked it using the Controller: app/Controllers/Discord/greetUser.py.
32+
33+
This command uses the greet_user function to generate a greeting message for the user who invoked
34+
the command and sends it as a response in the Discord channel.
35+
36+
Args:
37+
ctx (commands.Context): The context in which the command was invoked.
38+
"""
39+
greeting = greet_user(ctx.author.name)
40+
await ctx.send(greeting)
41+
42+
def setup(bot):
43+
bot.add_cog(GreetSlash(bot))

app/Commands/Slash/ping.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,38 @@
22
from disnake.ext import commands
33

44
class PingSlash(commands.Cog):
5+
"""
6+
A cog that contains a slash command to check the bot's latency.
7+
8+
This cog defines a slash command named 'ping' that responds with the bot's latency
9+
when invoked. The latency is calculated and returned in milliseconds.
10+
11+
Attributes:
12+
bot (commands.Bot): The bot instance that the cog is attached to.
13+
"""
14+
515
def __init__(self, bot):
16+
"""
17+
Initializes the PingSlash cog.
18+
19+
Args:
20+
bot (commands.Bot): The bot instance that the cog is attached to.
21+
"""
622
self.bot = bot
723

824
@commands.slash_command(
925
name="ping",
1026
description="Check the bot's latency",
1127
)
1228
async def ping(self, ctx):
13-
"""Command to check the bot's latency"""
29+
"""
30+
Command to check the bot's latency.
31+
32+
This command calculates the bot's latency and sends it as a response in the Discord channel.
33+
34+
Args:
35+
ctx (commands.Context): The context in which the command was invoked.
36+
"""
1437
await ctx.send(f"Pong! Latency: {round(self.bot.latency * 1000)}ms")
1538

1639
def setup(bot):

app/Controllers/Discord/greetUser.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def greet_user(name):
2+
"""
3+
Controller function to greet a user in Discord.
4+
5+
This function takes the name of a user and returns a greeting message. It can be used in a Discord bot
6+
to send personalized greetings to users.
7+
8+
Args:
9+
name (str): The name of the user.
10+
11+
Returns:
12+
str: A greeting message.
13+
"""
14+
# Do some processing here
15+
return f"Hello, **{name}**!"

app/Controllers/Flask/greetUser.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def greet_user(name):
2+
"""
3+
Controller function to greet a user.
4+
5+
This function is called when the user visits the /example route. It processes the user's name
6+
and returns a greeting message in HTML format.
7+
8+
Args:
9+
name (str): The name of the user.
10+
11+
Returns:
12+
str: An HTML string with a greeting message.
13+
"""
14+
# Do some processing here
15+
return f"<h1>Hello, {name}!</h1>"

routes/web.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
from flask import current_app as app, render_template
2+
from app.Controllers.Flask.greetUser import greet_user
23

34
@app.route('/')
45
def home():
5-
return render_template('index.html')
6+
"""
7+
Render the home page.
8+
9+
This route renders the index.html template when the user visits the root URL.
10+
11+
Returns:
12+
str: The rendered HTML of the home page.
13+
"""
14+
return render_template('index.html')
15+
16+
@app.route('/<name>')
17+
def api_hello(name):
18+
"""
19+
Greet the user with the provided name using the Controller: app/Controllers/Discord/greetUser.py.
20+
21+
This route calls the greet_user function to generate a greeting message for the user
22+
with the given name and returns it as a response.
23+
24+
Args:
25+
name (str): The name of the user.
26+
27+
Returns:
28+
str: A greeting message for the user.
29+
"""
30+
return greet_user(name)

0 commit comments

Comments
 (0)