Skip to content

Commit 0bd9008

Browse files
committed
Template v2.6
* Added new `dailyfact` command that gives a random fact every day, using cool down * Fixed some typos in README.md * Remade the `on_command_error` event for `CommandOnCooldown`
1 parent 9c260ac commit 0bd9008

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

README.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
This repository is a template that everyone can use for the start of their discord bot.
1414

1515
When I first started creating my discord bot it took me a while to get everything setup and working with cogs and more.
16-
I would've been happy if there were any template existing. But there wasn't any existing template. That's why I decided
17-
to create my own template to let <b>you</b> guys create your discord bot in an easy way.
16+
I would've been happy if there were any template existing. However, there wasn't any existing template. That's why I
17+
decided to create my own template to let <b>you</b> guys create your discord bot easily.
1818

1919
Please note that this template is not supposed to be the best template, but a good template to start learning how
20-
discord.py works and to make your own bot in a simple way. You're
20+
discord.py works and to make your own bot in a simple way.
2121

2222
If you plan to use this template to make your own template or bot, you **have to** give me credits somewhere and keep
2323
the copyright notice on the files.
@@ -30,17 +30,17 @@ information.
3030

3131
## Support
3232

33-
Before requesting support you should know that this template requires you to have a **basic knowledge** of Python. Do
34-
not use this template if you don't know the basics. [Here's](https://pythondiscord.com/pages/resources) a link for
35-
resources to learn python.
33+
Before requesting support, you should know that this template requires you to have at least a **basic knowledge** of
34+
Python and the library is made for advanced users. Do not use this template if you don't know the
35+
basics. [Here's](https://pythondiscord.com/pages/resources) a link for resources to learn python.
3636

3737
If you need some help for something, do not hesitate to join my discord server [here](https://discord.gg/HzJ3Gfr).
3838

3939
All the updates of the template are available [here](UPDATES.md).
4040

4141
## How to download it
4242

43-
This repository is now a template, on the top left you can simple click on "**Use this template**" to create a GitHub
43+
This repository is now a template, on the top left you can simply click on "**Use this template**" to create a GitHub
4444
repository based on this template.
4545

4646
Alternatively you can do the following:
@@ -54,7 +54,7 @@ Alternatively you can do the following:
5454
https://discordapp.com/oauth2/authorize?&client_id=YOUR_APPLICATION_ID_HERE&scope=bot&permissions=8 (
5555
Replace `YOUR_APPLICATION_ID_HERE` with the application ID)
5656

57-
## How to setup
57+
## How to set up
5858

5959
To set up the bot I made it as simple as possible. I now created a [config.yaml](config.yaml) file where you can put the
6060
needed things to edit.
@@ -71,7 +71,8 @@ Here is an explanation of what everything is:
7171

7272
## How to start
7373

74-
To start the bot you simply need to launch, either your terminal (Linux, Mac & Windows) or your Command Prompt (Windows)
74+
To start the bot you simply need to launch, either your terminal (Linux, Mac & Windows), or your Command Prompt (
75+
Windows)
7576
.
7677

7778
Before running the bot you will need to install all the requirements with this command:

UPDATES.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
Here is the list of all the updates that I made on this template.
44

5+
### Version 2.6
6+
7+
* Added new `dailyfact` command that gives a random fact every day, using cool down
8+
* Fixed some typos in [README.md](README.md)
9+
* Remade the `on_command_error` event for `CommandOnCooldown`
10+
511
### Version 2.5
612

713
* Code reformat

bot.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Description:
44
This is a template to create your own discord bot in python.
55
6-
Version: 2.5
6+
Version: 2.6
77
"""
88

99
import os
@@ -117,9 +117,12 @@ async def on_command_completion(ctx):
117117
@bot.event
118118
async def on_command_error(context, error):
119119
if isinstance(error, commands.CommandOnCooldown):
120+
minutes, seconds = divmod(error.retry_after, 60)
121+
hours, minutes = divmod(minutes, 60)
122+
hours = hours % 24
120123
embed = discord.Embed(
121-
title="Error!",
122-
description="This command is on a %.2fs cool down" % error.retry_after,
124+
title="Hey, please slow down!",
125+
description=f"You can use this command again in {f'{round(hours)} hours' if round(hours) > 0 else ''} {f'{round(minutes)} minutes' if round(minutes) > 0 else ''} {f'{round(seconds)} seconds' if round(seconds) > 0 else ''}.",
123126
color=config["error"]
124127
)
125128
await context.send(embed=embed)

cogs/fun.py

+37
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import random
44
import sys
55

6+
import aiohttp
67
import discord
78
import yaml
89
from discord.ext import commands
10+
from discord.ext.commands import BucketType
911

1012
if not os.path.isfile("config.yaml"):
1113
sys.exit("'config.yaml' not found! Please add it and try again.")
@@ -18,6 +20,41 @@ class Fun(commands.Cog, name="fun"):
1820
def __init__(self, bot):
1921
self.bot = bot
2022

23+
"""
24+
Why 1 and 86400?
25+
-> Because the user should be able to use the command *once* every *86400* seconds
26+
27+
Why BucketType.user?
28+
-> Because the cool down only affects the current user, if you want other types of cool downs, here are they:
29+
- BucketType.default for a global basis.
30+
- BucketType.user for a per-user basis.
31+
- BucketType.server for a per-server basis.
32+
- BucketType.channel for a per-channel basis.
33+
"""
34+
35+
@commands.command(name="dailyfact")
36+
@commands.cooldown(1, 86400, BucketType.user)
37+
async def dailyfact(self, context):
38+
"""
39+
Get a daily fact, command can only be ran once every day per user.
40+
"""
41+
# This will prevent your bot from stopping everything when doing a web request - see: https://discordpy.readthedocs.io/en/stable/faq.html#how-do-i-make-a-web-request
42+
async with aiohttp.ClientSession() as session:
43+
async with session.get("https://uselessfacts.jsph.pl/random.json?language=en") as request:
44+
if request.status == 200:
45+
data = await request.json()
46+
embed = discord.Embed(description=data["text"], color=config["main_color"])
47+
await context.send(embed=embed)
48+
else:
49+
embed = discord.Embed(
50+
title="Error!",
51+
description="There is something wrong with the API, please try again later",
52+
color=config["error"]
53+
)
54+
await context.send(embed=embed)
55+
# We need to reset the cool down since the user didn't got his daily fact.
56+
self.dailyfact.reset_cooldown(context)
57+
2158
@commands.command(name="dick")
2259
async def dick(self, context, member: discord.Member = None):
2360
"""

0 commit comments

Comments
 (0)