How to get role id in discord.py - discord.py

I was creating a createrole command and I wanted to show some info about the role, such as the name, color, and id, but how did I get the role id? I tried looking on this site, Reddit, and the API reference but I couldn't find an answer.
This is what I have now:
#bot.command(name='createrole')
async def createrole(ctx, *, content):
guild = ctx.guild
await guild.create_role(name=content)
role = get(ctx.guild.roles, name='content')
roleid = role.id
description = f'''
**Name:** <#{roleid}>
**Created by:** {ctx.author.mention}
'''
embed = discord.Embed(name='New role created', description=description)
await ctx.send(content=None, embed=embed)

Quick Fix:
Right now you are passing the string 'content' to your get function instead of the variable with the name content. Try replacing the following line:
role = get(ctx.guild.roles, name='content')
with this:
role = get(ctx.guild.roles, name=content)
Much more efficient and less error prone way of doing it:
await guild.create_role returns the role object created, meaning you don't need to refetch it by name and can just do this:
#bot.command(name='createrole')
async def createrole(ctx, *, content):
guild = ctx.guild
role = await guild.create_role(name=content)
roleid = role.id
description = f'''
**Name:** <#{roleid}>
**Created by:** {ctx.author.mention}
'''
embed = discord.Embed(name='New role created', description=description)
await ctx.send(content=None, embed=embed)

Related

How can i put the title of the meme and the upvotes and the link to the meme in the embed discord.py

#client.command(pass_context=True)
async def meme(ctx):
embed = discord.Embed(title="", description="")
async with aiohttp.ClientSession() as cs:
async with cs.get('https://www.reddit.com/r/dankmemes/new.json?sort=hot') as r:
res = await r.json()
embed.set_image(url=res['data']['children'] [random.randint(0, 25)]['data']['url'])
await ctx.send(embed=embed)
so that is my meme command rn i need help to put the title and amount of upvotes in the embed
You can set those attributes via
embed.title = "<your-title>"
embed.url = "https://<your-url>"
And for the upvotes you could add a field
embed.add_field(name = "Upvotes", value = "<your upvotes>")

Giving role based on role ID

I am trying to give roles based on the ID of the role to users in a guild once they join however, cannot seem to get it to give when I provide the role ID and I can when I provide the role name.
I've tried both
#Nao.event
async def on_member_join(member):
database = mysql.connector.connect(
host='localhost',
user='root',
passwd='',
database='Nao'
)
cursor = database.cursor()
query = "SELECT role FROM autorole WHERE server = %s"
values = (member.guild.id,)
cursor.execute(query, values)
result = cursor.fetchall()
for role in result:
role = discord.utils.get(member.guild.roles, name='Members')
await member.add_roles(role)
Which works and
#Nao.event
async def on_member_join(member):
database = mysql.connector.connect(
host='localhost',
user='root',
passwd='',
database='Nao'
)
cursor = database.cursor()
query = "SELECT role FROM autorole WHERE server = %s"
values = (member.guild.id,)
cursor.execute(query, values)
result = cursor.fetchall()
for role in result:
role = discord.utils.get(member.guild.roles, id=role[0])
await member.add_roles(role)
which does not work and gives me the error;
await req(guild_id, user_id, role.id, reason=reason) AttributeError: 'NoneType' object has no attribute 'id'

Making commands so only certain servers can do it

I am wanting only certain servers to be able to use this feature shown below.
I want to know if there is a way to make it so only certain server ids can use this command.
#bot.command(pass_context = True)
async def shittymeme(ctx):
api = 'https://api.imgflip.com/get_memes'
response = requests.get(api)
url = random.choice(response.json()['data']['memes'])
url = url['url']
await bot.say( embed = discord.Embed(color = 0x0072ff, title = "Here you go").set_image(url = url))
Assuming you have a whitelist of server ids, you can take advantage of the Checks feature built into the commands extension.
whitelist = ["id1", "id2"]
def is_in_server_list(server_list):
def predicate(ctx):
return ctx.message.server.id in server_list
return commands.check(predicate)
#bot.command(pass_context = True)
#is_in_server_list(whitelist)
async def shittymeme(ctx):
api = 'https://api.imgflip.com/get_memes'
response = requests.get(api)
url = random.choice(response.json()['data']['memes'])
url = url['url']
await bot.say( embed = discord.Embed(color = 0x0072ff, title = "Here you go").set_image(url = url))

I can't login with WebSecurity.Login and I get WebSecurity.CurrentUserId as -1.

I am trying to register a user and place his order in same action for unregistered users. Please see problematic part of my code below. I get WebSecurity.CurrentUserId as -1 here. Therefore I cant find users and add user info to order. What is wrong here. It should be Id of newly registered user. Thanks for your helps.
WebSecurity.CreateUserAndAccount(UpperEmail, user.Password, new { user.Password, user.PasswordConfirm, user.Email, user.Name }, false);
int userId = db.Users.FirstOrDefault(u => u.Email == user.Email).Id;
if (!Roles.RoleExists("User"))
Roles.CreateRole("User");
Roles.AddUserToRole(UpperEmail, "User");
WebSecurity.Login(UpperEmail, user.Password);
neworder.user = db.Users.Find(WebSecurity.CurrentUserId);
neworder.UserId = WebSecurity.CurrentUserId;
neworder.PaymentStatus = PaymentStatus.Onaylandi;I

