How to remove or change the default help command? - python

How do you remove or at least change the format of the default help command in discord.py?
I think changing the format would be nice, I don't really like the format at all.

Try this:
bot.remove_command('help')
Put this at the top of your code, after your imports.
Then create your own.
Or to format it check this out: Click here!

The proper way to disable the help command according to the docs is to pass help_command=None into the constructor for discord.ext.commands.Bot, such as:
bot = commands.Bot(help_command=None)
or
class MyBot(commands.Bot):
def __init__(self):
super().__init__(help_command=None)
This also allows you the opportunity to pass your own help function into the help_command argument for different formatting.

You will need to remove the command for example
client.remove_command('help')
you will need to put it under
client = commands.Bot
it will be like
client = commands.Bot(command_prefix = 'somethingelse')
client.remove_command('help')

Here you can use this:
intents = discord.Intents.all()
activity = discord.Game(name=f"!help in {len(client.guilds)} servers!")
client = commands.Bot(command_prefix="!", intents=intents, activity=activity, status=discord.Status.do_not_disturb, help_command=None)

This is how you should do it so that it preserves the behavior of the help command while letting you change how it looks:
class MyHelpCommand(commands.MinimalHelpCommand):
def get_command_signature(self, command):
return '{0.clean_prefix}{1.qualified_name} {1.signature}'.format(self, command)
class MyCog(commands.Cog):
def __init__(self, bot):
self._original_help_command = bot.help_command
bot.help_command = MyHelpCommand()
bot.help_command.cog = self
def cog_unload(self):
self.bot.help_command = self._original_help_command```
See the documentation: https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#help-commands for more details.
For migrating from old helpformatters: https://discordpy.readthedocs.io/en/rewrite/migrating.html#helpformatter-and-help-command-changes

You don't really need to remove the command... It isn't good, using the (prefix)help commandname <- It wont appear then... If you want it embed you can do.
class NewHelpName(commands.MinimalHelpCommand):
async def send_pages(self):
destination = self.get_destination()
for page in self.paginator.pages:
emby = discord.Embed(description=page)
await destination.send(embed=emby)
client.help_command = NewHelpName()```
The built in help command is of great use

Related

Basics - How do I output random.sample to newlines for a Discord bot?

My first time doing any coding, so apologies in advance.
I'm making a Discord bot for fun and am trying to get random.sample to output each selection to a new line.
Currently looks like this
Need it to look like:
Seed the Rebellion
Wreck the Place
Burn it Down
Code here:
import disnake
import random
from disnake.ext import commands
from variables import IntriguesList
class IntrigueCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
#commands.slash_command(guild_ids= [guildID])
async def intrigue(interaction: disnake.ApplicationCommandInteraction):
"""Generate Intrigues for an upcoming battle"""
await interaction.response.defer(with_message=True)
intrigue = disnake.Embed(
title="Intrigues",
description="Three side-missions for your next battle",
color = disnake.Colour.dark_green()
)
intrigue.add_field(name="Your Intrigues", value=f"{(random.sample(IntriguesList, 3))}")
Tried \n in various ways, but don't really know what I'm doing so help greatly appreciated!
You can use \n to enter a newline.
sample = random.sample(IntriguesList, 3)
sample_str = "\n".join(sample)
intrigue.add_field(name="Your Intrigues", value=sample_str)

Need telegram bot to edit a function from another file

