I've been trying to make a command that adds a role to the user when he/she speaks in a specific channel. Then the role mutes the user making it a one message channel.
Code:
#client.event
async def on_message(message):
if message.channel.name == '💻・message-archive':
await client.process_commands(message)
elif "" in message.content:
role = discord.utils.get(message.guild.roles, name="one message")
await message.author.add_roles(role)
await client.process_commands(message)
The problem is that it adds a role in ANY channel. I want it to be used only on 1 channel.
I've tried using if and else if but it didn't work.
Simply check if the name/ID of the channel is the correct one:
if channel.id == 182739817283172:
await message.author.add_roles(role)
if channel.name == "whatever":
await message.author.add_roles(role)
Related
I am trying to code a bot that sends a message with a reactable icon which you can click on it will give you a role, but if you un react if removes the role.
This is what I had, but it isn't practical.
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == 'bumper_role':
role = get(message.server.roles, name='bumper_role')
await client.add_roles(message.author, role)
replace role = get(message.server.roles, name='bumper_role')
with role = discord.utils.get(message.guild.roles, name='bumper_role').
Also replace await client.add_roles(message.author, role) with await message.author.add_roles(role)
Have a nice day :)
Trying to add role once someone has joined and got the "y" confirmation from someone in the same channel
Not sure if I can add another async in the middle...
Sorry if it is a very obvious question but most answers I got online were quite outdated and the docs didn't answer all my questions
This is my attempt
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = discord.ext.commands.Bot(command_prefix="$", intents=intents)
#bot.event
async def on_member_join(member):
await channel.send("{} has just joined. \nDo you know them (y/n)?".format(member.name))
print("someone joined")
async def on_message(message):
if "y" in message.content:
role = get(message.guild.roles, name="Hey i know you")
await member.add_roles(role, atomic=True)
await message.channel.send("added Hey i know you to {}".format(member.name))
One way of doing this is by using the wait_for function.
#client.event
async def on_member_join(member):
channel = client.get_channel(your channel id)
guild = client.get_guild(your guild id)
await channel.send(f"{member.mention} joined!")
await asyncio.sleep(3)
await channel.send("{} has just joined. \nDo you know them (y/n)?".format(member.name))
msg = await client.wait_for("message")
if msg.content == "y":
role = guild.get_role(your rold id)
await member.add_roles(role)
await channel.send("Added role!")
elif msg.content == "n":
await channel.send("i wont add the role!")
return
#client.command()
#commands.has_permissions(manage_messages=True)
async def mute(ctx, member: discord.Member):
try:
guild = ctx.guild
rolecreate = "MutedByCloudy"
await guild.create_role(rolecreate, colour=discord.Colour.red())
if member.guild_permissions > ctx.author.guild_permissions or member.guild_permissions.administrator:
await ctx.send("I can't mute that user. User probably has more perms than me or you")
else:
if member.id == 739424025205538817:
await ctx.send("Nice try. I can't mute myself.")
else:
role = get(member.guild.roles, name='MutedByCloudy')
await member.add_roles(role)
embed=discord.Embed(title="User Muted! :white_check_mark:", description=f"**{member}** was muted by **{ctx.message.author}**!", color=0x2f3136)
await ctx.send(embed=embed)
except:
await ctx.send(":x: Something happened. I don't know what.")
So i have this command that creates a role, and gives it to the user. But It doesnt create the role and doesnt throw any errors, can you guys help? The giving role part works but the creating role doesn't.
Well you aren't creating the role, you could try something like this:
role = discord.utils.get(ctx.guild.roles, name='MutedByCloudy')
if not role:
role = await ctx.guild.create_role(name='MutedByCloudy', reason='Role for muting, auto generated by Cloudy')
for channel in ctx.guild.channels:
await channel.set_permissions(role, send_messages=False,
read_message_history=False,
read_messages=False)
You could also add an try and except discord.Forbidden to handle permission errors
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)
I'm trying to make a discord bot that gives a user role when they join a voice channel and removes the role when they leave it. I know about on_voice_state_update and how to give a user a role but I don't know how to get what user joined the channel to give them the role.
Right now my code is slightly modified version of the answer from
How to use discord.py event handler on_voice_state_update to run only when a user joins a voice channel.
#client.event
async def on_voice_state_update(before, after ):
if before.voice.voice_channel is None and after.voice.voice_channel is not None:
for channel in before.server.channels:
if channel.name == 'general':
await client.send_message(channel, "User joined")
elif before.voice.voice_channel is not None and after.voice.voice_channel is None:
for channel in before.server.channels:
if channel.name == 'general':
await client.send_message(channel, "User left");
Three months late, but for anyone who stumbles upon this later, here's the rewite (1.0) branch version.
# VC PROCESSING
#client.event
async def on_voice_state_update(member, before, after):
if not before.channel and after.channel:
role = discord.utils.get(member.guild.roles, name="role name")
await member.add_roles(role)
elif before.channel and not after.channel:
role = discord.utils.get(member.guild.roles, name="role name")
await member.remove_roles(role)
after is the Member object as it exists "now", after the voice state has changed. That would be the member object that you pass to add_roles
#client.event
async def on_voice_state_update(before, after ):
role = discord.utils.get(after.server.roles, name="YOUR ROLE NAME")
if not before.voice.voice_channel and after.voice.voice_channel:
await client.add_roles(after, role)
elif before.voice.voice_channel and not after.voice.voice_channel:
await client.remove_roles(after, role)