discord bot doesn't respond [duplicate] - discord

I'm trying to understand how migrating from discord.py version 1.7.3 to 2.0 works. In particular, this is test code I'm using:
from discord.ext import commands
with open('token.txt', 'r') as f:
TOKEN = f.read()
bot = commands.Bot(command_prefix='$', help_command=None)
#bot.event
async def on_ready():
print('bot is ready')
#bot.command()
async def test1(ctx):
print('test command')
bot.run(TOKEN)
In discord.py 1.7.3, the bot prints 'bot is ready', and I can do the command $test1.
In discord.py 2.0, the bot prints 'bot is ready', but I can't do the command, and there isn't any error message in the console when I'm trying to do the command.
Why does this occur, and how can I restore the behaviour of version 1.7.3 in my bot?

Use Intents with discord.py
Enable Intents
On Discord Developer Portal
Select your application
Click on the Bot section
And check MESSAGE CONTENT INTENT
Add your intents to the bot
Let's add the message_content Intent now.
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents, help_command=None)
Put it together
The code should look like this now.
import discord
from discord.ext import commands
with open('token.txt', 'r') as f: TOKEN = f.read()
# Intents declaration
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents, help_command=None)
#bot.event
async def on_ready():
print('bot is ready')
# Make sure you have set the name parameter here
#bot.command(name='test1', aliases=['t1'])
async def test1(ctx):
print('test command')
bot.run(TOKEN)

Related

Discord py. How to get if user is online without commands

i need a help with a python code. All I need is to get if user selected (by myself) ID is currently online. How can I do that?
The code for now looks like this, but its dosnt print anserw
import discord
from discord.ext import commands
from discord import Member
TOKEN = "token"
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
client = commands.Bot(command_prefix=".", intents=intents)
#client.event
async def on_ready():
return
#client.event
async def on_message(message, member : discord.Member=None):
if member == MyID:
print("draziv online")
if message.author == client.user:
return
if "hello" in message.content or "hi" in message.content:
return
# await message.channel.send(f'Hello there {message.author.mention}. I hope you have a fantastic day so far. ')
client.run(TOKEN)
I was trying all the methods I found over the internet, but almost all of them are outdated

Commands not working after updating to discord.py 2.0

I was trying to change my version from 1.7.3 to 2.0+ using pip install -U git+https://github.com/Rapptz/discord.py. Usually when I did this there would be an error about intents that would pop up, but instead there were no errors but it said:
discord.client logging in using static token
2022-12-20 12:34:14 INFO
discord.gateway Shard ID None has connected to Gateway (Session ID:6837cbf3ad28b9040ceb5e044dffe90f).
After that, any commands that I used didnt get responded to, as it worked perfectly fine in 1.7.3.
My code:
import discord, os, requests, json, random, time, datetime
from discord.ext import commands
from replit import db
import urllib
import asyncio
from discord import Member
from discord.ui import Select,Button,View
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="e!",
intents=intents)
client = discord.Client(intents=intents)
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('whats the meaning of life'):
await message.channel.send(
'The meaning of life is "freedom from suffering" through apatheia (Gr: απαθεια), that is, being objective and having "clear judgement", not indifference. - Wikipedia.'
)
time.sleep(2)
await message.channel.send(
'There, someone just explained your life. Kind of depressing, isnt it?'
)
basically you don't have the correct intents to be able to send message with the bot
you need to have the discord.Intents.all()
and please don't use the time.sleep in an asynchronous function use asycio.sleep()
import asyncio
bot = commands.Bot('e!',intents=discord.Intents.all())
#bot.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('whats the meaning of life'):
await message.channel.send(
'The meaning of life is "freedom from suffering" through apatheia (Gr: απαθεια), that is, being objective and having "clear judgement", not indifference. - Wikipedia.'
)
await asyncio.sleep(2)
await message.channel.send(
'There, someone just explained your life. Kind of depressing, isnt it?'
)

discord.py changing bot status

I am trying to change my bot status but i keep on getting this error.
TypeError: Client.__init__() missing 1 required keyword-only argument: 'intents
my code
import discord
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
#client.event
async def on_message(message):
if message.content.startswith('bruh'):
channel = message.channel
await channel.send('brah')
client = discord.Client(activity=discord.Game('Beep Boop'))
i added the intents to my code but i am still getting the same error message. I tried to google but all the answers are from pre discord.py v2.
You redefined the client in line 14 and didn't pass the intents keyword argument. And if you want to change the bot's status, just put it in the first client constructor.
import discord
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents, activity=discord.Game('Beep Boop'))
#client.event
async def on_message(message):
if message.content.startswith('bruh'):
channel = message.channel
await channel.send('brah')
# client = discord.Client(activity=discord.Game('Beep Boop'))

Discord.py autorole issue

I'm new at programming.I want to create an autorole system in my Discord server.I tried this:
class MyClient(discord.Client):
async def on_member_join(member):
server = client.get_guild(serverid)
role = server.get_role(roleid)
await member.add_roles(role)
But it's not working.Thanks for helping.
This works for me so I guess this will be useful for you:
import discord
from discord.ext import commands
from discord.utils import get
client = commands.Bot(command_prefix='!', intents=discord.Intents.all())
#client.event
async def on_ready():
print(f'{client.user} Started!')
#client.event
async def on_member_join(member: discord.Member):
role_id = 1234567890123456
role = get(member.guild.roles, id=role_id)
await member.add_roles(role)
client.run('your bot token here')
role_id is Integer.
must have member intents turned on, if you don't know how to enable read this:
Here

Discord self bot slow

from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
#TOKEN
TOKEN = "token"
client = discord.Client()
b = Bot(command_prefix = "!")
#b.event
async def on_ready():
print("on")
#b.event
async def on_message(message):
if "here" in message.content:
try:
await message.author.dm_channel.send("Message I choose")
except:
print('The DM could not be send due to privacy permissions')
b.run(TOKEN, bot = False)
So I have this self bot in discord. Basically it just checks when a person in a server says "#here" it will auto dm them with the message I choose as quick as possible. I would just like to know how would I make this self bot check if the person said that said #here faster.

Resources