Discord.py rewrite 'Context' object has no attribute 'send' - heroku

Last month I hosted a Discord.py (async version) bot on heroku and it worked perfect, this month I updated my bot from async to rewrite and it doesn't really work.
On my requirements.txt file I have put the git+https://github.com/Rapptz/discord.py#rewrite#egg=discord.py[voice] but it doesn't work.
I always get this error:
2019-01-25T08:30:23.592293+00:00 app[worker.1]: Traceback (most recent call last):
2019-01-25T08:30:23.592356+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 846, in process_commands
2019-01-25T08:30:23.592358+00:00 app[worker.1]: yield from command.invoke(ctx)
2019-01-25T08:30:23.592363+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 374, in invoke
2019-01-25T08:30:23.592365+00:00 app[worker.1]: yield from injected(*ctx.args, **ctx.kwargs)
2019-01-25T08:30:23.592395+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 54, in wrapped
2019-01-25T08:30:23.592397+00:00 app[worker.1]: raise CommandInvokeError(e) from e
2019-01-25T08:30:23.592435+00:00 app[worker.1]: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Context' object has no attribute 'send'
Script:
print("Loading Script")
print("Loading Libs")
import discord
import random
from discord.ext import commands
print("Loading Bot")
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print("Online!")
#bot.command()
async def rps(ctx):
rpsa = ["Rock", "Paper", "Scissors"]
rpsr = random.choice(rpsa)
await ctx.send(rpsr)
bot.run(Token)

Are you sure you have the right version installed? Just having it in your requirements file doesn't guarantee that you are using the right one. Check what you have installed with pip freeze and then uninstall if necessary the old async version.

