Discord Bot - Give roles after Message, Python - python

I am trying to make a simple discord bot, that gives a role to a user after he gives the bot a certain command
On the command !role the user should get a role called Beta
I first tried this:
from discord_webhook import DiscordWebhook, DiscordEmbed
import discord
from discord.ext.commands import Bot
from discord.ext import commands
client = commands.Bot(command_prefix = "!")
Client = discord.Client()
#client.event
async def on_message(message):
member = message.author
if member.bot:
return
if message.attachments:
return
print(message.content)
print(str(message.author))
if "role" in message.content:
embed=discord.Embed(title="Giving role.", color=0x00ff40)
await message.channel.send(message.channel, embed=embed)
role = discord.utils.get(server.roles, name="Beta")
await client.add_roles(message.author, role)
client.run("BOT TOKEN")
But I always get the following problem:
AttributeError: 'list' object has no attribute 'roles'
Thanks a lot for taking time to read this and if you can help me. Thanks

When you do:
role = discord.utils.get(server.roles, name="Beta")
You have to use
message.guild.roles
instead of:
server.roles
to access the list of roles, newer discord versions use guild instead of server to avoid confusion with voice servers.

Related

'Member' has no attribute 'server'

In the given code below I was trying to give a role to the person who reacts to a message by using any emoji but this code throws me an error saying that 'Member' has no attribute 'server' what should I do now?
import discord
from discord.ext import commands
import random
client = discord.Client()
#client.event
async def on_ready():
print('ready')
#client.event
async def on_reaction_add(reaction, user):
channel = reaction.message.channel
await channel.send(f'{user.name} has reacted by using {reaction.emoji} emoji, his message was {reaction.message.content}')
role = discord.utils.get(user.server.roles, name = 'Bot')
await client.add_roles(user, role)
client.run('TOKEN')
First off take a look at the documentation. It'll tell you everything you need to know about what has what attributes. Also, server is called guild in discord.py. So use user.guild.roles instead.

Discord Bot Python - Changing Bot's nickname

I'm trying to get the bot's name to change every time it runs. I've check the discord.py docs but nothing there has been of use and none of them throw up any errors. Any ideas?
Has perms 'admin' and 'change nickname'
import discord
from discord.ext import commands
from discord.ext.commands import bot
bot = commands.Bot(command_prefix=PREFIX)
bot.remove_command('help')
#bot.event
async def on_ready():
await bot.user.edit(nick="New Nickname")
bot.run(TOKEN)
You must have the member object of your bot to change your nickname as nicknames are done in guild's. Then you must edit it.
#bot.event
async def on_ready():
for guild in bot.guilds:
await guild.me.edit(nick="new nickname")

How do I make a Discord bot give me the role I want?

As I said in the title I wanted to make my bot give me any rank I want.
I tried this and it doesn't work becouse of Unresolved attribute reference 'add_roles' for class 'Client'.
import discord
from discord.utils import get
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == '$rank':
role = get(message.server.roles, name='Człek')
await client.add_roles(message.author, role)
From the docs, Client doesn't have the method add_roles, but Member does.
Try await message.author.add_roles(role)

Discord.py I am looking for make command to set role for everyone using the command

import discord
from discord.utils import get
client = commands.Bot(command_prefix="!")
#client.command(pass_context=True)
async def addrole(ctx):
user = ctx.message.author
role = discord.utils.get(user.server.roles, name="Open")
await ctx.add_roles(user, role)
but when I using this command . i getting this error (discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Member' object has no attribute 'server')
server is changed to guild and its Member.add_roles()
Below is the revised code:
import discord
from discord.utils import get
client = commands.Bot(command_prefix="!")
#client.command()
async def addrole(ctx):
role = get(ctx.guild.roles, name="Open")
await ctx.author.add_roles(role)

How to find users with a specific role in discord.py

I am trying to make a discord bot that finds users with a specific role so it can do something with this group of users. I have written code to list all of the members in a server but I don't think this will help.
import discord
#some code
client = discord.Client()
#some code
client.run("my token")
You can use the following, use it like this $get_members ROLENAME
ref for commands
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="$")
#bot.event
async def on_ready():
print("Logged in as")
print(bot.user.name)
print("------")
#bot.command()
async def get_members(ctx,role_name):
role = discord.utils.find(
lambda r: r.name == role_name, ctx.guild.roles)
for user in ctx.guild.members:
if role in user.roles:
await ctx.send(f"{user.mention} has the role {role.mention}")
bot.run("TOKEN")

Categories