As the title says. I need my telegram bot to take user input, and use that to change some values on another function from another file. I already got the file to be successfully run from the bot, but I can't figure out how to change values first. I am using Python-Telegram-bot.
here is the code I need to edit that is in a separate file (call.py)
call = client.calls.create(
machine_detection='Enable',
url='https://ngrok.io/main',
to='',
from_=''
)
I need to edit the "to" and "from" field(s) in this code above.
The code I use to run this from my bot is as follows:
def update(update, context):
update.message.reply_text('Enter number :\n'
'e.g. 18004585478\n')
update.message.reply_text('Calling...')
exec(open("call.py").read())
I am pretty new to all this so I know the code is not good at all. I have read that I should be using ConversationHandler or CommandHandler but I honestly am not sure how to implement it.
I edited the code based on what Alexey suggested and now am stuck on a similar issue.
def update(update, context):
update.message.reply_text('Enter number:\n'
'e.g. 18004585478\n'
'Number Must begin with 1')
from_number = update.message.text
update.message.reply_text('Enter number:\n'
'e.g. 18004585478\n'
'Number Must begin with 1')
to_number = update.message.text
update.message.reply_text('Calling...')
call_state = call.make_call(to_number, from_number)
The Telegram bot just runs all the code at once, it doesn't stop and wait for any input from the number fields. How do I go about implementing MessageHandler to make the bot stop and accept input to pass along to call_state, then execute call_state at the end?
You don't need to change the code, you need to use arguments to pass the data you wanted to.
In call.py you can make a funciton
def make_call(to_number, from_number):
call = client.calls.create(
machine_detection='Enable',
url='https://ngrok.io/main',
to=to_number,
from=from_number,
)
return call
In your update function just use the function by giving it the necessary values
import call
def update(update, context):
update.message.reply_text('Enter number :\n'
'e.g. 18004585478\n')
update.message.reply_text('Calling...')
call_state = call.make_call(to_number='0123456789', from_number='9876543210')
# use call_state ...
What Alexey stated ended up working with very slight modifications.
I took what Alexey posted and deleted the numbers and turned them into a variable I could edit from my other script.
def make_call(to_number, from_number):
call = client.calls.create(
machine_detection='Enable',
url='https:snip/main',
to=to_number,
from_=from_number
)
print(call.sid)
Then in the other file I defined the variables and executed them by importing the file I needed to edit by using user_data[FROM] = update.message.text and user_data[TO] = update.message.text.
then calling the funciton.
call_state = call.make_call({user_data[TO]}, {user_data[FROM]})
Dont forget to add user_data = {} at the top of your code.

Deformat text in discord.py

I want to remove the formatting from text in discord.py - adding \ before *'s, ```'s etc. I have not yet been able to come up with a perfect solution.
Can anyone tell me what I could use?
I use discord.py and python 3
If this is in a command you can use commands.clean_content
#bot.command()
async def test(ctx, arg: commands.clean_content(fix_channel_mentions=False, use_nicknames=True, escape_markdown=True, remove_markdown=False)):
await ctx.send(arg)
All params are kwarg only and optional, for more info see the docs
If you want to escape markdown for some other text (e.g. from an API), you can use utils.escape_markdown
import discord
text = "Hello my name is **Wasi**"
print(discord.utils.escape_markdown(text))
# 'Hello my name is \*\*Wasi\*\*'
There is also utils.escape_mentions for removing mentions
Discord.py provides a utility function: discord.utils.escape_markdown
This converts:
#```python
#print("hello")
#```
#**bold** *italics*
into
#\`\`\`python
#print("hello")
#\`\`\`
#\*\*bold\*\* \*italics\*

Wikipedia Command Discord.py

#commands.command()
#commands.cooldown(1,10,BucketType.user)
async def wiki(self,ctx,*,word):
def sum(arg):
definition = wikipedia.summary(arg,sentences=3,chars=1000)
return sum(word)
await ctx.send(sum)
I am making a Wiki command, it doesn't work as expected and responds with this:
That's because you are using sum the wrong way. Instead of this:
await ctx.send(sum)
you have to do this:
await ctx.send(sum(word))
Also, why are you invoking the function from within itself? In sum you should return definition.
Try not to shadow the builtin sum. You should rename the function to be get_definition or something else instead.

Discord.py: How do I get the user name from the user ID

Yeah, so I got the user ID of the Users, but I'm unable to find code to get the user name, like I tried everything like client.fetch_user(payload.user_id) and client.get_user(user_id) but it won't work.
Here is the code:
#client.command(pass_context = True)
async def test(ctx,id):
print(getname(id))
def getname(a):
return client.get_user(a).name
thanks.
If you're trying to have have it in a different function then you can do:
async def getname(ctx):
user = await ctx.author.guild.fetch_member(id)
return user
If you're trying to get the name specifically try adding .name behind ctx.author.guild.fetch_member(id)
Maybe this is the answer you're looking for?

Categories

Resources