Skip to content

Instantly share code, notes, and snippets.

Nzg0OTk3MTkyMzM0ODM1NzEy.X8xbmw.eGM6T5AAQVPpqdOW-EzEU0h6ayI
@15696
15696 / lambda_bot.py
Last active December 6, 2020 19:52
A basic discord bot made entirely in a lambda. Featuring an on_ready event, a ping command, and the jishaku cog.
# thanks to mental32 on github for the idea
# thanks to sleep-cult#3040 on discord for the help
token = "put your token here"
(lambda c, a: [
f(c, a) for f in [
lambda c, a: [c.__setattr__(k, a.coroutine(v)) for k, v in {
"on_ready": lambda: print("Ready!"),
NzgyMzA1MTE4ODg4MDAxNTQ2.X8KQag.y4_7W7ffUC3m2pBj3JeOEdgM4t4
@15696
15696 / cogs.py
Last active May 3, 2024 12:59
simple cogs example in discord.py
# main.py
from discord.ext import commands
import os
client = commands.Bot(command_prefix = "!")
for f in os.listdir("./cogs"):
if f.endswith(".py"):
client.load_extension("cogs." + f[:-3])
@15696
15696 / member.py
Created November 2, 2020 17:41
simple member converter that defaults to ctx.author
class Member(commands.Converter):
async def convert(self, arg):
if arg == "":
return ctx.author
return discord.Member.convert(arg)
@15696
15696 / uktest.json
Created October 30, 2020 23:28
uktest.json
{"Version": "1.1", "URL": "https://www.icloud.com/shortcuts/2ed58291f7f84f3da7990379d8516164", "Notes": "Added stuff."}
@15696
15696 / wait.py
Created October 17, 2020 21:19
a simple example of: commands.Bot.wait_for
bot_msg = await ctx.send("what is 2 + 3?")
def check(message):
return message.channel == bot_msg.channel
try:
msg = bot.wait_for("message", check = check, timeout = 60) # timeout is 60 by default
except asyncio.exceptions.TimeoutError:
# user didnt reply in 60 seconds
else:
@15696
15696 / cooldowns.py
Created October 17, 2020 20:57
simple cooldowns and cooldown handlers in discord.py
@client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.send(f"{round(error.retry_after, 2)} seconds left")
@client.command()
@commands.cooldown(1, 5, commands.BucketType.user)
async def command(ctx):
await ctx.send("command output")
@15696
15696 / converters.py
Last active November 2, 2020 17:42
simple subreddit converter example in discord.py
class Subreddit(commands.Converter):
async def convert(self, arg):
if arg.startswith("r/"):
arg = arg[2:]
return arg
@client.command()
async def convert(ctx, subreddit: Subreddit): # use a typehint to specify converter
await ctx.send(subreddit)
@15696
15696 / customcontext.py
Last active May 29, 2023 05:51
simple custom context in discord.py (requires commands.Bot subclass)
# bot.py
from .context import CustomContext
class Bot(commands.Bot):
"""A subclass of commands.Bot, useful for creating custom context."""
async def get_context(self, message, *, cls = CustomContext):
return await super().get_context(message, cls = cls)
# context.py
class CustomContext(commands.Context):