Discord bot python script - python

I am coding a discord bot for my special interest can I have some help, please? The code format is this:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)```
How do I make this work?

I would suggest, that you defined client wrong. Try:
from discord.ext import commands
//*Your dotenv stuff here*
client = commands.Bot(command_prefix="Your prefix here")
client.run(TOKEN)
I hope it works, if not, feel free to correct me.

Related

Discord.py commands wont run when used under #client.command()

i am attempting to use discord.ext commands to make my commands neater but the commands will not run
imports and intents:
import discord
from dotenv import load_dotenv
import requests
from discord.ext import commands
from discord.ext.commands import Bot
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
#Delcare imports
intents = discord.Intents().all()
client = commands.Bot(command_prefix='$', intents=intents)
command:
#client.command()
async def ping(ctx):
await ctx.send("pong")
i tried using
client = commands.Bot(command_prefix='$', intents=intents)
however the following error message occurred:
client = commands.Client(command_prefix='$', intents=intents)
AttributeError: module 'discord.ext.commands' has no attribute 'Client'
Try only using bot and not client:
import discord
from dotenv import load_dotenv
import requests
from discord.ext import commands
from discord.ext.commands import Bot
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
intents = discord.Intents().all()
bot = commands.Bot(command_prefix='$', intents=intents)
#bot.command()
async def ping(ctx):
await ctx.send("pong")
bot.run('DISCORD_TOKEN')

discord.client: logging in using static token help me

enter image description here
Code:
import discord
import os
import sys
import asyncio
import time
import json
from discord.ext import commands
from discord.utils import get
bot = commands.Bot(command_prefix='*', intents=discord.Intents.all())
TOKEN= 'ODU1MDAwMzU3Njk2NDM4MzEy.Gxtt2S.IxUbC9bS8Ps3hTeUYpwxW_U7A6q0MuzTwJxUFA'
#bot.event
async def on_ready():
print(" Bot account ")
print()
print(f"Username: {bot.user.name}")
print(f"Bot ID: {bot.user.id}")
print(f"Bot token: {TOKEN}")
print()
bot.run(TOKEN)
Your code is throwing an discord.errors.PrivilegedIntentsRequired Exception as you can see in the bottom line of your screenshot.
This means it is trying to use intents that it has no permission to use. Either enable those intents in the discord developer portal or replace discord.Intents.all() with the intents that you really need and have sufficient permissions for.

Not recognizing commands in discord with Python

I run this, it connects however when running it will not return anything when it comes to commands. I've tried changing this to the context return method from on every message. Previously only one message would appear, now none with this method even though both commands are the same with slight differences. What is my error on this? Sorry this is literally my first attempt at a discord bot.
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
botToken = os.getenv("DiscBotToken")
discClient = discord.Client()
bot = commands.Bot(command_prefix = "!")
#discClient.event
async def on_ready():
print(f"{discClient.user} is now connected.")
print('Servers connected to:')
for guild in discClient.guilds:
print(guild.name)
#bot.command(name = "about")
async def aboutMSG(ctx):
aboutResp = "msg"
await ctx.send(aboutResp)
#bot.command(name = "test")
async def testMSG(ctx):
testResp = "msg"
await ctx.send(testResp)
discClient.run(botToken)
You heading in the right direction commands.Bot have both event and command no need to use a client just for events.
You should either use discord.Client or commands.Bot not both in your case it is commands.Bot.
Also you are running the client only
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
botToken = os.getenv("DiscBotToken")
bot = commands.Bot(command_prefix = "!")
#bot.event
async def on_ready():
print(f"{discClient.user} is now connected.")
print('Servers connected to:')
for guild in discClient.guilds:
print(guild.name)
#bot.command(name = "about")
async def aboutMSG(ctx):
aboutResp = "msg"
await ctx.send(aboutResp)
#bot.command(name = "test")
async def testMSG(ctx):
testResp = "msg"
await ctx.send(testResp)
bot.run(botToken)

Discord Python Bot not sending responses to commands

We are creating an Discord bot.
The Problem is that no error is in Console but the Chat in Discord is also empty. So no response at all. Could you please help me and say what my error is and how to resolve the problem
Best Wishes,
Niktix
import discord
from discord.ext import commands
import asyncio
from dotenv import load_dotenv
from discord.ext.commands import Bot
import askai
import numpy
import random
import json
import datetime as DT
from time import sleep
import sLOUT as lout
import os
botStartTime = DT.datetime.now()
ver = ['v1.0.8', '2021-01-13']
config = 'config.yml'
bot = commands.Bot(command_prefix = '!')
lout.writeFile('AskAIBotLogs.txt', '\naskai initialisiert!', True)
load_dotenv()
#bot.command(pass_context=True)
async def time(ctx):
startTime = DT.datetime.now()
await ctx.send('Auf dem server ist aktuell folgende Uhrzeit: {}'.format(DT.datetime.now()))
lout.log(config, startTime, 'time')
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Activity(type=1, name='Beantwortet Fragen', url='https://www.youtube.com/watch?v=e3tuUKGIaGw', platform='YouTube', assets='askai',))
lout.log(config, botStartTime, None, None, True)
print('Eingeloggt als {}'.format(bot.user.name))
#bot.event
async def on_message(message):
if message.author.bot:
return
if message.channel.name != "askai-support":
return
if message.content.startswith("!"):
return
await bot.process_commands(message)
await message.channel.send(askai.ask(message.content))
bot.run('token')
Why does it not work? Well, the answer is fairly simple. Please refer to the following lines you provided:
#bot.event
async def on_message(message):
if message.author.bot:
return
if message.channel.name != "askai-support":
return
At this point in the code, you provide the channel name. Although, you don't provide the channel ID afterward. Therefore the bot cannot get said channel. In order to get the channel ID, please refer to Discord's official guide here. Upon doing that, make the same line of code as above, although replace "message.channel.name" with "message.channel.ID". Let me know if you have any further questions. Best Regards,

How to go bot online on Python

I'm having some difficulties getting the bot online. When I run the file the bot is not online on the server I need. Before I used pycharm but due to many errors on my computer, I migrated to VSCode. In pycharm it worked normally, but now in VSCode I can't make it work. Can someone help me?
The variables token and canal_id are correcty configured on my code and i already check on My applications on discord.
Code:
import asyncio
from datetime import datetime, timedelta
import discord
from discord.ext import commands
token = 'TOKEN'
canal_id = 0000
bot = commands.Bot(command_prefix='%')
#bot.event
async def on_ready():
print('Got online')
bot.run(token)
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)

Categories

Resources