discord bot python auto-role(help) - discord.py

import discord
from discord.ext import commands
print(discord.__version__)
client = commands.Bot(command_prefix = '!')
#client.event
async def on_ready():
print('bot is online')
#client.event
async def on_member_join(member):
role = get(member.guild.roles, name="Members")
await member.add_roles(role)
print(f"{member} was given {role}")
client.run(removed)
what am I doing wrong here? I am trying to make an auto-join role when somebody joins it gives him a role

In version 1.5 intents were introduced which directly effect events like on__member_join. You would need to enable it in the Discord Developer Portal under bot in your application. Once that is enabled you would want to add this:
intents = discord.Intents.default()
intents.members = True

Related

Cogs Not Working and not detected In Discord.py

My bot program can't detected cogs file
I have tried every way to solve it, but still nothing has changed
main.py
import discord
import os
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents)
async def load():
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
bot.run('MTA2NzQ1MDMxMTAzMzIzMzUxOA.GSdg-i.CW3X5T82VYGYjPqEPWZ8YQ6oDZni33T-Bi1Ovo')
lab.py
import discord
from discord.ext import commands
class lab(commands.Cog):
def __init__(self,bot):
self.bot = bot
#commands.Cog.listener()
async def on_ready(self):
print('lab.py is running')
#commands.command()
async def holla(ctx):
await ctx.reply('hai')
async def setup(bot):
await bot.add_cog(lab(bot))
eror
I need someone to help me solve my problem, thanks in advance
You're never calling that load() function... It's cool that you made it, but if you're not using it then it's not very surprising that your cogs aren't being loaded.
The recommended place to do this in is setup_hook. Subclass Client (or Bot depending on what you want) and override it.

Can't figure out how to use discord.py ctx

I have tried to convert my discord bot's commands to using ctx instead of interactions but the code I have tried below does not work.
#bot.command(description="Says test")
async def test(ctx):
await ctx.send("Test")
My issue is that the command never loads.
Updated code but still broken
import discord
from discord import ui
from discord.ext import commands
bot = commands.Bot(command_prefix="/", intents = discord.Intents.all())
# Run code
#bot.event
async def on_ready():
print('The bot has sucuessfully logged in: {0.user}'.format(bot))
await bot.change_presence(activity=discord.Activity(type = discord.ActivityType.listening, name = f"Chilling Music - {len(bot.guilds)}"))
# Commands
#bot.command(description="Says Hello")
async def hello(ctx):
await ctx.send(f"Hello {ctx.author.mention}!")
#bot.command(description="Plays music")
async def play(ctx, song_name : str):
await ctx.author.voice.channel.connect()
await ctx.send(f"Now playing: {song_name}")
#bot.command(description="The amount of servers the bot is in")
async def servercount(ctx):
await ctx.send(f"Chill Bot is in {len(bot.guilds)} discord servers!")
#bot.command(description="The bots ping")
async def ping(ctx):
await ctx.send(f"🏓Pong\nChill Bot's ping is\n{round(bot.latency * 1000)} ms")
#bot.command(description="Announcement command")
async def announce(ctx, message : str):
await ctx.send(message)
#bot.command(description="Support Invite")
async def support(ctx):
await ctx.send(f"{ctx.author.mention} Please join our server for support! [invite]", ephemeral=True)
#bot.command(description = "Syncs up commands")
async def sync(ctx):
await ctx.send("Synced commands!", ephemeral=True)
bot.run('')
Your commands don't work because you incorrectly created an on_message event handler.
By overwriting it and not calling Bot.process_commands(), the library no longer tries to parse incoming messages into commands.
The docs also show this in the Frequently Asked Questions (https://discordpy.readthedocs.io/en/stable/faq.html#why-does-on-message-make-my-commands-stop-working)
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message.
Also, don't auto-sync (you're doing it in on_ready). Make a message command and call it manually whenever you want to sync.

Message content intent is not enabled. Prefix commands may not work as expected unless you enable this

I don't know why I'm getting this error. I have all the intents enabled as seen in the image below.
This is my first project** in discord.py, and I don't really understand some things.
Here is the code I have (hoping this is relavant):
import nextcord
from nextcord import Interaction
from nextcord.ext import commands
intents = nextcord.Intents.default()
intents.members = True
command_prefix = '!'
client = commands.Bot(command_prefix, intents = intents)
#client.event
async def on_ready():
print('logged in as {0.user}'.format(client))
print(command_prefix)
testserver = 988598060538015815
#nextcord.slash_command(name = "hello", description = "Bot Says Hello", guild_ids = [testserver])
async def hellocommand(interaction: Interaction):
await interaction.response.send_message("yoooo")
client.run("my token")
change intents = nextcord.Intents.default() to intents = nextcord.Intents.all()

on_member_join function not working in discord.py

I'm working on a bot and the on_member_join function isn't working when a user joins the server. My function is in a cog and is the only function that won't work,
def __init__(self, client):
self.client = client
#commands.Cog.listener()
async def on_ready(self):
#commands.Cog.listener()
async def on_member_join(self, member):
print('working')
I am assuming the indentation is just wrong while pasting here and not in the actual code.
If you haven't done this already, the new version of discord.py has something called intents. First you have to go to the developer portal and activate it. Then you have to add the intents to your code.
This will enable all the Intents:
intents = discord.Intents.all()
client = commands.Bot(command_prefix = '-', intents=intents)

How would I make my bot say something when a channel is created under a category?

I'm trying to make my Discord bot say something and have message awaits when a channel is created, etc however I'm not sure how to do that, here is the code I'm dealing with right now:
import discord, asyncio
discord_token = open('token.txt').read()
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.lower().startswith('test'):
await message.channel.send('testing, sending to channel works!')
#client.event
async def on_ready():
print('Successfully connected to', client.user)
client.run(discord_token)
This can be done using on_guild_channel_create
#client.event
async def on_guild_channel_create(channel):
if channel.category: #if the channel created is in a category
#send something
await channel.send("Something")

Resources