I'm using discord.py to write a bot. I want it to add a role named 'unverified' to all members upon joining. I don't get any error but the role just doesn't get added for some reason
#client.event
async def on_member_join(member):
role_unverified = discord.utils.get(member.guild.roles, name="unverified")
await member.add_roles(role_unverified)
from discord.utils import get
#client.event
async def on_member_join(member):
role = get(member.guild.roles, name="unverified")
await member.add_roles(role)
this should work i think
aren't you retrieving the role from the member you're trying to assign it to?
Woulnd't it be something like
#client.event
async def on_member_join(member):
role_unverified = discord.utils.get(guild.roles, name="unverified")
await member.add_roles(role_unverified)
Related
What am I doing wrong?
#bot.event
async def on_member_join(member):
print(f"{member} join")
role_1 = member.guild.get_role(start_role_id)
await member.add_roles(role_1)
I searched for answers on the forums for a long time and nothing helped.
Did you enable intents in Discord Developer Portal? while initializing bot add intents=discord.Intents.all(). Also I fixed your code.
bot = commands.Bot(command_prefix='', intents=discord.Intents.all())
#bot.event
async def on_member_join(member):
print(f"{member} join")
role1 = discord.utils.get(member.server.roles, id=role_id)
await member.add_roles(role1)
Try this:
bot = commands.Bot(command_prefix='PREFIX_HERE',
intents=discord.Intents.all())
#bot.event
async def on_member_join(member):
print(f"{member} has joined")
my_role = discord.utils.get(member.guild.roles, id=role_id)
await member.add_roles(my_role)
I am using replit and this is my code so far:
TOKEN = os.environ['TOKEN']
client = discord.Client()
#client.event
async def on_ready():
print("bot online")
#client.event
async def on_message(message):
if message.content == "!Help":
embedVar = discord.Embed(title="Title", description="Desc", color=0x00ff00)
embedVar.add_field(name="Field1", value="hi", inline=False)
embedVar.add_field(name="Field2", value="hi2", inline=False)
await message.channel.send(message.channel, Embed=embed)
client.run(TOKEN)
It returns an error message saying "embed" is not defined. I just copied this of a site, after looking for an hour so I do not know much so if somebody could explain to me how to fix this and how use embeds id appreciate it. I think I am a version below 1 but im not sure.
You defined your embed as embedVar so you need to pass that in when you send the message. Also since you are already using message.channel.send, the message.channel argument will be useless.
await message.channel.send(embed=embedVar)
In the code below I am trying to give a role to a person whenever they react to my message but this code throws me an error saying that 'int' object has no attribute 'id' the code says that the problem is with this code: await user.add_roles(user.guild.id, user.id, role, reason='reason') how do I solve this issue?
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.guild.roles, name = 'Test_Bot')
await user.add_roles(user.guild.id, user.id, role, reason='reason')
client.run('TOKEN')
As Dominik said, you do not need user.guild.id, user.id if you say await user.add_roles. What is happening is that it is trying to use the user.id (an integer) you passed instead of the user object you use when calling the function to begin with: await user.add_roles(...).
So, get rid of await user.add_roles(user.guild.id, user.id, role, reason='reason'), and make it just: await user.add_roles(role).
Fixed code should be:
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.guild.roles, name = 'Test_Bot')
await user.add_roles(role, reason='reason')
client.run('TOKEN')
TL;DR: Discord.py expects the full member object when you use add_roles(). You give it the integer of the member's ID instead.
i want to count members in discord but how
using async
await message.channel.send(f"""# of Members: {id.member_count}""")
i try
#client.event
async def on_message(message):
#id = client.get_guild(ID)
if message.content.find("!hello") != -1:
await message.channel.send("Hi")
elif message.content == "!users":
await message.channel.send(f"""# of Members: {id.member_count}""")
i know this is copy code my code is
#bot.command()
async def countmember(ctx):
ctx.guild.members
len(ctx.guild.members)
await ctx.send(f""" of member: {id.member_count}""")
To get the amount of members from a guild can be retrieved with member_count, with this you also need to properly define guild or just simply using ctx.guild.member_count
This is an example in your command, I would also recommend to use a command instead of an on_message event to use as a command, there's just loss of benefits using that way.
#bot.command()
async def countmember(ctx):
guild = ctx.guild
await ctx.send(f"Member count: {guild.member_count}")
Even if you still wanted to get the guild count with an on_message, it can be done this way,
#bot.event
async def on_message(message):
guild = message.guild
if message.content.find("!hello") != -1:
await message.channel.send("Hi")
elif message.content == "!users":
await message.channel.send(f"""# of Members: {guild.member_count}""")
You have also used bot and client you should only be using one but i'd assume your code works
so I'm working on making my own bot for my server and after a while I finally found a string for autorole & role assignment that worked.
I then kept on adding another string for the bot simply replying "Hello". As soon as I add that the role commands won't work anymore. Once I take it out it works again.
On the other hand I have a 8ball and a dice roll command that works with and without the Hello Command
I have no idea what is the problem...
#client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name='entrance')
await channel.send(f'Welcome {member.mention} to Dreamy Castle! \n Please make sure to read the rules!')
role = discord.utils.get(member.guild.roles, name="Peasants")
await member.add_roles(role)
#client.event
async def on_message(message):
if message.content.startswith('+acceptrules'):
member = message.author
role1 = discord.utils.get(member.guild.roles, name='The People')
await member.add_roles(role1)
#client.event #this is the hello command
async def on_message(message):
message.content.lower()
if message.content.startswith('Hello Conny'):
await message.channel.send('Hello!')
Use if and elif not 2 different functions for same event.
Also you might need commands.Bot for a fully functional commanded bot.
#client.event
async def on_message(message):
if message.content.startswith('+acceptrules'):
member = message.author
role1 = discord.utils.get(member.guild.roles, name='The People')
await member.add_roles(role1)
elif message.content.lower().startswith("hello conny"):
await message.channel.send("Hello!")
await client.process_commands(message)