discord python kick and ban command - discord.py

In Discord.py, we have created a command to banish a user. However, the result is output as Nameerror: name'app' is not definded. This is my code.
Do you have any problems with #app.command?
I use client run instead of app run.
#app.command(name="<<kick", pass_context= True)
#commands.has_permissions(administrator=True)
async def _kick(ctx, *, username: discord.Member, reason=None):
await user_name.kick(reason=reason)
await ctx.send(str(user_name) + '\n```User was kick!```')```

If you use client.run you also have to use #client.command(...).

Judging from your comments, you're using a discord.Client instead of a commands.Bot. If you want to use the commands module, you need to use commands.Bot instead.

Related

Discord.py bot repeating the command

Hey I am creating a discord bot using python I have put my first command it says pong but have edit that command and run the bot again and for checking I put the command ping the pong is repeated 2-3 times. This only my 1st command I have use I want to add more command then this issue I will face again plss help if someone is having to fix this thanks
import discord
from discord.ext import commands
bot = commands.Bot('!') # ! is Prefix
#bot.event
async def on_ready():
print("Bot is Online!")
#bot.command()
async def ping(ctx):
await ctx.send("pong")
#bot.command()
async def hi(ctx):
await ctx.send("hello")
bot.run(TOKEN)

How do I put all the text after the name of my command and the prefix into a variable like rest_of_message in the async def?

So I'm making an afk command, and I want to store everything I type after !afk into the reason, instead of having to put "" around it. Is there any way I can do this? Thanks in advance :)
The text after the command is passed on as all variables in your program you can get all of those with *args and merge them back into a sentence with ' '.join(args).
Example code:
bot.command()
async def afk(ctx, *args):
reason = ' '.join(args)
ctx.send(f'{ctx.author.name} has gone afk with the following reason: "{reason}", bye')

To log Lambda function name in cloudwatch

Is there any possible way to log the name of the Lambda in CloudWatch ?
Ex:
START RequestId: 4b453a3-f239-461f-94ab-ebesdfsdb04de Version: $LATEST
The "RequestId" is already getting logged. Any property I can use to log the name of the lambda as well ?
I don't want an explicit console.log statement but a property/parameter which directly gives out my lambda's name along with START , END and INFO fields.
You should be using context property (function_name).
def lambda_handler(event, context):
print("lambda function: {}".format(context.function_name))
Please refer below link for more details.
You coud use environment variable AWS_LAMBDA_FUNCTION_NAME (see full list). If your Lambda are written on Python it could looks like this:
import os
def lambda_handler(event, context):
print("Running function '%s'" % os.environ.get('AWS_LAMBDA_FUNCTION_NAME', None))
could do this in nodejs
console.log(process.env.AWS_LAMBDA_FUNCTION_NAME)

How to login with pywikibot using password from environment variable?

I want to run pywikibot from inside Docker container, so I could run some cron jobs with it from the cloud (maybe Azure).
I added code of my bot and user-config.py file to my Docker container, but when it tries to update some page, it uses getpass to read password from input:
Password for user BunykBot on wikipedia:uk (no characters will be shown): ^CWARNING: /usr/local/lib/python3.7/getpass.py:91: GetPassWarning: Can not control echo on the terminal.
passwd = fallback_getpass(prompt, stream)
Is there any way to give it password from some variable? I see that login.py script that creates .lwp file uses site.login() which uses api.LoginManager, but not gives it password anywhere, so it obtains it from input. Is there any way I could monkeypatch this with not much effort? Or do I need some updated fork of pywikibot?
So, I decided to go with monkey patching, and just created file that needs to be imported before pywikibot:
import os
def patch_wiki():
import pywikibot
original = pywikibot.input
def new_input(question, password=False, default='', force=False):
if password:
return os.getenv('WIKI_PASS')
return original(question, password, default, force)
pywikibot.input = new_input
if os.getenv('WIKI_PASS'):
patch_wiki()
Which works for me.
There are better options, like using BotPasswords or OAuth, see more in https://lists.wikimedia.org/pipermail/pywikibot/2019-December/009968.html
see https://phabricator.wikimedia.org/T248471 for a proposed patch to make password use from API much easier

Why does argparse fail to recognise an argument when a Python script is called directly?

I have a simple script like this (based on the docs for argparse):
def Main():
parser = argparse.ArgumentParser()
parser.add_argument("issuenumber", help="Create a local branch based on the specified issue number", type=int)
args = parser.parse_args()
if args.issuenumber:
print("Starting work on issue #"+str(args.issuenumber))
if __name__ == "__main__":
Main()
When I run it however, it never recognises the argument I'm passing it:
C:\Projects\PyTools>Gritter.py 1
usage: Gritter.py [-h] issuenumber
Gritter.py: error: the following arguments are required: issuenumber
If I call the script via a python call it works however:
C:\Projects\PyTools>python Gritter.py 1
Starting work on issue #1
If I print out sys.argv I get:
C:\Projects\PyTools>Gritter 1
['C:\\Projects\\PyTools\\Gritter.py']
C:\Projects\PyTools>Python Gritter.py 1
['Gritter.py', '1']
So I guess something is not passing on the arguments when the script is called directly. I wonder if there's anything that can be done so that the script can be called directly?
The C\ indicates you are using Windows. You have take extra effort to ensure that this 'direct call' passes arguments through to python.
Looking up windows shebang I find, from Python docs that you need to use
#!/usr/bin/python -v
to pass arguments
See https://docs.python.org/3/using/windows.html
argparse uses sys.argv. If that only has the script name then the call isn't passing arguments.
Based on mckoss` answer, I modified the following registry key:
HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command
From this:
"C:\Python34\python.exe" "%1"
To this:
"C:\Python34\python.exe" "%1" %*
And now my script works as I'd previously expected.

Resources