I am creating a discord bot with NodeJs and I want to utilize the commander.js package to make the commands a bit more structured.
I want to declare a param to give a username, eq('-u, --user <USERNAME>', 'the user to handle').
This works great, until the username consists of more than 1 word. eq. John Doe. what happens is that after parsing the arguments I get a ('-u' == 'John') and I have a leftover argument array with the word 'Doe' in it.
I have tried passing the username between quotes. eqnode program -u "John Doe" but the result is the same.
Am I missing something or is commander.js not capable of handling multi word arguments?
Turns out the args where wrongly splitted in another part of my bot. so this isn't an issue with either NodeJS or commander.js.
#Mods this question can be closed
Related
I've made a discord bot using python and I'd like to add a bug reporting command and some other commands so I was wondering how I could do this. For example User types: /report_bug and the bot responds: describe the bug. and then the User types in the bug. How could this be possible?
Here's a small snippet of the response code:
def handle_response(message) -> str:
p_message = message.lower()
if p_message == 'report_bug':
return 'Describe the bug'
#so here's where I'd like to take user input
You can't do anything after returning but assuming you just want to know how to get input:
Just add it as an argument to your command instead of making the bot ask for additional input afterwards. This makes the most sense, is the most user-friendly, and is the least pain to implement.
Use wait_for with a check.
Show a Modal where they can type whatever they want to.
Official examples for slash commands (with arguments): https://github.com/Rapptz/discord.py/blob/master/examples/app_commands/basic.py
Official example for a Modal: https://github.com/Rapptz/discord.py/blob/master/examples/modals/basic.py
Hi im currently adding some new features to a bot and it's been going great with some amazing progressions each day. I ran into an issue that i can't seem to solve even though i feel like the solution is right in front of me. In my code i have an if statement to check if a user has more matches won than 22 in the database, if so they are allowed to equip a certain background if not it returns an error message. I tried to do another if statement to check if a certain user is trying to run the command by matching their discord ID and for some reason it runs both the if statements when doing either command even though i used message.content.includes to specify what arguments it should look for in the command. Any help would be appreciated
According to the mozilla docs:
The comma operator (,) evaluates each of its operands (from left to right) and returns the value of the last operand.
This means that only the second operand in each if statement is being returned, ignoring message.content.includes().
Changing the , to && should fix the issue.
source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
I'm building a command-line interface using the argparse library to parse user input. At one stage, I'd like to take user input such as "'test', x=False" and use it in a function such as func('test', x=False).
I've tried using ast.literal_eval to do this but it encounters a syntax error at the equals sign. (I did ast.literal_eval("("+args+")") where args was above example)
Does anyone know of a safe way to parse the user input like that? Preferably without eval although worst-case scenario I could use eval as, well, it's a CLI tool.
Edit (to people that have said to use input manually(): I need the tool to parse input from when the command is run (it's a python module that I want to be able to be called like python3 -m hmcli.raw --args "'test', x=False" where the args can be flexible as the function used can differ.
import sqlite3
username=input(print("Username?"))
password=input(print("Password?"))
connection= sqlite3.connect("Logininfo.db")
c = connection.cursor()
IDuser=c.execute('SELECT Username, Password FROM LoginInfo WHERE Username= ? AND Password= ?', (username,password)).fetchone()
print(IDuser)
The data that should be returned from this code is "Hello1" and "Password1" (Without the quotemarks). Instead it outputs "('Hello1', 'Password1')".
A few questions, why does it do this?
How can I retrieve this data without the brackets and quotemarks i.e "('Hello1'," would become "Hello1"
The code also prints out "None" before asking for both the username and password why does it do this and how can I fix it?
To answer your first question, the quotes are there to tell you that it is a string. They are not actually a part of your username or password, so it shouldn't be a problem. Similarly, the brackets are there to tell you that this is a sequence (specifically, a tuple). Python has to get two strings to you, and this is the usual way in which it is done. If this causes a problem for you, then you need to explain why.
To answer your second question, None is printed because you're calling print() inside the input() functions. The return value of print() is None. Printing the prompt is already integral to input(), so there's no need to do that. Just input("Username?") will suffice.
fetchone() returns one "tuple" or the result of the queryset "object", you can access the individual values much like an array
try print(IDuser[0]) and it will print "Hello1".
Please refer to https://docs.python.org/2/library/sqlite3.html and search fetchone() call for more info.
I have been trying to create a basic program (to later incorporate into a bigger one) that searches for specific words within a user input, here is what I originally did:
command=input(">")
for "hi" in command():
print("Hello there.")
I thought this would search for "hi" in the user input and if it found the word, it would say hi back, this did not work so I tried using variables instead:
command=input(">")
string = "hi"
for string in command():
print("Hello there.")
This still did not work so I did both these again but used "if" instead of "for", and nothing had changed. If anyone could help, it would be much appreciated, thanks! - James.
You need to use in operator for membership checking:
if 'hi' in command:
print("Hello there.")
Also Note that since command is a string and not a callable object you can not use parenthesis the the trailing of it.
And note that when you use in it checks the membership in the whole of the string and it's possible that you have another word in your text which is contains hi like high, that using in it will returns True which is not what you want.
So all you should do is splitting the string with whitespaces and check the membership of word hi within other words:
if 'hi' in command.split():
print("Hello there.")