Giter VIP home page Giter VIP logo

discord-bot's Introduction

discord-bot-template

Generic, functional bot based on discord.py V2.
Including:

  • general bot setup, saving your time

    • overwritten behaviour in on_ready(), setup_hook() and on_guild_join()
    • cog-structure
  • commands

    • ping-commands for slash- and chat-usage as demonstration
    • a custom help command for old-style chat-command as examples
    • old-style reacts to prefix and mention
  • logging setup for console and file

  • utils for easy embed creation, id-extraction and more

  • easy to use external configuration using json or env-variables

  • overall project structure for easy packaging and deployment

This template is aimed at beginners (how to start) for learning purposes and advanced users for saving time.

Setup

Setup a venv (optional, but recommend)
python3 -m venv venv
source venv/bin/activate
Using pip to install and run the bot as (editable) package:
python3 -m pip install -e .
export TOKEN="your-token"
discord-bot

Note: -e is meant only for development. Do not use it for deployment!

Or run the bot directly:
pip install -r requirements.txt  
export TOKEN="your-token"
python3 src/discord_bot/__init__.py

Intents

The bot uses all intents by default, those are required for such simple things like 'display member-count at startup'.
You need to enable those intents in the discord developers portal under *YourApplication*/Bot/Privileged Gateway Intents.
It's possible reconfigure the requested intents in main.py if you don't need them.
But I'd suggest using them all for the beginning, especially if you're relatively new to discord.py.
This will only be an issue if your bot reaches more than 100 servers, then you've got to apply for those intents.

Optional env variables

parameter description
PREFIX="b!" Command prefix
OWNER_NAME="unknwon" Name of the bot owner
OWNER_ID="100000000000000000" ID of the bot owner
ACTIVITY_NAME=f"{PREFIX}help" Activity bot plays

The shown values are the default values that will be loaded if nothing else is specified.
Expressions like {PREFIX} will be replaced by during loading the variable and can be used in specified env variables.

Set those variables using env-variables (suggested):
export PREFIX="b!"
Or use a json-file expected at: ./data/config.json like:

{
  "TOKEN": "[your-token]",
  "PREFIX": "b!"
}

If a variable is set using env and json the environment-variable replaces the json!

I'm new to bots - where to start?

Have a look at src/discord_bot/cogs/misc.py this is a good place to start with your first smaller functions.
You'll find some basic examples there.
Try to modify the ping-command or start with a small listener (on_message) that responds to each message the bot receives.
Or write a slash command that sends the date the member joined the server.

You can expand to yor own, new modules when you feel ready for it :)
The official docs for discord.py are here.
There are also very well documented examples in the official repository.

about

This repository contains code that was written by me across various bot-projects, like:
https://github.com/nonchris/discord-fury
https://github.com/nonchris/quiz-bot
https://github.com/Info-Bonn/verification-listener
https://github.com/nonchris/discord-role-selection/tree/main/src/bot

I collected the most useful and generic functions to save me some time when starting the next bot-project.

documentation

In order to render this documentation, just call doxygen

discord-bot's People

Contributors

cburgard avatar nonchris avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

discord-bot's Issues

Running setup produces ValueError: path 'src/' cannot end with '/'"

First of all, thanks for the great work you did on this bot. The learning curve is very nice, and the code is high quality to my taste.

Running PyCharm on Windows 10 Home, imported my new project from your great template through the GitHub plugin.

It offers to create a VEnv:
image

Which produces the following error:
image

Full log dump:

Obtaining file:///D:/artist-bot-reproduce
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'error'

  ERROR: Command errored out with exit status 1:
   command: 'D:\artist-bot-reproduce\.venv\Scripts\python.exe' -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'D:\\artist-bot-reproduce\\setup.py'"'"'; __file__='"'"'D:\\artist-bot-reproduce\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\sergey\AppData\Local\Temp\pip-pip-egg-info-h5dh40jv'
       cwd: D:\artist-bot-reproduce\
  Complete output (9 lines):
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "D:\artist-bot-reproduce\setup.py", line 52, in <module>
      packages=find_packages(where='src/'),
    File "D:\artist-bot-reproduce\.venv\lib\site-packages\setuptools\__init__.py", line 65, in find
      convert_path(where),
    File "D:\artist-bot-reproduce\.venv\lib\site-packages\setuptools\_distutils\util.py", line 182, in convert_path
      raise ValueError("path '%s' cannot end with '/'" % pathname)
  ValueError: path 'src/' cannot end with '/'
  ----------------------------------------
WARNING: Discarding file:///D:/artist-bot-reproduce. Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
WARNING: You are using pip version 21.3.1; however, version 22.2 is available.
You should consider upgrading via the 'D:\artist-bot-reproduce\.venv\Scripts\python.exe -m pip install --upgrade pip' command.

Subsequent running of python -m pip install -e . (not python3 because it's venv) produces the same error:

(.venv) PS D:\artist-bot-reproduce> python -m pip install -e .
Obtaining file:///D:/artist-bot-reproduce
  Preparing metadata (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: 'D:\artist-bot-reproduce\.venv\Scripts\python.exe' -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'D:\\artist-bot-reproduce\\setup.py'"'"'; __file__='"'"'D:\\artist-bot-reproduce
\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'
"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\sergey\AppData\Local\Temp\pip-pip-egg-info-syh3zbi6'
       cwd: D:\artist-bot-reproduce\
  Complete output (9 lines):
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "D:\artist-bot-reproduce\setup.py", line 52, in <module>
      packages=find_packages(where='src/'),
    File "D:\artist-bot-reproduce\.venv\lib\site-packages\setuptools\__init__.py", line 65, in find
      convert_path(where),
    File "D:\artist-bot-reproduce\.venv\lib\site-packages\setuptools\_distutils\util.py", line 182, in convert_path
      raise ValueError("path '%s' cannot end with '/'" % pathname)
  ValueError: path 'src/' cannot end with '/'
  ----------------------------------------
WARNING: Discarding file:///D:/artist-bot-reproduce. Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
WARNING: You are using pip version 21.3.1; however, version 22.2 is available.
You should consider upgrading via the 'D:\artist-bot-reproduce\.venv\Scripts\python.exe -m pip install --upgrade pip' command.
(.venv) PS D:\artist-bot-reproduce>

I'm not entirely sure this is the correct fix, but removing the / from src/ in the two lines below fixed the issue for me. Maybe under other OS'es it won't work.

discord-bot/setup.py

Lines 50 to 52 in c78f234

package_dir={'': 'src/'},
packages=find_packages(where='src/'),

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.