Unable to create a social account with python-social-auth and django-rest-framework when auth token is not in header

I am using python-social-auth and django-rest-framework to allow users signup with their social accounts in my app.
my case is similar to this post
Users are not created after login with facebook (probably an homonymy case)
I use django-rest-framework's TokenAuthentication to authenticate users
When using my custom user model, when an authtoken is available in the header (user is logged in) the social user account created is linked to the user whose Token is attached to the request as expected, but when there is no token available, it fails to create a new account.
But when using the default django user model, everything works as expected. I suspect that I am not configuring my custom user model properly.
Below is my settings file can you please look and see if you can find out what I am doing wrong.
Thanks in advance.
My settings file
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticatedOrReadOnly',
),
'PAGINATE_BY': 10
}
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.tz',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.request',
'social.apps.django_app.context_processors.backends',
)
SOCIAL_AUTH_FACEBOOK_KEY = os.environ.get('FB_APP_ID')
SOCIAL_AUTH_FACEBOOK_SECRET = os.environ.get('FB_APP_SECRET')
SOCIAL_AUTH_FACEBOOK_EXTENDED_PERMISSIONS = ['email']
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_USER_MODEL = 'users.FLUser'
SOCIAL_AUTH_TWITTER_KEY = os.environ.get('TWITTER_APP_ID')
SOCIAL_AUTH_TWITTER_SECRET = os.environ.get('TWITTER_APP_SECRET')
AUTHENTICATION_BACKENDS = (
'social.backends.twitter.TwitterOAuth',
'social.backends.facebook.FacebookOAuth2',
'social.backends.facebook.Facebook2OAuth2',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.user.get_username',
'social.pipeline.social_auth.associate_by_email',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details'
# 'apps.users.pipeline.user_details',
)
Custom Manager
class FLUserManager(BaseUserManager):
def _create_user(self, username, email, password,
is_staff, is_superuser, **extra_fields):
"""
Creates and saves a User with the given username, email and password.
"""
now = timezone.now()
if not email:
raise ValueError('The given username must be set')
email = self.normalize_email(email)
user = self.model(username=username, email=email,
is_staff=is_staff, is_active=True,
is_superuser=is_superuser, last_login=now,
date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, username, email=None, password=None, **extra_fields):
return self._create_user(username, email, password, False, False,
**extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
return self._create_user(username, email, password, True, True,
**extra_fields)
Custom User Model
class FLUser(AbstractBaseUser, PermissionsMixin):
GENDER = (
('M', 'Male'),
('F', 'Female'),
)
email = models.EmailField(
verbose_name=_('email address'),
max_length=255,
unique=True,
)
username = models.CharField(
verbose_name=_('username'),
max_length=30,
unique=False,
help_text=_('Required. 30 characters or fewer. Letters, numbers and '
'#/./+/-/_ characters'),
validators=[
validators.RegexValidator(re.compile('^[\w.#+-]+$'),
_('Enter a valid username.'), 'invalid')
]
)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
birthday = models.DateTimeField(
blank=True,
default=None,
null=True
)
gender = models.CharField(
max_length=1,
choices=GENDER,
default=None,
null=True
)
avatar = models.ImageField(
upload_to='media/avatars',
blank=True,
default=None,
null=True
)
sm_avatar = models.URLField(
blank=True,
default=None,
null=True,
verbose_name=_('Social Media Avatar')
)
is_active = models.BooleanField(
_('active'),
default=True,
help_text=_('Designates whether this user should be treated as '
'active. Unselect this instead of deleting accounts.')
)
is_admin = models.BooleanField(default=False)
objects = FLUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name', 'username',]
def is_authenticated(self):
return True
def get_full_name(self):
return '''{} {}'''.format(self.first_name, self.last_name)
def get_short_name(self):
return self.first_name
#property
def is_staff(self):
return self.is_admin
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
# On Python 3: def __str__(self):
def __unicode__(self):
return self.email
def email_user(self, subject, message, from_email=None):
"""
Sends an email to this User.
"""
send_mail(subject, message, from_email, [self.email])
class Meta:
ordering = ('id', 'first_name',)
verbose_name = _('user')
verbose_name_plural = _('users')
#receiver(post_save, sender=FLUser)
def create_auth_token(sender, instance=None, created=False, **kwargs):
if created:
Token.objects.create(user=instance)
The View
#api_view(['POST'])
#permission_classes((AllowAny,))
#strategy()
def register_by_access_token(request, backend):
backend = request.strategy.backend
if backend.name == 'twitter':
token = {
'oauth_token': request.DATA.get('access_token'),
'oauth_token_secret': os.environ.get('TWITTER_APP_OAUTH_SECRET'),
}
elif backend.name == 'facebook':
token = request.POST.get('access_token')
else:
raise Response('Wrong backend type', status=HTTP_400_BAD_REQUEST)
user = backend.do_auth(
access_token=token,
user=request.user.is_authenticated() and request.user or None
)
if user and user.is_active:
login(request, user)
user = UserAuthSerializer(user)
return Response(user.data, status=HTTP_200_OK)
else:
return Response({'detail': 'Unable to authenticate user'}, status=HTTP_400_BAD_REQUEST)
not sure if this matters at all, but i think your inheritance is a little wrong for your custom user model (mixins come before):
class FLUser(PermissionsMixin, AbstractBaseUser):
....

Resources