discord.py slash commands not showing - discord.py

I am trying to create slash commands using discord.py but they do not show up when using discord.
I have tried re inviting the bot with applications.commands and giving the bot admin.
The code:
import discord
from discord.ext import commands
import os
import time
from dotenv import load_dotenv
import random
#sorry, global variable
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
client = discord.Client()
bot = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
#bot.command()
async def test(ctx, arg):
await ctx.send(str(arg))
client.run(TOKEN)

#bot.command() makes message commands, not slash commands. You won't see any slash commands if you don't make any...
There's an official example for slash commands that you could look at.
Also, you have both a Client and a Bot. That doesn't make any sense, you can only have one. Choose one, get rid of the other.
Next, your message command test is registered to your Bot, but you're running the Client.
Lastly, you're not passing intents. Make sure you're on the most recent version of dpy.

Related

Discord py bot not going online in server

My discord bot get this error this will explan what error I got from the message
error image
import os
import discord
client = discord.Client()
#client.event
async def on_ready():
print("I'm in")
print(client.user)
#client.event
async def on_message(message):
if message.author != client.user:
await message.channel.send(message.content[::-1])
my_secret = os.environ['DISCORD_BOT_SECRET']
client.run(my_secret)
Discord bots now require you to pass it a set of intents to run. Intents allow bot developers to "subscribe" to specific events in Discord.
Your discord client constructor takes a parameter of intents
https://discordpy.readthedocs.io/en/stable/api.html?highlight=intents#discord.Intents
You can construct your own object with custom intents, or just use an intents class method to give it all, default or no intents.

Hey, I just created a discord bot using PyCharm and its all going good, but when i try to send !hi or hi it doesnt send Hello back. can anyone help me

import discord
intents = discord.Intents(messages=True)
client = discord.Client(command_prefix='!', intents=intents)
import random
import time
import asyncio
TOKEN = "<My token>"
intents = discord.Intents(messages=True)
#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("hi"): #whenever i type this
await message.channel.send("Hello") #i dont get this
#client.event
async def on_ready():
print("Running!")
client.run(TOKEN) #Token
thanks for the help,i just need the program to say hello when i say hi,
I am kinda new to making bots soo yeah. I copied this from a youtube vid, but the intents i did by myself. I am using 3.8 version of Python, it runs without errors
This may be because you did not update your intents or in general something wrong with the intents,
Intents on the discord developer portal are in the Bot Section,
If these are enabled then try printing message.content to see if it returns blank or what the user says.

Discord.py - how to shut down a bot properly and disconnect from databases

I am developing a discord bot and currently managing data in memory. Everything is ok but I need to move over to using databases to store data. So my question is how do you properly 'shut down' a bot and disconnect from the database?
To give an example, suppose I have code like this
import os, sqlite3
import random
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix='!')
dbcon=None
#bot.event
async def on_ready(): #Q1
print(f'{bot.user.name} has connected to Discord!')
dbcon = sqlite3.connect('example.db')
bot.run(TOKEN)
I understand that the last line starts the bot, which will run and listens to events and respond to them. But I am not sure:
Q1: should I init the database in the on_connect method instead of on_ready? I read the API but still I am not sure
how do I implement a method that can be used to 'turn off' the bot, that method should shut down the sql connection dbcon? Is this through the on_disconnect method?
]

Discord.py only send a message to one person

I am making a discord bot in python and I want it to send a message to only the person who typed in a command without sending it to anyone else.
Any ideas?
See here: https://discordpy.readthedocs.io/en/rewrite/ext/commands/commands.html#converters
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix='!')
#bot.command(name='your_command')
async def DM(user: discord.User, message="Hi"):
await user.send(message)

Having problem with message sending command

I am trying to do a repeat command with this code:
import discord
from discord.ext import commands
bot = discord.Client()
client = commands.Bot(command_prefix='V!')
#client.command(name='repeat')
async def _repeat(ctx, arg):
await ctx.send(arg)
bot.run('TOKEN')
but when sending a message with a command, the bot doesnt respond neither with the wanted message, nor an error that would imply something is not right. i am also very new to programming so it may be something dumb that i do not know. Any help is appreciated.
If you examine your code carefully, you'll see that you are assigning the command to the client object, but running the bot object. You need to do client.run("<TOKEN>") as another commenter suggested.
You also don't need bot = discord.Client() at all. discord.Client is a parent class with less abilities. I encourage you to rename client to bot though.
from discord.ext import commands
bot = commands.Bot(command_prefix='V!')
#bot.command(name='repeat')
async def _repeat(ctx, arg):
await ctx.send(arg)
bot.run('TOKEN')
Notice now there's no import discord or discord.Client anywhere.
See: What are the differences between Bot and Client?

Resources