That type of error only occurs when you have the async version of discord.py installed and are using rewrite features
Double check what your requirements.txt file is called (you've named it requirement.txt in the question) and check that it doesn't still contain discord==0.0.2 or
discord.py==0.16.12

Related

Discord bot not running with heroku

I'm in the process of moving my discord bot from locally hosting to heroku and i seem to have everything down except for actually running it.
When I enable the python worker, it doesn't seem to bring my bot online.
I'm not sure why this would be the case. It's connected to my GitHub and the bot token is defined using the vars.
The build logs look normal but the app logs have some errors that I'm not sure how to fix, which I have to assume is the problem.
Application logs:
2022-04-15T05:10:52.792118+00:00 app[worker.1]: from .client import Client
2022-04-15T05:10:52.792127+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/discord/client.py", line 53, in <module>
2022-04-15T05:10:52.792226+00:00 app[worker.1]: from .webhook import Webhook
2022-04-15T05:10:52.792234+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/discord/webhook/__init__.py", line 12, in <module>
2022-04-15T05:10:52.792307+00:00 app[worker.1]: from .async_ import *
2022-04-15T05:10:52.792315+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/discord/webhook/async_.py", line 52, in <module>
2022-04-15T05:10:52.792382+00:00 app[worker.1]: from ..channel import PartialMessageable
2022-04-15T05:10:52.792405+00:00 app[worker.1]: ImportError: cannot import name 'PartialMessageable' from 'discord.channel' (/app/.heroku/python/lib/python3.10/site-packages/discord/channel.py)
2022-04-15T05:10:52.949575+00:00 heroku[worker.1]: Process exited with status 1
2022-04-15T05:10:53.184475+00:00 heroku[worker.1]: State changed from up to crashed
2022-04-15T06:01:19.686916+00:00 heroku[worker.1]: State changed from crashed to starting
2022-04-15T06:01:23.574488+00:00 heroku[worker.1]: Starting process with command `python3 bot.py`
2022-04-15T06:01:24.141886+00:00 heroku[worker.1]: State changed from starting to up
2022-04-15T06:01:25.439092+00:00 heroku[worker.1]: State changed from up to crashed
2022-04-15T06:01:25.319879+00:00 heroku[worker.1]: Process exited with status 1
2022-04-15T06:01:25.157617+00:00 app[worker.1]: Traceback (most recent call last):
2022-04-15T06:01:25.157650+00:00 app[worker.1]: File "/app/bot.py", line 1, in <module>
2022-04-15T06:01:25.157743+00:00 app[worker.1]: import discord
2022-04-15T06:01:25.157748+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/discord/__init__.py", line 25, in <module>
2022-04-15T06:01:25.157813+00:00 app[worker.1]: from .client import Client
2022-04-15T06:01:25.157822+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/discord/client.py", line 53, in <module>
2022-04-15T06:01:25.157898+00:00 app[worker.1]: from .webhook import Webhook
2022-04-15T06:01:25.157900+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/discord/webhook/__init__.py", line 12, in <module>
2022-04-15T06:01:25.157953+00:00 app[worker.1]: from .async_ import *
2022-04-15T06:01:25.157961+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/discord/webhook/async_.py", line 52, in <module>
2022-04-15T06:01:25.158026+00:00 app[worker.1]: from ..channel import PartialMessageable
2022-04-15T06:01:25.158049+00:00 app[worker.1]: ImportError: cannot import name 'PartialMessageable' from 'discord.channel' (/app/.heroku/python/lib/python3.10/site-packages/discord/channel.py)
My code is too long to put here but when I run it locally it works anyway, but if any specific code is needed I can reply with it.
Any help is appreciated, thanks!
Fixed by updating to discord.py v2.0.0a because for some reason when i installed it, i was on v1.7
if anyone needs the import code:
python3 -m pip install -U git+https://github.com/Rapptz/discord.py

Django Rest Framework AttributeError: module 'coreapi' has no attribute 'Client'

I am trying to run tests for api that worked before. Test.py looks like following:
from django.contrib.auth.models import User
from rest_framework import status
from rest_framework.test import APITestCase
class ApiUserTest(APITestCase):
"""
python manage.py test .\apps\api\api_user
API urls:
api_user/login/
api_user/logout/
api_user/register/
api_user/change_password/
"""
def setUp(self):
...
def test_user_register(self):
...
But I'm getting the following error
PS F:\ComputerShop> python manage.py test .\apps\api\api_user
System check identified no issues (0 silenced).
E
======================================================================
ERROR: api.api_user.tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: api.api_user.tests
Traceback (most recent call last):
File "C:\Users\Emil\AppData\Local\Programs\Python\Python38\lib\unittest\loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "C:\Users\Emil\AppData\Local\Programs\Python\Python38\lib\unittest\loader.py", line 377, in _get_module_from_name
__import__(name)
File "F:\ComputerShop\apps\api\api_user\tests.py", line 3, in <module>
from rest_framework.test import APITestCase
File "F:\ComputerShop\env\lib\site-packages\rest_framework\test.py", line 123, in <module>
class CoreAPIClient(coreapi.Client):
AttributeError: module 'coreapi' has no attribute 'Client'
I also tried to run tests on a known working project, but they did not start there either.
I've tried to reinstalling the rest framework but it doesn't help. I use virtual environment in the project
To solve this problem you should install next packages:
pip install coreapi pyyaml

Getting error when running music bot, discord rewrite

I get an error in one of my cogs. I believe it is talking about the attributes guild not being in discord.client:
File "/Users/me/Desktop/Personal Coding/Music-bot-forward/bot.py", line 27, in <module>
bot.add_cog(Player(client))
File "/Users/me/Desktop/Personal Coding/Music-bot-forward/music.py", line 16, in __init__
self.setup()
File "/Users/me/Desktop/Personal Coding/Music-bot-forward/music.py", line 19, in setup
for guild in self.bot.guilds:
AttributeError: module 'discord.client' has no attribute 'guilds'
The tutorial:https://youtube.com/watch?v=46ZHJcNnPJ8&feature=share
The code:https://pastebin.com/abgvsD29
I am new to the Discord.py library and do not really understand the docs either
The reason being was in my code,await ctx.voice.channel.connect() You would need to replace that with await ctx.author.voice.channel.connect().

Slack API Socket Mode and asyncio

I'm writing some middle-ware in Python that uses Socket Mode. Non-async testing works fine. But the connect() call fails:
app_token='xapp-XXX'
bot_token='xoxb-XXX'
web_client = AsyncWebClient(bot_token)
sm_client = SocketModeClient(app_token=app_token, web_client=web_client)
sm_client.connect()
Returns:
Traceback (most recent call last):
File "wrapper.py", line 57, in <module>
sm_client.connect()
File "/Library/Python/3.8/site-packages/slack_sdk/socket_mode/builtin/client.py", line 132, in connect
self.wss_uri = self.issue_new_wss_url()
File "/Library/Python/3.8/site-packages/slack_sdk/socket_mode/client.py", line 48, in issue_new_wss_url
return response["url"]
TypeError: 'coroutine' object is not subscriptable
sys:1: RuntimeWarning: coroutine 'AsyncWebClient.apps_connections_open' was never awaited
I can't find anything in the API or SDK documentation about how to use AsyncWebClient in a Socket Mode application. Is this possible?
In your traceback, it is referencing the builtin client, aka the concurrent client. You're probably importing this:
from slack_sdk.socket_mode import SocketModeClient
You need to use the async client if you're using the AsyncWebClient:
from slack_sdk.socket_mode.aiohttp import SocketModeClient

Ubuntu 10.04 - Python multiprocessing - 'module' object has no attribute 'local' error

The following code is from the python 2.6 manual.
from multiprocessing import Process
import os
def info(title):
print(title)
print('module name:', 'me')
print('parent process:', os.getppid())
print('process id:', os.getpid())
def f(name):
info('function f')
print('hello', name)
if __name__ == '__main__':
info('main line')
p = Process(target=f, args=('bob',))
p.start()
p.join()
This creates the following stack traces:
Traceback (most recent call last):
File "threading.py", line 1, in <module>
from multiprocessing import Process
File "/usr/lib/python2.6/multiprocessing/__init__.py", line 64, in <module>
from multiprocessing.util import SUBDEBUG, SUBWARNING
File "/usr/lib/python2.6/multiprocessing/util.py", line 287, in <module>
class ForkAwareLocal(threading.local):
AttributeError: 'module' object has no attribute 'local'
Exception AttributeError: '_shutdown' in <module 'threading' from '/home/v0idnull/tmp/pythreads/threading.pyc'> ignored
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.6/multiprocessing/util.py", line 258, in _exit_function
info('process shutting down')
TypeError: 'NoneType' object is not callable
Error in sys.exitfunc:
Traceback (most recent call last):
File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.6/multiprocessing/util.py", line 258, in _exit_function
info('process shutting down')
TypeError: 'NoneType' object is not callable
I'm completely clueless as to WHY this is happening, and google has given me very little to work with.
that code runs fine on my machine:
Ubuntu 10.10, Python 2.6.6 64-bit.
but your error is actually because you have a file named 'threading.py' that you are running this code from (see the stack-trace details). this is causing a namespace mismatch, since the multiprocessing module needs the 'real' threading module. try renaming your file to something other than 'threading.py' and running it again.
also... the example you posted is not from the Python 2.6 docs... it is from the Python 3.x docs. make sure you are reading the docs for the version that matches what you are running.

Resources