Use - LeConstellationniste/Discord.py-Framework- GitHub Wiki
Use
Basic use
Without CommandSet class
The bot can be created in a single file or in several files.
We will see here a possible method of organization, this is probably not the best.
my_commands.py
With decorators:
import asyncio
import discord
from discordEasy.objects import command # decorators to create commands
@command(name="Hello", aliases=("hello", "Hi", "hi"))
async def hello(message):
await message.channel.send(f"Hello {message.author.mention}!", reference=message)
@command(name="Admin", admin=True)
async def admin(message):
await message.channel.send(f"Hello {message.author.mention}! You are administrator!", reference=message)
@command(name="Product")
async def product(message, a: int, b: int):
await message.channel.send(f"`{a}*{b} = {a*b}`", reference=message)
Without decorators:
import asyncio
import discord
# Just function to add to the bot
async def hello(message):
await message.channel.send(f"Hello {message.author.mention}!", reference=message)
async def admin(message):
await message.channel.send(f"Hello {message.author.mention}! You are administrator!", reference=message)
async def product(message, a: int, b: int):
await message.channel.send(f"`{a}*{b} = {a*b}`", reference=message)
# Or Command create with this function
from discordEasy.objects import Command, CommandAdmin
async def hello(message):
await message.channel.send(f"Hello {message.author.mention}!", reference=message)
async def admin(message):
await message.channel.send(f"Hello {message.author.mention}! You are administrator!", reference=message)
async def product(message, a: int, b: int):
await message.channel.send(f"`{a}*{b} = {a*b}`", reference=message)
cmd_hello = Command(hello, name="Hello", aliases=("hello", "Hi", "hi"))
cmd_admin = CommandAdmin(admin, name="Admin")
cmd_product = Command(product, name="Product")
my_listeners.py
With decorators:
import asyncio
import discord
from discordEasy.objects import listener
@listener()
async def on_message(message):
if not message.author.bot:
await message.channel.send(f"{message.author.mention} a envoyé un message!")
Without decorators:
import asyncio
import discord
# Just with a function to add to the bot.
async def on_message(message):
if not message.author.bot:
await message.channel.send(f"{message.author.mention} a envoyé un message!")
# A Listener already created with the function
from discordEasy.objects import Listener
async def on_message(message):
if not message.author.bot:
await message.channel.send(f"{message.author.mention} a envoyé un message!")
listener_on_message = Listener(on_message)
main.py
In the main.py
script, we create and launch the bot.
from discordEasy import Bot
from my_commands import *
from my_listeners import *
my_token_bot = "YourToken"
my_prefix_bot = ">"
# The arguments 'print_traceback', 'send_errors' and 'sep_args' have by default the same values as below, they have been explained just for the example.
my_bot = Bot(prefix=my_prefix_bot, token=my_token_bot, print_traceback=True, send_errors=False, sep_args= " ")
# Commands was created with decorators
my_bot.add_commands([hello, admin, product])
# Just functions was created
my_bot.add_commands({'Hello': hello, 'Product': (product, [int, int])})
my_bot.add_command(admin, admin=True)
# Commands were created manually
my_bot.add_commands([cmd_hello, cmd_admin, cmd_product])
# Listener was created with decorators
my_bot.add_listener(on_message)
# Just functions was created
my_bot.add_listener(on_message)
# Listener were created manually
my_bot.add_listener(listener_on_message)
my_bot.run()
With CommandSet class
There is usually one file per CommandSet, and the CommandSet groups commands and listeners by relevance. In each CommandSet file we can put a setup function that will add the CommandSet to the bot.
math_commands.py
import asyncio
import discord
from discordEasy.objects import CommandSet, command
class Math(CommandSet):
def __init__(self, bot):
super().__init__()
self.bot = bot
self.description = "Commands to do differents calculations."
@command(name="Addition", aliases=("addition", "Add", "add"), description="A command to make an addition.")
async def addition(self, message, a: float, b: float):
await message.channel.send(f"`{a} + {b} = {a + b}`", reference=message, mention_author=False)
@command(name="Subtraction", aliases=("subtraction", "Sub", "sub"), description="A command to make an subtraction.")
async def subtraction(self, message, a: float, b: float):
await message.channel.send(f"`{a} - {b} = {a - b}`", reference=message, mention_author=False)
@command(name="Product", aliases("product", "Prod", "prod"), description="A command to make a product.")
async def product(self, message, a: float, b: float):
await message.channel.send(f"`{a}*{b} = {a*b}`", reference=message, mention_author=False)
@command(name="Divide", aliases=("divide", "Div", "div"), description="A command to make an division.")
async def divide(self, message, a: float, b: float):
if b == 0:
em_error = discord.Embed(title="DivideByZero Error", color=discord.Colour.red())
em_error.description = "You can't divide by zero!"
await message.channel.send(embed=em_error)
else:
await message.channel.send(f"`{a}/{b} = {a/b}`", reference=message, mention_author=False)
main.py
In the main.py
script, we create and launch the bot.
from discordEasy import Bot
from help import Help
from math_commands import Math
my_token_bot = "YourToken"
my_prefix_bot = ">"
# The arguments 'print_traceback', 'send_errors' and 'sep_args' have by default the same values as below, they have been explained just for the example.
my_bot = Bot(prefix=my_prefix_bot, token=my_token_bot, print_traceback=True, send_errors=False, sep_args= " ")
my_bot.add_commands(Help(my_bot))
my_bot.add_commands(Math(my_bot))
my_bot.run()
Advance use
If you want to customize your bot as much as possible, you can use inheritance.
MyBot example
from discordEasy import Bot
from discordEasy.utils import Logs
class MyBot(Bot):
def __init__(self, prefix, token, send_errors=False, print_traceback=True, sep_args=" ", colour: discord.Colour = discord.Colour.blue(), colour_error: discord.Colour = discord.Colour.red()):
super().__init__(self, prefix, token, send_errors, print_traceback, sep_args, colour, colour_error)
def a_new_method(self):
...
async def on_permission_error(self, channel): # For the time being, the arguments cannot be changed.
Logs.warning("a log message")
msg_error = "A new message for permission error."
em_error = discord.Embed(title="Missing permission", description=msg_error, color=self.colour_error)
await channel.send(embed=em_error)