import os
import discord
import random
from prettytable import PrettyTable
from PIL import Image
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("$picture"):
await channel.send(file=discord.File("images.jpg"))
client.run(os.getenv('TOKEN'))
Hello this is my first time using stack overflow I apologize ahead of time if my post has some mistakes,
so I was trying to have my discord upload a picture when entering a "$picture" in the chat box, but I get an red underline at the line that says await channel.send(file=discord.File("images.jpg")) Saying "undefined named 'channel'" I tried looking into it but couldnt find much. Any ideas? thank you.
You need to do message.channel.send() and not channel.send().
So instead of
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("$picture"):
await channel.send(file=discord.File("images.jpg"))
You can do
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("$picture"):
await message.channel.send(file=discord.File("images.jpg"))
Related
Im trying to make a discord bot, and I want its first command to be pretty simple, I just want it to respond "pong!" when someone types "!ping" but when I try to run the command, nothing happens.. I've tried using other commands too but they all result in errors.. I'm a beginner to using Python, so please, can someone tell me what's wrong with my code, and how I can fix it?
In short, my bot doesn't respond back when I type the command I made for it to do so.
import discord
import os
from discord.ext import commands
from discord.ext.commands import Bot
case_insensitive=True
client = discord.Client()
bot = commands.Bot(
command_prefix="!",
intents=discord.Intents(members=True, messages=True, guilds=True)
)
#client.event
async def on_ready():
print('logged in as {0.user}!'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
#bot.command(name="ping", description= "Find out!")
async def ping(ctx):
await ctx.send("Pong!")
client.run(os.getenv('TOKEN'))
Instead of #bot.command you should use #client.command.
Also no need for the from discord.ext.commands import Bot import
So your new code would be
import discord
import os
from discord.ext import commands
case_insensitive=True
client = discord.Client()
bot = commands.Bot(
command_prefix="!",
intents=discord.Intents(members=True, messages=True, guilds=True)
)
#client.event
async def on_ready():
print('logged in as {0.user}!'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
#client.command(name="ping", description= "Find out!")
async def ping(ctx):
await ctx.send("Pong!")
client.run(os.getenv('TOKEN'))
You could just do inside on_message code block
import discord
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
#client.event
async def on_ready():
print(f'We have logged in as {client.user}')
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!ping'):
await message.channel.send('Pong!')
client.run('your token here')
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('your token here')
As mentioned above, I believe the issues come with the second async function or below, I tried testing it but there was no hello back. Thank you.
You ideally shouldn't be using discord.Client() here but discord.ext.commands.Bot()
Re-write :
import discord
import os
from discord.ext import commands
bot = commands.Bot(command_prefix="$")
#bot.event
async def on_ready():
print(f"We have logged in as {bot.user}")
#bot.command()
async def hello(ctx):
await ctx.send("Hello!")
bot.run(os.getenv("DISCORD_TOKEN"))
Which would result in :
Also, I can't see any issues in the code you provided other than the second if statement being incorrectly indented but even if that were the case you'll get an error.
im making a python discord bot in replit and i cant get ctx to work
i tried import ctx and i just get an error message like AttributeError: module 'ctx' has no attribute 'message' heres my code:
import discord
import ctx
import os
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(""):
print(message.content)
guild = ctx.message.guild
await guild.create_text_channel('cool-channel')
client.run(os.getenv('TOKEN'))
my_secret = os.environ['TOKEN']
If you want to use the ctx parameter, you're supposed to use the commands extension for discord.py. In the on_message event you can use the message parameter "as ctx parameter".
#client.event
async def on_message(message):
if message.author != client.user:
if message.content.startswith(""):
print(message.content)
guild = message.guild
await guild.create_text_channel('cool-channel')
You should not need to use the ctx library to access the message variable's guild property. Try the following:
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith(""):
print(message.content)
guild = message.guild
await guild.create_text_channel('cool-channel')
I am trying to make a discord bot to make random encounters for a dnd, Right now I have the code
import discord
import os
import random
import asyncio
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('$desert'):
DesertList = ['charmander', 'charmeleon', 'charizard']
Desert = random.choice(DesertList)
await message.channel.send(Desert)
client.run("just trust me its the right ID")
What I am trying to do is send Desert (defined Desert = random.choice(DesertList)) but the bot doesn't send anything. I have tested if Desert has a value in the console in visual studio and it returns a random name. I was basing it off of something that works in discord where I type $hello in a channel and it replies Hello!
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
K so I finished it, it all works.
The final code is looking like
import discord
import os
import random
import asyncio
client = discord.Client()
DesertList1 = 'none'
DesertList2 = 'none'
def printdesert():
print(DesertList1)
def randomizedesert():
global DesertList1
global DesertList2
DesertList2 = ['charmander', 'charmeleon', 'charizard']
DesertList1 = random.choice(DesertList2)
#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('$desert'):
global DesertList1
randomizedesert()
printdesert()
message.channel.send("bruh")
await message.channel.send(DesertList1)
client.run(":)")
Simplifying it is a necessity but one for a later date
I am trying to make a fun bot for just me and my friends. I want to have a command that says what the authors username is, with or without the tag. I tried looking up how to do this, but none worked with the way my code is currently set up.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
##gets author.
await message.channel.send('You are', username)
client.run('token')
I hope this makes sense, all of the code i have seen is using the ctx or #client.command
The following works on discord.py v1.3.3
message.channel.send isn't like print, it doesn't accept multiple arguments and create a string from it. Use str.format to create one string and send that back to the channel.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are {}'.format(message.author.name))
client.run('token')
Or you can just:
import discord
from discord import commands
client = commands.Bot(case_insensitive=True, command_prefix='$')
#client.command()
async def whoAmI(ctx):
await ctx.send(f'You are {ctx.message.author}')
If you want to ping the user:
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are', message.author.mention)
client.run('token')