how to implement pause command(lavalink)? - discord.py

I'm trying to make a pause command. I'm using discord.py and lavalink and this is the error that I get: 'DefaultPlayer' object has no attribute 'pause'
This is the code that I'm using:
class MusicCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.bot.music = lavalink.Client(self.bot.user.id)
self.bot.music.add_node('localhost', 7000, 'testing', 'na', 'music-node')
self.bot.add_listener(self.bot.music.voice_update_handler, 'on_socket_response')
self.bot.music.add_event_hook(self.track_hook)
commands.command(name='pause')
async def pause(self, ctx):
player = self.bot.music.player_manager.get(ctx.guild.id)
await player.pause()

player.pause() is from another object type.
You must use player.set_pause(True) in order to work. To unpause the playback you simply do player.set_pause(False)

Related

Can't figure out how to use discord.py ctx

I have tried to convert my discord bot's commands to using ctx instead of interactions but the code I have tried below does not work.
#bot.command(description="Says test")
async def test(ctx):
await ctx.send("Test")
My issue is that the command never loads.
Updated code but still broken
import discord
from discord import ui
from discord.ext import commands
bot = commands.Bot(command_prefix="/", intents = discord.Intents.all())
# Run code
#bot.event
async def on_ready():
print('The bot has sucuessfully logged in: {0.user}'.format(bot))
await bot.change_presence(activity=discord.Activity(type = discord.ActivityType.listening, name = f"Chilling Music - {len(bot.guilds)}"))
# Commands
#bot.command(description="Says Hello")
async def hello(ctx):
await ctx.send(f"Hello {ctx.author.mention}!")
#bot.command(description="Plays music")
async def play(ctx, song_name : str):
await ctx.author.voice.channel.connect()
await ctx.send(f"Now playing: {song_name}")
#bot.command(description="The amount of servers the bot is in")
async def servercount(ctx):
await ctx.send(f"Chill Bot is in {len(bot.guilds)} discord servers!")
#bot.command(description="The bots ping")
async def ping(ctx):
await ctx.send(f"🏓Pong\nChill Bot's ping is\n{round(bot.latency * 1000)} ms")
#bot.command(description="Announcement command")
async def announce(ctx, message : str):
await ctx.send(message)
#bot.command(description="Support Invite")
async def support(ctx):
await ctx.send(f"{ctx.author.mention} Please join our server for support! [invite]", ephemeral=True)
#bot.command(description = "Syncs up commands")
async def sync(ctx):
await ctx.send("Synced commands!", ephemeral=True)
bot.run('')
Your commands don't work because you incorrectly created an on_message event handler.
By overwriting it and not calling Bot.process_commands(), the library no longer tries to parse incoming messages into commands.
The docs also show this in the Frequently Asked Questions (https://discordpy.readthedocs.io/en/stable/faq.html#why-does-on-message-make-my-commands-stop-working)
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message.
Also, don't auto-sync (you're doing it in on_ready). Make a message command and call it manually whenever you want to sync.

Apscheduler does not work with discord.py==2

I have updated the discord.py package from 1.7.3 to 2.1 and after that, triggers (for ex. interval) for apcheduler stopped working. The task is created but not called at all. Could someone tell me the reason for this and can it be fixed?
P.S. I updated the discord.py in order to use .is_timed_out method
scheduler = AsyncIOScheduler()
class ViewBot(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
intents.members = True
super().__init__(command_prefix=commands.when_mentioned_or("!"), intents=intents)
async def on_ready(self):
print(f"Logged in as {self.user} (ID: {self.user.id})")
print("------")
bot = ViewBot()
async def update_points():
print("HELP")
if __name__ == '__main__':
scheduler.start()
jobstore = RedisJobStore(jobs_key='bot.jobs', run_times_key='bot.run_times')
scheduler.add_jobstore(jobstore, alias='bot')
scheduler.add_job(update_points, "interval", minutes=1, jobstore='bot')
print(scheduler.print_jobs(jobstore='bot'))
bot.run(TOKEN)

on_member_join function not working in discord.py

I'm working on a bot and the on_member_join function isn't working when a user joins the server. My function is in a cog and is the only function that won't work,
def __init__(self, client):
self.client = client
#commands.Cog.listener()
async def on_ready(self):
#commands.Cog.listener()
async def on_member_join(self, member):
print('working')
I am assuming the indentation is just wrong while pasting here and not in the actual code.
If you haven't done this already, the new version of discord.py has something called intents. First you have to go to the developer portal and activate it. Then you have to add the intents to your code.
This will enable all the Intents:
intents = discord.Intents.all()
client = commands.Bot(command_prefix = '-', intents=intents)

How to use MessageType

so I am trying to make the bot do something when the premium_guild_subscription message comes. I have tried:
#bot.event
async def on_message(message):
MessageType = discord.MessageType
if MessageType == 'pins_add':
await message.channel.send('ITS WORKING!!!')
but its not working. The above code does nothing when the message comes. If I do message.MessageType then it says that message object has no attribute MessageType.
You need to use an instance of a class and compare that to see if it's the correct type, not comparing the class itself to a string.
#bot.event
async def on_message(message):
if message.type == discord.MessageType.pins_add:
await message.channel.send('ITS WORKING!!!')
A further example:
#bot.command()
async def test(ctx):
print(ctx.message.type == discord.MessageType.pins_add)
print(ctx.message.type == discord.MessageType.default)
Results in
!test
> False
> True
In your code, you're trying to use class. You have to call the init function from that class. So you need to access a MessageType type data from message.
According to API References, you can use message.type to access MessageType object. Then you can check if a message is pins_add type with message.type.pins_add.
#bot.event
async def on_message(message):
if message.type == discord.MessageType.pins_add:
await message.channel.send('ITS WORKING!!!')

Making say command bot owner only

I am trying to make my !say command only work for the bot owner. This is what I currently have
#bot.command(pass_context = True)
async def say(ctx, *args):
mesg = ' '.join(args)
await bot.delete_message(ctx.message)
return await bot.say(mesg)
The code works but I want to make it so only I (the bot owner) can run the command.
You can also use the decorator #is_owner().
#bot.command(pass_context = True)
#commands.is_owner()
async def say(ctx):
your code...
Try doing it this way by adding an if statement
#bot.command(pass_context=True)
async def say(ctx):
if ctx.message.author.id =='bot owner id':
then execute the following code
This is how I did it - do comment if this does not work as intended.
#client.command()
#commands.is_owner()
async def say(ctx, *, message):
await ctx.send(f"{message}")
Have fun with your coding! Sorry for the late reply, I was reading old questions and seeked for unsolved issues
(remade)
The documentation for check in the 1.0 docs has the below example (slightly modified.)
def user_is_me(ctx):
return ctx.message.author.id == "Your ID"
#bot.command(pass_context = True)
#commands.check(user_is_me)
async def say(ctx, *args):
mesg = ' '.join(args)
await bot.delete_message(ctx.message)
return await bot.say(mesg)
How to find your ID

Resources