User's avatar discord.py 2.0 - discord.py

How to get member's avatar in discord.py v2?
Before, it was like this:
#client.command(aliases=["av"])
async def avatar(ctx, *, avamember : discord.Member=None):
userAvatarUrl = avamember.avatar_url
embed2 = discord.Embed(title=f"{avamember}")
embed2.set_image(url=userAvatarUrl)
await ctx.send(embed=embed2)
Error I got:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Member' object has no attribute 'avatar_url'
Thanks!

You need to replace avamember.avatar_url with avamember.avatar.url.
Here's the link to the 2.0 docs if you're interested:
https://discordpy.readthedocs.io/en/master/api.html#discord.Asset

Related

Discord.py: Guild object has no attribute create_scheduled_event

Discord.py is handy to automate things and I am trying to create a scheduled discord event by command. But it seems that I cannot call the function create_scheduled_event of the Guild object while other functions work fine.
For example, the function fetch_roles() works fine and returns the roles of my guild.
But calling the function create_scheduled_event raises the error 'Guild' object has no attribute 'create_scheduled_event'.
Does anybody have an idea what I am missing, here?
Thanks in advance!
Here's the code
import discord
client = discord.Client()
#client.event
async def on_message(message):
# ...
if message.content.startswith('!newevent'):
myguild = client.guilds[0]
roles = await myguild.fetch_roles()
print(roles)
newevt = await myguild.create_scheduled_event(...)
client.run(TOKEN)

Adding a single role to all server members

So I am trying to make the bot give all the server members a single role. Code:
#commands.command(pass_context = True)
async def test(self, ctx, role: discord.Role):
for user in ctx.guild.members:
await user.add_roles(role)
print(f'{user.name} {role.name}')
However, it only printsOdysea(the name of my bot) test role And only gives the role to itself.
Any ideas how to fix this?
By the comment section you can do like this.
Ps. Im using client variable instead bot, that's up to you
In main file
client = commands.Bot(command_prefix=[your_prefix], intents=discord.Intents().all())
In cogs
#commands.command()
async def test(self, ctx, role:discord.Role):
for member in ctx.guild.members:
try:
await member.add_role(role)
except:
pass
print(f"{member.name}, {role.name}")

Uncaught TypeError: strapi.query is not a function

I'm trying to get the value of a field from a single type collection in the home page of a strapi plugin.
const test = strapi.query('global-settings').find({},{'externalUrl':1})
It's returning me an Uncaught TypeError: strapi.query is not a function
Any idea why?
I am not sure about the last part of your request.
But remember that those requests are asynchronous, so try adding await to the call.
This is an example of a query I am using.
const user_settings = await strapi.query('user-settings').find({Send_Monthly_Email: true})
So, that's why I don't understand why you have an empty parameter on that query. >> find({},{'externalUrl':1})
I am assuming you are inside a controller or inside a service.

Flask.session keyError

I'm having a KeyError issue when trying to access something from flask.session and I'm questioning my implementation.
Essentially, I have a PUT request that looks something like this
def auth():
flask.session["access"] = "Admin"
blueprint.before_request(auth)
def put(...):
...
if flask.session["access"] == "Admin":
do_something_cool()
I'm getting a KeyError issue here and I suspect does it have something to do with the usage of blueprint?
Thanks
flask.session is not available outside of a request context. You are missing a decorator to register your view:
import flask
bp = flask.Blueprint('auth', 'auth')
#bp.before_request
def auth(): flask.session['access'] = 'Admin'
#bp.route('/something')
def put():
if flask.session['access'] == 'Admin': do_something_cool()

django error: unexpected keyword argument 'widget'

I want to display a field (named 'icon') as radio button.
I created method callback in order to display DateTimeFields with JQuery. The code following should do it, however i get this error when i run my server:
Error when calling the metaclass bases
make_custom_datefield() got an unexpected keyword argument 'widget'
...
Exception Location: Virtualenvs/django/local/lib/python2.7/site-packages/django/forms/models.py in fields_for_model, line 164
forms.py:
def make_custom_datefield(f):
formfield = f.formfield()
if isinstance(f, DateTimeField):
formfield.widget.format = '%m/%d/%Y'
formfield.widget.attrs.update({'class':'datetimePicker', 'readonly':'true'})
return formfield
class FlashForm(forms.ModelForm):
formfield_callback = make_custom_datefield
class Meta:
model = Flash
exclude=('user','marker','address')
widgets = {'icon': forms.RadioSelect(), }
Can you please help me, i have really no clue how to solve this !
Thanks
Eventually, i found the answer: i had to add **kwargs parameter.
def make_custom_datefield(f,**kwargs):
formfield = f.formfield(**kwargs)
if isinstance(f, DateTimeField):
formfield.widget.format = '%m/%d/%Y'
formfield.widget.attrs.update({'class':'datetimePicker', 'readonly':'true'})
return formfield

Resources