args = message.content.split()
category = await message.guild.create_category(args[1], overwrites=None, reason=None, position=0)
I'm trying to create a category with the name I got from a message, but nothing happened.
If I use this code it works perfectly
category = await message.guild.create_category("Category", overwrites=None, reason=None, position=0)
Note: the command isn't returning an error its just not creating the category
Assuming you're making commands, you should be using discord.ext.commands.
Anyways, assuming that args is the text after the command, list indices start at 0. Therefore, args[0] returns thr first argument, args[1] returns the second, etc.
You need to make sure it's a string value. You can do that by using str(arg[1]). Do this:
args = message.content.split()
category = await message.guild.create_category(str(args[1]), overwrites=None, reason=None, position=0)
Related
I am trying to to create a command that that allows a user to select multiple options and then put them in an embed. So far I have been able to do this with one option but I want to do it with more as well. Is there a way to add multiple SlashOptions to a slash command and if so how might it be done. Thank you for all your help.
Here is what I currently have
async def announce(interaction: Interaction, buysell:str = SlashOption(
name="bto-stc",
choices={"BTO": "BTO", "STC": "STC"}),
stock:str,):
embed = nextcord.Embed(description=f"**Options Entry**", color=0x00FF00)
embed.add_field(name=f"** [🎟️] Contract:** *{buysell}* *{stock}*", value="⠀", inline=False)
await interaction.response.send_message(embed=embed, ephemeral=False)```
Here is what I would like the end product to look like.
https://i.stack.imgur.com/9SmIK.png
Your stock argument must be before your choices argument as for Python this argument is and optional argument and you can't have required arguments before optionals ones.
Also if you don't want your buysell argument to be optional you need to add a required argument set to True in your SlashOption object. And in your case you can also replace the choices argument in the SlashOptionobject by a list as the choices and the data you want from them are the same.
Example:
async def announce(interaction: Interaction, stock:str, buysell:str = SlashOption(
name="bto-stc",
choices=["BTO", "STC"],
required=True)):
embed = nextcord.Embed(description=f"**Options Entry**", color=0x00FF00)
embed.add_field(name=f"** [🎟️] Contract:** *{buysell}* *{stock}*", value="⠀", inline=False)
await interaction.response.send_message(embed=embed, ephemeral=False)
I was looking at the API Reference and I found fetch_ban(user). How can I check if the user is banned from the server, I was reading that it returns the the BanEntry, and get a boolean? Can I use member as well or I need to get the user?
Thank you for any reply.
Tip: Always link what you're talking about.
fetch_ban
BanEntry (discord.py source code)
If you go through the source code you will very quickly find this in the first lines:
BanEntry = namedtuple('BanEntry', 'reason user')
Returned is a BanEntry object if the user is banned, otherwise it returns a NotFound Exception.
So to check if a user is banned just do:
async def is_banned(guild, user):
try:
entry = await guild.fetch_ban(user)
except discord.NotFound:
return False
return True
This will also work with members, as they are basically user objects with a bit of extra.
BanEntry is a named tuple (if you need a refresher on those here).
if you wanna command that sending list of banned users
async def banlist(self, ctx):
bans = await ctx.guild.bans()
loop = [f"{u[1]} ({u[1].id})" for u in bans]
_list = "\r\n".join([f"[{str(num).zfill(2)}] {data}" for num, data in enumerate(loop, start=1)])
await ctx.send(f"```ini\n{_list}```")
it gives list like this
[01] 尸仁长仈乃冂仨#0529 (269800030300033098)
[02] Yako#1001 (294113773333557952)
[03] Ping#9216 (46804048093530418)
[04] Vasky#6978 (494069478291921344)
I'm building a Telegram bot that uses ConversationHandler to prompt the user for a few parameters and settings about how the bot should behave. This information is stored in some global variables since it needs to be available and editable by different functions inside the program. Every global variable is a dictionary in which each user is associated with its own value. Here's an example:
language = {123456: 'English', 789012: 'Italian'}
where 123456 and 789012 are user ids obtained from update.message.from_user.id inside each function.
After all the required information has been received and stored, the bot should send a message containing a text fetched from a web page; the text on the web page is constantly refreshed, so I want the message to be edited every 60 seconds and updated with the new text, until the user sends the command /stop.
The first solution that came to my mind in order to achieve this was something like
info_message = bot.sendMessage(update.message.chat_id, text = "This message will be updated...")
...
def update_message(bot, update):
while True:
url = "http://example.com/etc/" + language[update.message.from_user.id]
result = requests.get(url).content
bot.editMessageText(result, chat_id = update.message.chat_id, message_id = info_message.message_id)
time.sleep(60)
Of course that wouldn't work at all, and it is a really bad idea. I found out that the JobQueue extension would be what I need. However, there is something I can't figure out.
With JobQueue I would have to set up a callback function for my job. In my case, the function would be
def update_message(bot, job):
url = "http://example.com/etc/" + language[update.message.from_user.id]
result = requests.get(url).content
bot.editMessageText(result, chat_id = update.message.chat_id, message_id = info_message.message_id)
and it would be called every 60 seconds. However this wouldn't work either. Indeed, the update parameter is needed inside the function in order to fetch the page according to the user settings and to send the message to the correct chat_id. I'd need to pass that parameter to the function along with bot, job, but that doesn't seem to be possible.
Otherwise I would have to make update a global variable, but I thought there must be a better solution. Any thoughts? Thanks.
I had the same issue. A little digging into the docs revealed that you can pass job objects a context parameter which can then be accessed by the callback function as job.context.
context (Optional[object]) – Additional data needed for the callback function. Can be accessed through job.context in the callback. Defaults to None
global language
language = {123456: 'English', 789012: 'Italian'}
j=updater.job_queue
context={"chat_id":456754, "from_user_id":123456, "message_id":111213}
update_job = job(update_message, 60, repeat=True, context=context)
j.put(update_job, next_t=0.0)
def update_message(bot, job):
global language
context=job.context
url = "http://example.com/etc/" + language[context["from_user_id"]]
result = requests.get(url).content
bot.editMessageText(result,
chat_id = context["chat_id"],
message_id = context["message_id"])
I'm using a script exactly like the one on the tutorial here, https://developers.google.com/apps-script/reference/ui/file-upload
However, despite using the syntax I keep getting e is undefined in the statement:
var fileBlob = e.parameter.dsrFile;
I think that means my function doPost(e) is probably wrong somehow. Here is my entire script below.
// Create Menu to Locate .CSV
function doGet(e) {
var app = UiApp.createApplication().setTitle("Upload CSV");
var formContent = app.createVerticalPanel();
formContent.add(app.createFileUpload().setName("dsrFile"));
formContent.add(app.createSubmitButton("Start Upload"));
var form = app.createFormPanel();
form.add(formContent);
app.add(form);
return app;
}
// Upload .CSV file
function doPost(e)
{
// data returned is a blob for FileUpload widget
var fileBlob = e.parameter.dsrFile;
var doc = DocsList.createFile(fileBlob);
}
e is undefined because you are not passing anything to doPost. You have to pass the needed object to doPost. Check where you call the function and what parameters do you pass to it if any. Even if you pass a parameter to that function, it holds undefined value. Make sure that you are passing the correct objects to your functions.
Your script should work perfectly. e is defined by Google Apps Script, not need to pass anything in particular is contains the fields of your form, in particular in this case the file you uploaded.
I would suspect you may be falling foul to the dev url vs publish url syndrome, where you are executing an old scrip rather that the code you are currently working on.
Be sure you script end with 'dev' and not 'exec'
https://script.google.com/a/macros/appsscripttesting.com/s/AKfyck...EY7qzA7m6hFCnyKqg/dev
Let me know if you are still getting the error after running it from the /dev url
In my game, I have an __init__ function which creates a set of seven entry boxes, like so:
self.string1 = StringVal()
self.entry1 = Entry(frame, textvariable = self.string1).grid(row = 4, column = 1, sticky = W)
This is copied six more times. This works.
At the end of the game, though, I want to delete the Entry box's text, using this code I found several places online:
self.entry1.delete(0, END)
I also tried using something else I found:
if self.entry1.get():
self.entry1.delete(0, END)
These both say that self.entry1 is a NoneType object, and has no method .get() or .delete(). Just to try things out, I substituted self.entry1.get() and self.entry1.delete(0,END) with self.string1.get(), etc. I also tried substituting .delete(0, END) with .delete(0.0, END). Neither of these worked either. I do not understand what I am doing wrong.
Thanks for your help!
When you do something like this:
self.foo = Button(...).grid(...)
... Then what gets stored in self.foo is the result of the call to grid(). This will always be None. You need to separate your widget creation from the loyout in order to save a reference to the created widgets.