Examples
The following section outlines some helpful Examples to use DCActivity module.
Client
from discord import Client, VoiceChannel
from dcactivity import DCActivity, DCApplication
client = Client()
dcactivity = DCActivity(client)
@client.event
async def on_message(message):
if message.content.startswith("!youtube"):
invite = await dcactivity.create_invite(message.author.voice.channel, DCApplication.youtube)
await message.channel.send(invite)
client.run("token")
Bot
from discord import Intents, VoiceChannel
from discord.ext.commands import Bot
from dcactivity import DCActivity, DCApplication
bot = Bot(command_prefix="!", intents=Intents.default())
dcactivity = DCActivity(bot) # or bot.dcactivity = DCActivity(bot) to use it as a BotVar
@client.command()
async def youtube(ctx, channel: VoiceChannel):
invite = await dcactivity.create_invite(channel, DCApplication.youtube)
await ctx.send(invite)
bot.run("token")
Advanced Usage
Bot with Cogs
- bot.py:
from discord import Intents
from discord.ext import commands
from dcactivity import DCActivity, DCApplication
class Bot(commands.Bot):
def __init__(self):
super().__init__(command_prefix="!", intents=Intents.default())
self.bot.dcactivity = DCActivity(self)
bot = Bot()
bot.run("token")
- cogs.py:
from discord import VoiceChannel
from discord.ext import commands
from dcactivity import DCApplication
from dcactivity.errors import InvalidChannel
class MyCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def youtube(self, ctx, channel=None):
if not channel:
if not ctx.author.voice:
return await ctx.send("You need to connect to a voice channel first")
if not isinstance(ctx.author.voice.channel, VoiceChannel):
return await ctx.send("This feature is not supported in Stage Channels.")
_channel = ctx.author.voice.channel
else:
_channel = channel
invite = await self.bot.dcactivity.create_invite(
_channel, DCApplication.youtube, max_age=0, max_users=10)
await ctx.send(invite)
@youtube.error
async def youtube_error(self, ctx, exc):
exc = getattr(exc, "original", exc)
if isinstance(exc, InvalidChannel):
await ctx.send("Invalid Channel given as argument.")
def setup(bot):
bot.add_cog(MyCog(bot))