discord python bot documentation code example
Example 1: python discord bot command permissions
@commands.guild_only()
# Command cannot be used in private messages.
@commands.is_owner()
# Command can only be used by the bot owner.
@commands.is_nsfw()
# Command can only be used in NSFW channels
@commands.has_role("name")
# Check if member has a role with the name "name"
@commands.bot_has_role(11132312313213)
# As above, but for the bot itself. (name can be replaced with id)
@commands.has_any_role(["role1","foo",11132312313213])
# Check if user has any of the roles with the names "role1", "foo", or the role with id 11132312313213
@commands.bot_has_any_role(*roles)
# As above, but for the bot itself
@commands.has_permissions([ban_members=True, kick_members=True])
# Check if user has all of the passed permissions
# e.g. this command will require both kick and ban permissions
@commands.bot_has_permissions(**perms)
# As above, but for the bot itself.
@commands.has_guild_permissions(**perms)
@commands.bot_has_guild_permissions(**perms)
# As for the two above, but for guild permissions rather than channel permissions.
@commands.check(myfunction)
# Check against your own function that returns those able to use your command
@commands.check_any(*myfunctions)
# Command will be ran if the conditions of any of your own check functions are met
from discord.ext.commands.cooldowns import BucketType
# BucketType can be BucketType.default, member, user, guild, role, or channel
@commands.cooldown(rate,per,BucketType)
# Limit how often a command can be used, (num per, seconds, BucketType)
@commands.max_concurrency(number, per=BucketType.default, *, wait=False)
# Limit how many instances of the command can be running at the same time.
# Setting wait=True will queue up additional commands. False will raise MaxConcurrencyReached
# Checks can be stacked, and will Raise a CheckFailure if any check fails.
Example 2: discord.py
# Discord.py is a API wrapper for python.
Docs = "https://discordpy.readthedocs.io/en/latest/"
PyPI = "pip install -U discord.py"
# --- A simple bot ---
import discord
from discord.ext import commands
client = commands.Bot(comand_prefix='bot prefix here') # You can choose your own prefix here
@client.event()
async def on_ready(): # When the bot starts
print(f"Bot online and logged in as {client.user}")
# A simple command
@client.command(aliases=["ms", "aliases!"]) # You make make the command respond to other commands too
async def ping(ctx, a_variable): # a_variable is a parameter you use in the command
await ctx.send(f"Pong! {round(client.latency * 1000)}ms. Your input was {a_variable}")
client.run('your token here') # Running the bot
Example 3: discord.py fetch channel
await client.fetch_channel(channelId)
Example 4: python discord
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')
client = MyClient()
client.run('token')