Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions how-to-make-a-discord-bot-python/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DISCORD_TOKEN={your-bot-token}
DISCORD_GUILD={your-guild-name}
18 changes: 18 additions & 0 deletions how-to-make-a-discord-bot-python/01_connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")

intents = discord.Intents.default()
client = discord.Client(intents=intents)


@client.event
async def on_ready():
print(f"{client.user} has connected to Discord!")


client.run(TOKEN)
26 changes: 26 additions & 0 deletions how-to-make-a-discord-bot-python/02_guild_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
GUILD = os.getenv("DISCORD_GUILD")

intents = discord.Intents.default()
client = discord.Client(intents=intents)


@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break

print(
f"{client.user} is connected to the following guild:\n"
f"{guild.name}(id: {guild.id})"
)


client.run(TOKEN)
30 changes: 30 additions & 0 deletions how-to-make-a-discord-bot-python/03_guild_members.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
GUILD = os.getenv("DISCORD_GUILD")

intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)


@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break

print(
f"{client.user} is connected to the following guild:\n"
f"{guild.name}(id: {guild.id})\n"
)

members = "\n - ".join([member.name for member in guild.members])
print(f"Guild Members:\n - {members}")


client.run(TOKEN)
23 changes: 23 additions & 0 deletions how-to-make-a-discord-bot-python/04_utils_find.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
GUILD = os.getenv("DISCORD_GUILD")

intents = discord.Intents.default()
client = discord.Client(intents=intents)


@client.event
async def on_ready():
guild = discord.utils.find(lambda g: g.name == GUILD, client.guilds)
print(
f"{client.user} is connected to the following guild:\n"
f"{guild.name}(id: {guild.id})"
)


client.run(TOKEN)
23 changes: 23 additions & 0 deletions how-to-make-a-discord-bot-python/05_utils_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
GUILD = os.getenv("DISCORD_GUILD")

intents = discord.Intents.default()
client = discord.Client(intents=intents)


@client.event
async def on_ready():
guild = discord.utils.get(client.guilds, name=GUILD)
print(
f"{client.user} is connected to the following guild:\n"
f"{guild.name}(id: {guild.id})"
)


client.run(TOKEN)
17 changes: 17 additions & 0 deletions how-to-make-a-discord-bot-python/06_custom_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")


class CustomClient(discord.Client):
async def on_ready(self):
print(f"{self.user} has connected to Discord!")


intents = discord.Intents.default()
client = CustomClient(intents=intents)
client.run(TOKEN)
27 changes: 27 additions & 0 deletions how-to-make-a-discord-bot-python/07_welcome_members.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")

intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)


@client.event
async def on_ready():
print(f"{client.user.name} has connected to Discord!")


@client.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f"Hi {member.name}, welcome to my Discord server!"
)


client.run(TOKEN)
48 changes: 48 additions & 0 deletions how-to-make-a-discord-bot-python/08_respond_to_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import random

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")

intents = discord.Intents.default()
intents.members = True
intents.message_content = True
client = discord.Client(intents=intents)


@client.event
async def on_ready():
print(f"{client.user.name} has connected to Discord!")


@client.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f"Hi {member.name}, welcome to my Discord server!"
)


@client.event
async def on_message(message):
if message.author == client.user:
return

brooklyn_99_quotes = [
"I'm the human form of the 💯 emoji.",
"Bingpot!",
(
"Cool. Cool cool cool cool cool cool cool, "
"no doubt no doubt no doubt no doubt."
),
]

if message.content == "99!":
response = random.choice(brooklyn_99_quotes)
await message.channel.send(response)


client.run(TOKEN)
50 changes: 50 additions & 0 deletions how-to-make-a-discord-bot-python/09_raise_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
import random

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")

intents = discord.Intents.default()
intents.members = True
intents.message_content = True
client = discord.Client(intents=intents)


@client.event
async def on_ready():
print(f"{client.user.name} has connected to Discord!")


@client.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f"Hi {member.name}, welcome to my Discord server!"
)


@client.event
async def on_message(message):
if message.author == client.user:
return

brooklyn_99_quotes = [
"I'm the human form of the 💯 emoji.",
"Bingpot!",
(
"Cool. Cool cool cool cool cool cool cool, "
"no doubt no doubt no doubt no doubt."
),
]

if message.content == "99!":
response = random.choice(brooklyn_99_quotes)
await message.channel.send(response)
elif message.content == "raise-exception":
raise discord.DiscordException


client.run(TOKEN)
59 changes: 59 additions & 0 deletions how-to-make-a-discord-bot-python/10_handle_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
import random

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")

intents = discord.Intents.default()
intents.members = True
intents.message_content = True
client = discord.Client(intents=intents)


@client.event
async def on_ready():
print(f"{client.user.name} has connected to Discord!")


@client.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f"Hi {member.name}, welcome to my Discord server!"
)


@client.event
async def on_message(message):
if message.author == client.user:
return

brooklyn_99_quotes = [
"I'm the human form of the 💯 emoji.",
"Bingpot!",
(
"Cool. Cool cool cool cool cool cool cool, "
"no doubt no doubt no doubt no doubt."
),
]

if message.content == "99!":
response = random.choice(brooklyn_99_quotes)
await message.channel.send(response)
elif message.content == "raise-exception":
raise discord.DiscordException


@client.event
async def on_error(event, *args, **kwargs):
with open("err.log", "a") as f:
if event == "on_message":
f.write(f"Unhandled message: {args[0]}\n")
else:
raise


client.run(TOKEN)
22 changes: 22 additions & 0 deletions how-to-make-a-discord-bot-python/11_bot_connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
from dotenv import load_dotenv

# 1
import discord
from discord.ext import commands

load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")

# 2
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)


@bot.event
async def on_ready():
print(f"{bot.user.name} has connected to Discord!")


bot.run(TOKEN)
40 changes: 40 additions & 0 deletions how-to-make-a-discord-bot-python/12_bot_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import random

import discord
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)


@bot.command(name="99", help="Responds with a random quote from Brooklyn 99")
async def nine_nine(ctx):
brooklyn_99_quotes = [
"I'm the human form of the 💯 emoji.",
"Bingpot!",
(
"Cool. Cool cool cool cool cool cool cool, "
"no doubt no doubt no doubt no doubt."
),
]

response = random.choice(brooklyn_99_quotes)
await ctx.send(response)


@bot.command(name="roll_dice", help="Simulates rolling dice.")
async def roll(ctx, number_of_dice: int, number_of_sides: int):
dice = [
str(random.choice(range(1, number_of_sides + 1)))
for _ in range(number_of_dice)
]
await ctx.send(", ".join(dice))


bot.run(TOKEN)
Loading
Loading