Skip to main content

A simplistic yet feature rich discord bot template.

Project description

Nextcord Bot Base

A subclass of commands.Bot which implements the following features and reduces the boilerplate between discord bots.

Features

  • Built in persistent blacklist system for both Guilds and Users The bot leaves blacklisted guilds on join and doesn't process commands for anything blacklisted.

  • Built in database using MongoDB with dynamic Document allocation.

import os
from bot_base import BotBase


bot = BotBase(
    command_prefix="!", mongo_url=os.environ["MONGO_URL"], mongo_database_name="my_bot"
)

# Blacklist a guild
bot.blacklist.add_to_blacklist(9876, reason="My enemy!")

# Un-blacklist a guild
bot.blacklist.remove_from_blacklist(9876, reason="I forgave them")

# Blacklist a user
bot.blacklist.add_to_blacklist(12345, reason="My enemy!", is_guild_blacklist=False)

# Un-blacklist a user
bot.blacklist.remove_from_blacklist(12345, reason="I forgave them", is_guild_blacklist=False)

All documents are created automatically when first accessed. No need to manually define database Documents anymore.

Documents are created as class variables on db, so anything you attempt to access will be created as a Document then used.

import os
from bot_base import BotBase


bot = BotBase(
    command_prefix="!", mongo_url=os.environ["MONGO_URL"], mongo_database_name="my_bot"
)

@bot.event
async def on_ready():
    # Create a document connection to 'config' and get all entries
    configs = await bot.db.config.get_all()
    print(f"{len(configs)} stored guild configs!")

.prompt(message, *, timeout=60.0, delete_after=True, author_id=None)

Easily get back a Yes or No to a given message, subclassed context allows for simplified command interactions.

import os
from bot_base import BotBase


bot = BotBase(
    command_prefix="!", mongo_url=os.environ["MONGO_URL"], mongo_database_name="my_bot"
)

@bot.command()
async def check(ctx):
    # Returns True if they click yes, else False
    # Returns None on timeout
    answer = await ctx.prompt("Should I say hi back?")
    if answer:
        await ctx.send("Hi!")

The ability to send a basic embed without actually making one you can also set embed color, the target to send to, whether to display timestamps and the command invoker.

import os
from bot_base import BotBase


bot = BotBase(
    command_prefix="!", mongo_url=os.environ["MONGO_URL"], mongo_database_name="my_bot"
)

@bot.command()
async def ping(ctx):
    await ctx.send_basic_embed("Pong!")

Example image


Finally, you can also get input from ctx cleanly with further options for timeouts, whether to delete content once a response is gained, etc.

import os
from bot_base import BotBase


bot = BotBase(
    command_prefix="!", mongo_url=os.environ["MONGO_URL"], mongo_database_name="my_bot"
)

@bot.command()
async def echo(ctx):
    # Send an embed with the prompt "What should I say?"
    # before waiting on a response.
    text = await ctx.get_input("What should I say?", delete_after=False)

    if not text:
        return await ctx.send("You said nothing!")

    await ctx.send(text)

Both ctx.author and ctx.channel also include these methods. However, they do lack some things. (See below)

The bot features many convenience methods. The following methods exist in order to Always get the given object or error trying.

await bot.get_or_fetch_user(user_id)
  • This returns a User which includes the above methods.
await bot.get_or_fetch_channel(channel_id)
  • This returns a Channel which includes the above methods with the follow caveats:
    • Both prompt and get_input require author_id for checks
    • send_basic_embed will not set footers or timestamps
await bot.get_or_fetch_guild(guild_id)
  • Simply returns a nextcord.Guild object from cache or api
await bot.get_or_fetch_member(guild_object: nextcord.Guild, member_id)
  • This returns a Member which includes the above methods.

NOTE

These classes with methods attach do not subclass the relevant class so isinstance() checks will fail. In order to do isinstance() checks you need to do the following.

# To check for Guild
import nextcord


guild = await bot.get_or_fetch_guild(98765)
if isinstance(guild, nextcord.Guild):
    # It just returns `nextcord.Guild`
    print("This is a nextcord.Guild!")

# To check for User
user = await bot.get_or_fetch_user(12345)
if isinstance(user.person, nextcord.User):
    print("This person is a nextcord.User!")

# To check for Member
member = await bot.get_or_fetch_member(guild, 12345)
if isinstance(member.person, nextcord.Member):
    print("This person is a nextcord.Member!")

# To check for Channel
channel = await bot.get_or_fetch_channel(45678)
if isinstance(channel.channel, nextcord.TextChannel):
    print("This channel is a TextChannel")
    
# All objects also have there regular attributes attached
print(f"Woah, thats {member.display_name}!")

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Bot-Base-1.0.1.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Bot_Base-1.0.1-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file Bot-Base-1.0.1.tar.gz.

File metadata

  • Download URL: Bot-Base-1.0.1.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for Bot-Base-1.0.1.tar.gz
Algorithm Hash digest
SHA256 02605c2be1110dc98cfc3af7a2755697eb8e3fd3aace2f6f028ad2c13ee40413
MD5 7be7a5fba1fd457128d10cd22b704794
BLAKE2b-256 f4ea77f477d922197fc99965aa8d2cc3eadc623ffea404f4307b2bc9cc932bb3

See more details on using hashes here.

File details

Details for the file Bot_Base-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: Bot_Base-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for Bot_Base-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4591174ef1154abac5b28508c062bccc2185104a92d22df93369eb708a7193a1
MD5 5867bbeb93e38e095ba99d88fb38e836
BLAKE2b-256 27cb459eee25c9289e2fa685adfdb4cb5d5a93435bc8703595a15db659ba48e8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page