intents not working on discord.py commands extension - python

I'm trying to use intents on my discord bots so I can get all members. I looked through a few posts and read that I'm supposed to do
from discord.ext import commands
intents = discord.Intents(messages=True, members=True)
bot = commands.Bot(command_prefix=',', intents=intents)
However when I try it, it says NameError: name 'discord' is not defined. I don't know how to solve it and why works for other people.
I also have "Server Members Intent" ticked.
I've tried putting import discord at the top but it return None if I print the guild or member list (it only shows the info for the bot itself, the other users it just says "None").

This is because you have imported only the commands extension, you have to import discord fully too, simply add this to the top of your file
import discord
...

Related

Discord .py bot to assign role to any user who types the command

there. I'm brand new to coding in Python and I'm just not able to figure out what I'm doing wrong.
I want any user in a Discord server to be able to type my specified prefix and a command and have the bot assign the desired role.
For example, if a user want to add a role to themselves called Rook, I want them to be able to type:
%Rook
And have the role added to them by the bot.
I've tried piecing together so many different examples I've found on the internet and nothing works.
Is anyone able to help?
Here's my code:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = "%",intents=intents)
bot = commands.Bot(command_prefix="%", intents=discord.Intents.all())
#client.command()
async def Rook(ctx):
# if prefix is '-' you'd type: -rook
await user.remove_roles("1073162715499089981")
await user.add_roles("1073162715499089981")
await ctx.send("Rook role added!")
I tried piecing together various bits I could find from similar questions, but nothing is working. I type in the command %Rook and no role is assigned. I'd ideally NOT like the user to have to type the command %Rook #TheirName, but just %Rook.

What is and How to fix this error in discord.py?

I was using replit (maybe because replit doesnt work?) tryoing to make some code for a Discord bot when I saw this:
Type error: __init__ missing 1 required keyword-only argument: "intents"
Im not really sure what is means
Heres my code (BTW i used pip install discord in shell)
`
import discord
token = "mytoken (not revealing it i guess)"# but even with right token it doesnt work
client = discord.Client()
name = "MIIB.BOT_v1.0"
#client.event
async def on_ready():
print("Bot logged in as ", name, "!")
client.run(token)
on_ready()
#i did *****pip install discord****** in shell btw
`
I tried some variations but not much. I expected:
Bot logged in as botname#6969
Replace
client = discord.Client()
with
client = discord.Client(intents=discord.Intents.default())
As per recently(-ish), you need to specify the intents your bot uses when login to the API, so you should populate the Client argument with an intent object.
Calling the default intents, if you don't plan on doing anything with any other that you might need to enable, would look like this
bot_intents = discord.Intents.default()
client = discord.Client(intents=bot_intents)

Discord bot, The bot does not send messages about the arrival of a new member Python

I am new to creating bots for Discord and I take codes from different examples and I write on replit.
I want the bot to send a welcome message to a new member of the server, but when I invite my 2nd account, it doesn't send anything. I have included Intentions
Full code
import discord
from discord import Game
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
from webserver import keep_alive
import os
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
client = commands.Bot(command_prefix='!')
#client.event
async def on_member_join(member):
channel = client.get_channel(channel_id=870695778593701939)
embed = discord.Embed(
title=f"Welcome {member.name}",
description=f"Thanks for joining {member.guild.name}!") # F-Strings!
embed.set_thumbnail(
url=member.avatar_url
) # Set the embed's thumbnail to the member's avatar image!
await channel.send(embed=embed)
And that's all I'm writing on the phone and I don't understand how I can return this whole code and where, so sorry
I'm guessing I have included Intentions means that you have added the intents in the bot constructor and the developers portal
so first
channel = client.get_channel(channel_id=870695778593701939)
channel_id is not the argument name its id and its a positional argument so you can just do client.get_channel(channel_id_here) would work, just replace channel_id_here with the channel id
second when your setting url=member.avatar_url in the set_thumbnail function it would raise an error if the member has no avatar so i would suggest you check if the user has a avatar, if they don't just give it member.default_avatar_url, but if your using discord.py 2.0 (master branch) then its ALOT easier with member.display_avatar.url
this is just a suggestion because display_avatar returns the users/members avatar if they have one or the default discord gives them so no errors are raised

My bot is no longer responding to commands

This was asked previously, but never answered (as in marked) and did not have the same specifics as me (here)
For context, I was working on my bot, specifically on a command that add and removes roles, I restarted the bot, and it just stopped responding to commands. I say that instead of "stopped working" because it still works. It comes online, it can put its status up, and it responds to on_message functions. Any attempt at calling a command does nothing, it doesn't respond in chat, it doesn't give an error, throwing a print statement in the command shows that it doesn't even run the command.
I have already tried reverting the roles command because it happened right after, did not fix it. I removed the command entirely and the same problem occurred. I re-installed all my packages whether they related to discord or not and updated. Nothing fixed it.
I suspect it has something to do with the way I'm running the bot. I'm using Pycord. Here is some code that may be helpful, but I really don't know what would help resolve the problem
# Top of main.py file
import discord, random, json, os
from discord.ext import commands
from keep_alive import keep_alive
from discord.utils import find
# discord.py API docs: https://discordpy.readthedocs.io/en/latest/api.html
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='!', intents=intents)
client.remove_command('help')
# ...
# unrelated code here
# ...
# I use cogs for my commands, here is the imports for all cogs
client.load_extension('cogs.commands.fun')
client.load_extension('cogs.commands.msg')
client.load_extension('cogs.commands.mod')
client.load_extension('cogs.commands.games')
client.load_extension('cogs.commands.role')
client.load_extension('cogs.commands.misc')
client.load_extension('cogs.commands.secretCommands')
client.load_extension('cogs.commands.msgTriggers')
# ...
# unrelated code here
# ...
# Bottom of main.py file
keep_alive()
client.run(os.getenv('token'))
# Top of one of my cogs (fun commands cog)
import discord, json, randfacts, random
from discord.ext import commands
from random import randint
from cogs import assets
class Fun(commands.Cog):
def __init__(self, client):
self.client = client
# ...
# unrelated code here
# ...
# Bottom of one of my cogs (fun commands cog)
def setup(client):
client.add_cog(Fun(client))

How to link a discord channel with python

So I am working on making a discord bot for a support server of my friends the one thing i am stuck up on is I want the bot to tell a user to go to a specific channel with the #channel that way they can click on it for convince I have tried some things but I am relatively new to coding any ideas. this is all that i have so far.
import discord
import os
import keep_alive
if message.content.startswith("!help"):
await message.channel.send("To contact a support person go to")
First off you should use a bot command decorator rather than looking for the command statement in the messages content.
In this command you should extract the target channel object through its ID, and mention that channel in your message. In this way users will be able to click on the mention and be redirected to that channel.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def help(ctx):
guild = ctx.guild # The guild to which the channel belongs to
target_channel = guild.get_channel(channel_ID_goes_here)
await ctx.send(f"To contact a support person go to {target_channel.mention}")

Categories