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

# 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()

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')

Why can't my discord.py bot see messages?

I am making a discord.py bot and it does not "see" messages at all, it is supposed to print everything into the terminal and have a prefix, but it doesn't even print any message.
code:
import datetime
import os
import discord
from timeit import default_timer as timer
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
bot = commands.Bot(command_prefix="!")
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
TOKEN = ('boop')
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
async def on_message(message):
print(f'USER - {message.author} texted - {message.content}')
#bot.command()
async def start(ctx):
await ctx.channel.send("you have started")
await ctx.guild.create_role(name= {message.author} + "w")
client.run(TOKEN)
(there are extra unused modules I'm planning to use later)
There are a few problems in your code:
You should only have one commands.Bot instance, events are not exclusive to discord.Client.
For your bot to catch commands, you need bot.process_commands at the end of your on_message event.
You were missing #bot.event above on_message.
You forgot the fstring in ctx.guild.create_role(...).
import discord
from discord.ext import commands
import os
import datetime
from dotenv import load_dotenv
from timeit import default_timer as timer
load_dotenv()
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
#bot.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
#bot.event
async def on_message(message):
print(f'USER - {message.author} texted - {message.content}')
await bot.process_commands(message)
#bot.command()
async def start(ctx):
await ctx.channel.send("You have started")
await ctx.guild.create_role(name=f"{message.author}w")
bot.run("TOKEN")

How do I get a random line from a file and send it as a discord bot?

I'm new to python and I'm trying to get a discord bot to send a random line from a text file. So far i have this code, but it wont send when i send the command in the server.
import os
import discord
import requests
import random
from discord.ext import commands
client = discord.Client()
#client.event
async def on_ready():
print('online {0.user}'.format(client))
bot = discord.ext.commands.Bot(command_prefix = "$");
#bot.command(name='filetxt')
async def filetxtgetter(ctx):
lines = open('file.txt').read().splitlines()
file = random.choice(lines)
await ctx.send(file)
client.run(os.environ['token'])
#bot.command()
async def filetxtgetter(ctx):
f=open('file.txt')
lines=f.readlines()
value = random.choice(lines)
random_line = (value.removesuffix('\n'))
await ctx.send(random_line)
This should solve your question.

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)

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