Not recognizing commands in discord with Python - 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)

Related

Python, await ctx.send(response) send doesn't work

# bot.py
import discord
import os
import random
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
TOKEN = "token"
intents = discord.Intents.all()
intents.message_content = True
bot = commands.Bot(command_prefix='.', intents=intents)
#bot.event
async def on_ready():
print(f'{bot.user.name} text')
#bot.command(name='rastgelefilm')
async def rf(ctx):
with open("movies.txt") as f:
lines = f.readlines()
response = random.choice(lines)
await ctx.send(response)
bot.run(TOKEN)
I'm making a Discord bot and I'm trying to run this code but send doesn't work and it's white on Visual Studio Code. Is something wrong with the code?
I tried these two.
async def fn(ctx:discord.ApplicationContext):
ctx.channel.send()

discord bot sends infinite messages regardless of user input

I'm trying to make a discord bot respond when someone makes laughing remarks but it infinitely sends gifs whenever someone types anything
code is as follows
import os
import discord
import random
from discord.ext import commands
import keep_alive
Bot_Token = os.environ['Bot_Token']
bot = discord.Client()
#bot.event
async def on_ready():
guild_count = 0
for guild in bot.guilds:
print(f"- {guild.id} (name: {guild.name})")
guild_count = guild_count + 1
print("AGOP_Bot is in " + str(guild_count) + " guilds.")
#bot.event
async def on_message(message):
if message.content == "AGOP~hello":
await message.channel.send("https://c.tenor.com/tTXwGpHrqUcAAAAC/summoned.gif")
if message.content == "Lmao" or "Lol" or "lmao" or "lol" and message.author.id != bot_id:
response_funny = ["https://c.tenor.com/mUAgLfICUC0AAAAC/i-didnt-get-the-joke-abish-mathew.gif","https://c.tenor.com/zdoxFdx2wZQAAAAd/not-funny-joke.gif","https://i.pinimg.com/originals/f5/53/97/f55397a7de1c82b37d6d62e655a0e915.gif","https://jutsume.com/images2/2022/04/16/is-this-some-peasant-joke-meme.png","https://c.tenor.com/FnASqUdvJH4AAAAC/whats-so-funny-john.gif"]
await message.channel.send(random.choice(response_funny))
bot.run(Bot_Token)
keep_alive.py
import os
import discord
import random
from discord.ext import commands
# import keep_alive
Bot_Token = os.environ['Bot_Token']
bot = discord.Client()
...
#bot.event
async def on_message(message):
if message.content == "AGOP~hello":
await message.channel.send("https://c.tenor.com/tTXwGpHrqUcAAAAC/summoned.gif")
if message.content in ("Lmao" or "Lol" or "lmao" or "lol") and message.author.id != bot.user:
response_funny = ["https://c.tenor.com/mUAgLfICUC0AAAAC/i-didnt-get-the-joke-abish-mathew.gif","https://c.tenor.com/zdoxFdx2wZQAAAAd/not-funny-joke.gif","https://i.pinimg.com/originals/f5/53/97/f55397a7de1c82b37d6d62e655a0e915.gif","https://jutsume.com/images2/2022/04/16/is-this-some-peasant-joke-meme.png","https://c.tenor.com/FnASqUdvJH4AAAAC/whats-so-funny-john.gif"]
await message.channel.send(random.choice(response_funny))
bot.run(Bot_Token)
# keep_alive.py
In addition to some formatting, I changed some of the variables for the API calls. I commented out the keep_alive.py as I assume you are using that to keep your code hosted on Repl.it or something, and you can just comment it back in. I was also able to get this code to work with my bot and execute as you want.

Python - stops job on discord

The plan for this bot is to wake up --> retrieve data I need --> terminate itself.
I've tried using client.logout() and client.close(), but the program won't stop after it runs. Any advice?
import discord
import os
from discord.ext import tasks
from discord.ext import commands
discord_client = discord.Client()
#discord_client.event
async def on_ready():
for guild in discord_client.guilds:
if guild.name == GUILD:
break
print(
f'{discord_client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
# Send message when Bot wakes up
channel = discord_client.get_channel(ID)
await channel.send("*wakes up and starts working*")
retrieve_data.start()
await discord_client.logout()
#tasks.loop(count=1)
async def retrieve_data():
# do things here
discord_client.run(TOKEN)
You don't have your token defined, import load_dotenv and call it, you have undefined variables, also logout() is deprecated so use close() instead and it works.
import discord
import os
from discord.ext import tasks
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
discord_client = discord.Client()
#discord_client.event
async def on_ready():
# for guild in discord_client.guilds:
# if guild.name == GUILD:
# break
# print(
# f'{discord_client.user} is connected to the following guild:\n'
# f'{guild.name}(id: {guild.id})'
# )
# Send message when Bot wakes up
# channel = discord_client.get_channel(ID)
# await channel.send("*wakes up and starts working*")
await retrieve_data.start()
await discord_client.close()
#tasks.loop(count=1)
async def retrieve_data():
print("test")
discord_client.run(os.getenv("TOKEN"))

Supposed to send a welcome message when someone joins but nothing happens

import discord
import os
intents = discord.Intents.default()
intents.members = True
#client.event
async def on_member_join(member):
if member.guild.name == 'Bot Test Server':
await client.get_channel(927272717227528262).send("https://tenor.com/view/welcome-to-hell-lucifer-morningstar-tom-ellis-lucifer-welcome-gif-18399120")
else:
return
Everything is set correctly and it should send the message, but nothing happens.
Why you didn't initialize client? and I fixed your code since it's not clear nor doesn't look like from dpy docs
import discord
from discord.ext import commands
import os
client = commands.Bot(command_prefix='', intents=discord.Intents.all())
#client.event
async def on_member_join(member):
if member.guild.name == 'Bot Test Server':
channel = client.get_channel(927272717227528262)
await channel.send("https://tenor.com/view/welcome-to-hell-lucifer-morningstar-tom-ellis-lucifer-welcome-gif-18399120")
else:
return
client.run('')

Python discord bots

I am currently trying to create my discord bot. sadly, this does not work and I have no Idea why...
import discord
import os
import time
from ka import keep_alive
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix = '.')
prefix = '.'
#client.event
async def on_ready():
print("I'm ready! {0.user}".format(client))
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="Croissants!"))
#client.command()
async def join(ctx):
channel = ctx.message.author.voice.voice_channel
await client.join_voice_channel(channel)
await ctx.send("On my way!")
client.run(os.getenv('TOKEN'))
there are NO errors. But no output aswell. I try making it join my vc by writing: .join
channel returns None, because ctx.message.author don't have voice attribute. Also, Client.join_voice_channel is deprecated since v1.0 (read here)
Instead of that, try this:
import discord
import os
import time
from ka import keep_alive
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix = '.')
prefix = '.'
#client.event
async def on_ready():
print("I'm ready! {0.user}".format(client))
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="Croissants!"))
#client.command()
async def join(ctx):
channel = ctx.guild.get_member(ctx.author.id).voice.channel # This
await channel.connect()
await ctx.send("On my way!")
client.run(os.getenv('TOKEN'))
Do client.add_command(join). You currently have the function for your command, but you haven't told the bot to recognize it as a command.

Categories

Resources