0

I'm currently working on a bot that allows someone at the console to (sort of) communicate with people on discord through the bot. However I've come across a problem. The input command doesn't work. I don't really care that it stops the code in its tracks, as I'll be self hosting anyway, and will have to be at the terminal to actually use the bot. The issue I'm having is that the input isn't working. Nothing is displayed in the terminal for the input's message. Here is my code if it helps:

enable = True

@client.event
async def on_message(ctx):
    global previousGld
    if previousGld != ctx.guild:
        print(f"{ctx.author}- {ctx.content} \n From- {ctx.guild} \n")
    else:
        print(f"{ctx.author}- {ctx.content} \n")
    if enable == True:
        res = input("Enter response:")
        if res != "n":
            await ctx.send(f"I say: {res}")
            
    previousGld = ctx.guild
Fireye
  • 5
  • 4

2 Answers2

0

Found a solution! I used both @Ceres's tip about aioconsole, as well as a try statement to circumvent it. Thanks to everyone who helped out! Here's the fixed code.

import discord
import aioconsole

from discord.ext import commands    
client = commands.Bot(command_prefix=".")
client.remove_command('help')

@client.event
async def on_message(message):
    try:
        if message.author == client.user:
            print(f"\nSent- '{message.content}'\n")
        else:
            print(f"\n\n{message.author}- {message.content}\n")
        res = await aioconsole.ainput('Enter response: ')
        if res == "pass":
            pass
        else:
            await message.channel.send(f"I say: {res}")
    except RuntimeError:
        pass

Have a nice day! :)

Fireye
  • 5
  • 4
-3

This is because you don't pass in ctx for your on_message event. Instead, pass in message. Also, as @Ceres pointed out, you should change your input method. Make sure you download aioconsole with pip install aioconsole in your terminal.

import aioconsole

@client.event
async def on_message(message):
    global previousGld
    if previousGld != message.guild:
        print(f"{message.author}- {message.content} \n From- {message.guild} \n")
    else:
        print(f"{message.author}- {message.content} \n")
    res = await aioconsole.ainput("Enter response:")
    if res != "n":
        await message.channel.send(f"I say: {res}")
            
    previousGld = message.guild
Aditya Tomar
  • 1,601
  • 2
  • 6
  • 25