#🔒 NameError: name 'ctx' is not defined

7 messages · Page 1 of 1 (latest)

undone nest
#

How can this be fixed?

import discord
from discord.ext import commands, tasks

class func2(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.loop_task2.start()

    @tasks.loop(seconds=210)
    async def loop_task2(self):
        ids = ['1263869253082157126', '772320334606368788']
        for i in range(len(ids)):
            user_id = ids[i]
            
            user = await ctx.bot.fetch_user(user_id)
            await user.send('Test message')

    @loop_task2.before_loop
    async def before_loop(self):
        await self.bot.wait_until_ready()


def setup(bot):
    bot.add_cog(func2(bot))

Error:

user = await ctx.bot.fetch_user(user_id)
                 ^^^
NameError: name 'ctx' is not defined
somber coralBOT
#

@undone nest

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

vital phoenix
#

This code doesn't need context. Context only is used when commands are being invoked.

Represents the context in which a command is being invoked under.

This class contains a lot of meta data to help you understand more about the invocation context. This class is not created manually and is instead passed around to commands as the first parameter.

This class implements the Messageable ABC.

Your code should work without it.

somber coralBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.