I am trying to create a simple bot for a discord server in Python. For now, as a joke, I am just trying to create a "report" command that will send the entire Bee Movie script to whoever types that.
For some reason, whenever I try to do await message.channel.send(line.strip())
I get an "invalid syntax error"
Does anyone have any tips on how to solve this issue?
# bot.py
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('TOKEN')
client = commands.Bot(command_prefix ='$')
def read_lines(file):
with open(file, 'rb') as f:
lines = f.readlines()
for line in lines:
if line == "\n":
print("\nEmpty Line\n")
else:
await message.channel.send(line.strip())
return
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
if message.content.startswith('$report'):
read_lines('bot.txt')
client.run(TOKEN)
P.S: I am fairly new to Python and the return
right after the mentioned line is also giving me an indentation block issue
That's because you don't specify message
in read_lines()
function in def read_lines(file)
put another argument and make it async like this: async def read_lines(file, message)
and in read_lines("bot.txt")
add this message like this: await read_lines("bot.txt", message)
and see if it works now.
Final code: (Edit also put from discord.ext import commands
on top)
import os
from discord.ext import commands
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('TOKEN')
client = commands.Bot(command_prefix ='$')
async def read_lines(file, message):
with open(file, 'rb') as f:
lines = f.readlines()
for line in lines:
if line == "\n":
print("\nEmpty Line\n")
else:
await message.channel.send(line.strip())
return
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
if message.content.startswith('$report'):
await read_lines('bot.txt', message)
client.run(TOKEN)