Apscheduler does not work with discord.py==2 - discord.py

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)

Related

Cogs Not Working and not detected In Discord.py

My bot program can't detected cogs file
I have tried every way to solve it, but still nothing has changed
main.py
import discord
import os
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents)
async def load():
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
bot.run('MTA2NzQ1MDMxMTAzMzIzMzUxOA.GSdg-i.CW3X5T82VYGYjPqEPWZ8YQ6oDZni33T-Bi1Ovo')
lab.py
import discord
from discord.ext import commands
class lab(commands.Cog):
def __init__(self,bot):
self.bot = bot
#commands.Cog.listener()
async def on_ready(self):
print('lab.py is running')
#commands.command()
async def holla(ctx):
await ctx.reply('hai')
async def setup(bot):
await bot.add_cog(lab(bot))
eror
I need someone to help me solve my problem, thanks in advance
You're never calling that load() function... It's cool that you made it, but if you're not using it then it's not very surprising that your cogs aren't being loaded.
The recommended place to do this in is setup_hook. Subclass Client (or Bot depending on what you want) and override it.

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.

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)

discord bot python auto-role(help)

import discord
from discord.ext import commands
print(discord.__version__)
client = commands.Bot(command_prefix = '!')
#client.event
async def on_ready():
print('bot is online')
#client.event
async def on_member_join(member):
role = get(member.guild.roles, name="Members")
await member.add_roles(role)
print(f"{member} was given {role}")
client.run(removed)
what am I doing wrong here? I am trying to make an auto-join role when somebody joins it gives him a role
In version 1.5 intents were introduced which directly effect events like on__member_join. You would need to enable it in the Discord Developer Portal under bot in your application. Once that is enabled you would want to add this:
intents = discord.Intents.default()
intents.members = True

how to implement pause command(lavalink)?

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)

Resources