A modern, easy to use, feature-rich, and async ready API wrapper for Discord written in Python.
- Modern Pythonic API using
asyncandawait. - Proper rate limit handling.
- Optimised in both speed and memory.
Python 3.8 or higher is required
To install the library without full voice support, you can just run the following command:
Note
A Virtual Environment is recommended to install the library, especially on Linux where the system Python is externally managed and restricts which packages you can install on it.
# Linux/macOS
python3 -m pip install -U discord.py
# Windows
py -3 -m pip install -U discord.pyOtherwise to get voice support you should run the following command:
# Linux/macOS
python3 -m pip install -U "discord.py[voice]"
# Windows
py -3 -m pip install -U discord.py[voice]To install the development version, do the following:
$ git clone https://github.com/Rapptz/discord.py
$ cd discord.py
$ python3 -m pip install -U .[voice]- PyNaCl (for voice support)
Please note that when installing voice support on Linux, you must install the following packages via your favourite package manager (e.g. apt, dnf, etc) before running the above commands:
- libffi-dev (or
libffi-develon some systems) - python-dev (e.g.
python3.8-devfor Python 3.8)
import discord
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
async def on_message(self, message):
# don't respond to ourselves
if message.author == self.user:
return
if message.content == 'ping':
await message.channel.send('pong')
intents = discord.Intents.default()
intents.message_content = True
client = MyClient(intents=intents)
client.run('token')import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='>', intents=intents)
@bot.command()
async def ping(ctx):
await ctx.send('pong')
bot.run('token')You can find more examples in the examples directory.
This fork adds cache eviction that upstream discord.py does not have. All of it lives in
discord/cache.py; the upstream files only carry a few hook lines marked # Maki fork: cache layer.
Without the cache option the library behaves exactly like upstream.
cache=discord.CacheSettings(...)is accepted byClient,AutoShardedClient,commands.Botandcommands.AutoShardedBot.- Members inside a guild are evicted after
member_ttlseconds without activity, or beyondmember_maxentries per guild. The bot's own member is never evicted. - The message cache is keyed by id (O(1) lookups) and evicts messages after
message_ttlseconds.max_messagesstill bounds its size and on a busy bot is usually what evicts first. - A background sweeper runs every
sweep_intervalseconds (default 300).
Nothing else is bounded. Guild, channel, role, emoji, sticker, thread and voice state caches behave as upstream. Users are held weakly by upstream already and die with their last member or message.
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(
command_prefix='!',
intents=intents,
chunk_guilds_at_startup=False,
max_messages=50_000, # size this to the delete log window you want
cache=discord.CacheSettings(
member_ttl=24 * 3600, # members untouched for a day are dropped
member_max=10_000, # and at most 10000 members per guild
message_ttl=6 * 3600,
),
)- An evicted member or message behaves like one that was never cached: the raw event still fires, the
richer one does not.
on_member_update,on_member_remove,on_message_editandon_message_deleteonly fire for objects still in memory. max_messagesis a count shared by every guild on the process, not a duration. Set it from yourMESSAGE_CREATErate times the window you want;message_ttlonly trims quiet processes.Guild.chunkedcomparesmember_countwith the cached member count and is not meaningful once members are evicted. Usechunk_guilds_at_startup=Falsewithmember_ttl.Messageobjects keep theirMember/Useralive until the message itself is evicted.- Nothing persists across restarts; the cache starts cold like upstream.
Where the fork lives
- All logic:
discord/cache.pyandtests/test_cache.py. Upstream never touches these. - Hook lines inside upstream files, every one marked
# Maki fork: cache layer:discord/state.py(7),discord/client.py(3),discord/__init__.py(1). 11 in total;tests/test_cache.pyasserts those numbers. discord/guild.py,discord/gateway.pyanddiscord/ext/commands/bot.pyare identical to upstream and must stay so.- Also fork-only: this README section and the
CacheSettingsentry indocs/api.rst.
Procedure
Merge, never rebase.
git remote add upstream https://github.com/Rapptz/discord.py.git # once
git fetch upstream
git checkout master
git merge upstream/masterResolving a conflict: keep upstream's version of the surrounding code, then put the marked line(s) back.
Never drop a marked line to make a conflict go away. Each hook is a single call into self._cache.
Checklist after every merge
git diff upstream/master -- discord/guild.py discord/gateway.py discord/ext/commands/bot.py # must print nothing
python -m pytest -q
python -m pyright discord/cache.py discord/state.py discord/client.py
ruff format --checktest_fork_marker_count fails if a hook line was dropped. test_message_cache_supports_every_state_usage
fails if upstream started calling something on self._messages that MessageCache does not provide;
add the method to MessageCache when that happens. When a hook is intentionally added or removed,
update the counts above and in the test in the same commit.