0

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

EniGzz
  • 7
  • 1
  • 3

1 Answers1

0

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)

loloToster
  • 1,395
  • 3
  • 5
  • 19
  • Oh I see that makes sense. One last thing, do you know how to remove the b' from the message that is being sent to Discord? Thanks for the help – EniGzz May 09 '21 at 20:33
  • As described [here] you have to decode it. In your case instead of `await message.channel.send(line.strip())` use `await message.channel.send(line.strip().decode("utf8"))` – loloToster May 10 '21 at 06:16
  • sorry i didn't put the link: the [here](https://stackoverflow.com/questions/41918836/how-do-i-get-rid-of-the-b-prefix-in-a-string-in-python) – loloToster May 10 '21 at 08:03
  • oh I see it. Thank you so much – EniGzz May 10 '21 at 22:10
  • also, what exactly do the async and the await keywords do? – EniGzz May 10 '21 at 22:26
  • It is a little bit complicated you can read about it [here](https://docs.python.org/3/library/asyncio-task.html). For you it should mean that everything that needs `await` before it (some discordpy operations) should be inside an `async` function. – loloToster May 11 '21 at 06:25