Skip to main content

A pycord extension that adds cogs that have a different object for each server.

Project description

Pycord Cogs by Server

A simple addon to the pycord library that allows you to make cogs that exist as different objects between different servers. This is better explained by an example, so say you're making a simple music bot. Your cog might look something like this without the library...

import discord
from discord.ext import commands
class CogThing(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.queues = {}
        self.volumes = {}
    
    @discord.slash_command()
    async def add_to_queue(self, ctx, *, song_title):
        if ctx.guild not in self.queues:
            self.queues[ctx.guild] = []
        self.queues[ctx.guild].append(song_title)
        await ctx.respond("Song added")
    
    @discord.slash_command()
    async def change_volume(self, ctx, *, volume: int):
        self.volumes[ctx.guild] = volume
        await ctx.respond(f"Volume set to {volume}")
    
    async def play_song(self, vc):
        if vc.guild not in self.queues or len(self.queues[vc.guild])==0:
            return
        
        song = self.queues[vc.guild].pop(0)
        volume = 100 if vc.guild not in self.volumes else self.volumes[vc.guild]
        do_some_things_play_some_music(vc, song, volume)

...but with the library it can look like this!

import discord
from pycord_cogsbyserver import ServerCog
class CogThing(ServerCog):
    def __init__(self, bot):
        self.bot = bot
        self.queue = []
        self.volume = 100
    
    @ServerCog.slash_command()
    async def add_to_queue(self, ctx, *, song_title):
        self.queue.append(song_title)
        await ctx.respond("Song added")
    
    @ServerCog.slash_command()
    async def change_volume(self, ctx, *, volume:int):
        self.volume = volume
        await ctx.respond(f"Volume set to {volume}")
    
    async def play_song(self, vc):
        if len(self.queue) == 0:
            return
        song = self.queue.pop(0)
        do_some_things_play_some_music(vc, song, self.volume)

You can imagine how that would reduce complexity as the bot's functionality increases.

The library supports event listeners too, like so:

class CogThing(ServerCog):
    @ServerCog.listener()
    async def on_message(self, message):
        await message.channel.send("infinite loop time")

Those are sorted by guild as well, or into the special DMs instance if there's no one guild that makes sense. In the special case of on_ready, the event instead fires in all cogs, to allow general prep.

Release Notes/Changelog

Not a whole lot to say, really. I've done what I've done.

V0.3.1

MINOR SPELLING ERROR (changed summary from "An pycord..." to "A pycord...")

V0.3.0

Added documentation to literally everything relevant, and a lot of pretty irrelevant things too.

V0.2.0

Added basic slash command support

V0.1.0

First version, ayyyyy! Added basic library functionality in listeners.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycord-cogsbyserver-0.3.2.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycord_cogsbyserver-0.3.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file pycord-cogsbyserver-0.3.2.tar.gz.

File metadata

  • Download URL: pycord-cogsbyserver-0.3.2.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.4 Windows/10

File hashes

Hashes for pycord-cogsbyserver-0.3.2.tar.gz
Algorithm Hash digest
SHA256 8b2cb548c7a30a8dd946376b00b69aad60ccc4bc603b62b2c6279b5dd3421ead
MD5 286b84f113fc8f57abf9364ba9c89f13
BLAKE2b-256 1049b2f8dd3a3e63b4f5bed2c448e493de8b3584de23acf3c1fbac968532c9b4

See more details on using hashes here.

File details

Details for the file pycord_cogsbyserver-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pycord_cogsbyserver-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f8fc16b666951a45344c3b013b4d8e643f2c5c9ed125b64c4988cd66769ccbde
MD5 788fd320749e2993622854b476a12d7a
BLAKE2b-256 3178388ca34a2f13eb51a4cb117d73f8291b7c55e271e5ba51727b1b877f019e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page