I'm trying to make a command line on the terminal in my bot that has some basic commands like send <channel ID> <text>
and exit
. Here is my current code:
async def start_bot():
await client.start('token')
@tasks.loop(count=1)
async def cli():
'''Implement the bot command line interface.'''
try:
while True:
command = input(f'{c.bdarkblue}>>>{c.end} ') # my formatting
command = command.split()
command, subcommand = command[0], command[1:]
if command == 'exit':
os.kill(os.getpid(), 15) # there is sigterm catching in other parts of my code that save everything
if command == 'send':
client.get_channel(int(subcommand[0])).send(' '.join(subcommand[1:]))
if command == 'send_all_guilds':
for guild in client.guilds:
try:
guild.system_channel.send(' '.join(subcommand))
except discord.errors.Forbidden:
print(f'{c.darkwhite}Failed to send to guild ID: {guild.id} Name: {guild.name}{c.end}')
except Exception as e:
print(e)
cli.start()
asyncio.get_event_loop().run_until_complete(start_bot())
The problem is that none of the code executes while the input()
is happening, and prefixing the input function with await
gives object str can't be used in 'await' expression
. The bot starts only when I do something with the commands that raises an exception (like trying to send a message while the client hasn't started). Is there another asynchronous alternative to input()
?
(Whenever I say "command", I refer to the terminal commands, and not the discord bot commands, like this