'NameError: name 'Option' is not defined
I'm writing a discord bot via py-cord and I get NameError: name 'Option' is not defined. Using pre-release version. Extensions are added.
Code:
import discord
from discord.ext import commands
bot = discord.Bot()
@bot.slash_command(guild_ids=[...])
async def hello(
ctx: discord.ApplicationContext,
name: Option(str, "Enter your name"),
age: Option(int, "Enter your age", min_value=1, max_value=99, default=18)
# passing the default value makes an argument optional
# you also can create optional argument using:
# age: Option(int, "Enter your age") = 18
):
await ctx.respond(f"Hello! Your name is {name} and you are {age} years old.")
bot.run('real token is here')
Solution 1:[1]
Simple fix for this is instead of Option, just do
name: discord.Option(str, "Enter your name"),
age: discord.Option(int, "Enter your age", min_value=1, max_value=99, default=18)
or:
from discord import Option
and it'll be fixed! Hope this helps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user18478673 |
