An 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.
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
Built Distribution
Close
Hashes for pycord-cogsbyserver-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0235806280d3d9024c1fc61d4583f99a6ca3f92a187d42e30ca8e5b4b3813ec |
|
MD5 | b25100a78468b3daeaa925469ae16372 |
|
BLAKE2b-256 | a210697331b697b9d3f68d44b8b01785b31131ca9e29db346498623571b8452c |
Close
Hashes for pycord_cogsbyserver-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbb66584590d606bfb9d2ff73c502cc048c3037f8ecd9c76fd4c99e2128d1cd5 |
|
MD5 | 0dd37515e4a83e1b4198560668a8d653 |
|
BLAKE2b-256 | 5ed3be9946030c3799e456b20ea1d26e0c6ef70babee7ca9a660941c3130dcf1 |