My discord bot isn't responding to my commands - discord

I have multiple discord bots but suddenly, they all just stopped responding to my commands. I copied this code from online to see if there was a problem with my code, but this doesn't seem to work either. When I type in "$hello", nothing happens. The bot goes online when it runs but doesn't do anything aside from that. I have double-checked my python is up to date, the bot has server roles and permissions, tried it on multiple servers, and ensured that the bot has admin permissions on the discord developer portal. I'm not sure what else could be wrong. It may just be a coincidence but I installed some discord-ui packages when the problem occurred. However, I did uninstall it and the bot still does not work. And yes, my real token is in the code.
import discord
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.send('Hello!')
client.run('MY TOKEN IS HERE')

If you copy it from somewhere else, they probably don't do it right (proof: here and here). After checking the docs (to check is my guess is right), it's await message.channel.send('things'), not await message.send('Hello!'). So to fix it, change to this:
import discord
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run('MY TOKEN IS HERE')
PS: Because you only show the copied code, I only answer that code

It because of Message Intent. Your bot won't see message content if it is disabled. Of course you can just turn it on but it is recommended to use slash commands. discord.py has no support for slash commands, so I'll recommend to use pycord.
Here is short example of slash command
import discord
bot = discord.Bot()
#bot.slash_command()
async def hello(ctx, name: str = None):
name = name or ctx.author.name
await ctx.respond(f"Hello {name}!")
bot.run("token")
Pycord documentation

Related

Python detects Vanity URL discord.py

I am making a Serverinfo command on my bot. So i want add the server info command show the guild's vanity too! But I read the whole discord documentation but can't find how can i make the bot detect the vanity url and sends it. Then I tried to test by making that command as a single command. Here is my code:
#bot.command()
async def vanity(ctx):
vanity = str(ctx.guild.vanity_url)
await ctx.send(vanity)
The bot doesn't response with the code. A help will be appreciated. Thanks in advance.
Try this to get the vanity url:
vanity = (await ctx.guild.vanity_invite()).url
Documentation reference: https://discordpy.readthedocs.io/en/stable/api.html#discord.Guild.vanity_invite

Send message to user upon join with discord.py self-bot

It suppose to send message upon join but Member_join event isn't working
By the way it's a self bot
Discord.py Version : 1.7.3
My code:
#client.event
async def on_member_join(member):
await member.send("welcome to random server")
print(f"Sent message to {member.username}")
So if you use discord.py-self then your current code will work. it in newer version as in newer version of discord.py they have blocked the access to api thru non bot auth token.

discord.py bot can't dm multiple users with a role

#client.command()
async def eventsoon(ctx):
role = discord.utils.get(ctx.message.guild.roles, name='Golden God')
for member in ctx.message.guild.members:
if role in member.roles:
await member.send("ur mom")
I want the bot to dm users with golden god role, it doesnt work aand gives no errors
I tested your code and it's probably happening because you didn't enable Privileged Gateway intents and your bot can't get all members. Enable Server Members Intent on the Discord Developer site in the Bot section.
Then, add them to your code. Example:
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix="!", intents=intents)

Discord.py-rewrite - How to make the BOT self mute or self deaf in a voice channel?

I am making a Discord BOT using discord.py-rewrite and am focusing on the Music part of my BOT. I checked the API several times but I do not know how to make my self mute or self deaf on a voice channel (not server mute or server deafen). Anyone please know how I can self mute or self deaf my discord BOT?
It looks like the API doesn't expose this functionality, but there is a method for doing this in the websocket code
#bot.command()
async def mute(ctx):
voice_client = ctx.guild.voice_client
if not voice_client:
return
channel = voice_client.channel
await voice_client.main_ws.voice_state(ctx.guild.id, channel.id, self_mute=True)
I can't test this at the moment, so it may not work. Keep in mind also that there is no guarantee that internal methods like this won't change even between minor versions.

Error 403 discord.py with admin privileges?

I am currently working on a command to change a user's nick name.
The bot currently has admin privileges, and the admin role is at the top of the role's list.
However, whenever I run the command, I keep getting this error:
Command raised an exception: Forbidden: FORBIDDEN (status code: 403): Privilege is too low...
The code for the cog is
from discord.ext import commands
from discord.ext.commands import Bot
import discord
class CMDs():
def __init__(self, bot):
self.bot = bot
#commands.command()
async def change(self, ctx):
await ctx.message.author.edit(nick="test")
def setup(bot):
bot.add_cog(CMDs(bot))
When I googled my problem, the solution was to move the role to the top of the role's list, but that did not work (because I keep getting the error). Does anyone else have any ideas on how I might get this to work?
Nobody has or can gain permission to modify the nickname of the guild's owner. This command as written however will work for every other user except the owner.

Resources