hey i get this error Ignoring exception in on_message Traceback (most recent call last): File "C:\Users\gamer\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 333, in _run_event await coro(*args, **kwargs) File "C:\Users\gamer\Desktop\New folder\bot.py", line 43, in on_message if msg.mentions[0] is not None: IndexError: list index out of range i get this error when i mention the bot and try and change the bots prefix
@@El_IrregularDX Put it in a try and except. @bot.event async def on_message(msg): try: if msg.mentions[0] == bot.user: with open("prefixes.json", "r") as f: prefixes = json.load(f)
pre = prefixes[str(msg.guild.id)] await msg.channel.send(f"My prefix for this server is {pre}") except: pass await bot.process_commands(msg)
Shouldnt be better make an if statement in the "get_prefix" checking if the custom prefix for that server exist, if not, use the default? so you dont have to add ALL the servers to the json? i dont know too much about code complexity or optimization so that why i'm asking
yup because the prefix set in json file with a event ;) before the prefix is juste a {} ^^' you need write manually, but no, you just have to invite your bot to set a prefix :)
Make Sure you have your code like this: def get_prefix(client,message): with open("prefixes.json", "r") as f: prefixes = json.load(f) return prefixes[str(message.guild.id)] client = commands.Bot(command_prefix = get_prefix, case_insensitive=True)
@@CodeWithSwastik by the way, i seem to be having some problems with the rich presence code? could you help me with this? thanks! #---- Changing Rich Presence ----# async def ch_pr(): await client.wait_until_ready() choices = ["with some very cool people | ^help", "on canvas 7 | ^help", f"with {len(client.guild.members)} Aeronians | ^help", "pixelplace.io with the bois | ^help", "Minecraft | ^help", "hide and seek in the Aeronic capitol | ^help", "with the mods | ^help", "Mu IDE | ^help", "^help"] choice = random.choice(choices) while not client.is_closed: await client.change_presence(activity=discord.Game(name=choice)) await asyncio.sleep(5) # rest of code client.loop.create_task(ch_pr()) client.run(TOKEN)
all works except when you change the prefix but dont specify a new one, it changes the prefix to "null" for your server which renders the bot useless as it doesnt work then
@@CodeWithSwastik bot can send user to the channel and delete it when it is empty by the help of audit logs. Like dnyo did user#0000 left the voice channel. Like that when the bot get that then it delete the channel but the reason is that don't know to get audit logs by the help of bot . So if I get this that how to get then I can do this.
I have a very small doubt, I am new to python. you have assigned the function 'get_prefix' in command_prefix without the parentheses, what is the difference between a function with and without parentheses?
Without the () you give it a reference to the function so it can call the function whenever it wants to get the prefix. You should read this for more info treyhunner.com/2020/01/passing-functions-as-arguments/
Hi. Love your tutorials. I would like to request, can you make a tutorial to show how to show how many servers your bot is in? Will be really helpful if you do as I am struggling with that one thing. Thanks! 👍
@@CodeWithSwastik I tried it. Is it like this though: @client.command() async def servers(ctx): await ctx.send("I'm in " + len(client.guild) + " servers!") but i got an error: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Bot' object has no attribute 'guild'
Yeah, I am getting the same error. I'm pretty sure it's because the bot can't find message.guild.id in the database. I just made a command that adds the prefix to all of the servers your bot is in. Delete it once you're done so people don't use it or make it so only you can use it. I hope this helps! @bot.command() async def mass_prefix(ctx): for guild in bot.guilds: with open("prefix.json", "r") as f: prefixes = json.load(f) prefixes[str(guild.id)] = "!" with open("prefix.json", "w") as f: json.dump(prefixes,f) print(f"Added the prefix `!` to {guild.name}!")
@@coler7975 this can help too. Basically what Coler six gamer said worked to me but in | async def on_ready |. I Full command looks like this (using bot instead of client) @bot.event async def on_ready(): for guild in bot.guilds: with open("databases/prefixes.json", "r") as f: prefixes = json.load(f) prefixes[str(guild.id)] = "#" with open("databases/prefixes.json", "w") as f: json.dump(prefixes,f) print(f"Added the prefix `#` to {guild.name}!")for guild in bot.guilds: look careful where you store the prefixes, in my case was | databases/prefixes.json |
Hi guys! In line 42 in the video It should have been ctx.guild and not just guild. So the line would be
prefixes[str(ctx.guild.id)] = prefix
Oookieee
LOL I watched the video 3 to 4 time to find out the error and when I saw the comments I got the error XD🤣
Somehow, you come out with every video that I've been needing!
Could you make more per-server settings?
Hope you enjoyed!
If you did subscribe😀😀
Hehehe
My prefix code was the exact same he
yea, but is there any source code?
Thanks so much I was having an issue with implementing custom prefixes into my bot, but this fixed it!
hey i get this error Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\gamer\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "C:\Users\gamer\Desktop\New folder\bot.py", line 43, in on_message
if msg.mentions[0] is not None:
IndexError: list index out of range
i get this error when i mention the bot and try and change the bots prefix
i get the same error
@@El_IrregularDX Put it in a try and except.
@bot.event
async def on_message(msg):
try:
if msg.mentions[0] == bot.user:
with open("prefixes.json", "r") as f:
prefixes = json.load(f)
pre = prefixes[str(msg.guild.id)]
await msg.channel.send(f"My prefix for this server is {pre}")
except:
pass
await bot.process_commands(msg)
Keep up the good work! :))
Thanks, you too!
Shouldnt be better make an if statement in the "get_prefix" checking if the custom prefix for that server exist, if not, use the default? so you dont have to add ALL the servers to the json? i dont know too much about code complexity or optimization so that why i'm asking
How to edit custom embed message with message id
what do i put in the package.json?
It's not working in my phone ,does it only work with pcs?
Help
Key error at line "return prefixes[str(message.guild.id)]
Read pinned msg
yup because the prefix set in json file with a event ;) before the prefix is juste a {} ^^' you need write manually, but no, you just have to invite your bot to set a prefix :)
in the add prefix command, guild is not found?
Do ctx.guild
Instead of replacing a prefix, what would it be like to add one more?
0:03 nosql vs sql?
great vids :) been helping me alot
Thanks
Is it needed to invite the bot again
When are you posting the database one?
client = commands.Bot(command_prefix = get_prefix)
NameError: name 'get_prefix' is not defined
Do u put the part "def get_prefix" above client = commands.Bot(command_prefix = get_prefix) ?
Make Sure you have your code like this:
def get_prefix(client,message):
with open("prefixes.json", "r") as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
client = commands.Bot(command_prefix = get_prefix,
case_insensitive=True)
It is not working for me
I need help someone please help in line 27 it's saying cannot assign to function call
vs code:
client is not defined
*moves def prefix below client*
get prefix is not defined
Can you make a video on COGS? I've watched a few videos online, but none of them were that good.
ping thing not working how to fix
Awesome! These vids have helped me immensely with my bot. By the way, I think there is a problem your microphone; it seems a bit fuzzy right now
Ah thanks I'll get a better mic soon
@@CodeWithSwastik by the way, i seem to be having some problems with the rich presence code? could you help me with this? thanks!
#---- Changing Rich Presence ----#
async def ch_pr():
await client.wait_until_ready()
choices = ["with some very cool people | ^help",
"on canvas 7 | ^help",
f"with {len(client.guild.members)} Aeronians | ^help",
"pixelplace.io with the bois | ^help",
"Minecraft | ^help",
"hide and seek in the Aeronic capitol | ^help",
"with the mods | ^help",
"Mu IDE | ^help",
"^help"]
choice = random.choice(choices)
while not client.is_closed:
await client.change_presence(activity=discord.Game(name=choice))
await asyncio.sleep(5)
# rest of code
client.loop.create_task(ch_pr())
client.run(TOKEN)
@@xianthulaman712 I know im late(also kinda suck with bots) but did you define TOKEN, also can u show me the error?
How do you get f to work? It just says undefined variable
How is that possible
it says it wrong
Is there a GitHub link pls?
I am late but your videos are so amazing
all works except when you change the prefix but dont specify a new one, it changes the prefix to "null" for your server which renders the bot useless as it doesnt work then
i have now found a fix to this so dont worry!
What if my prefix is alphabets? How to make it case insensitive?
put case_insensitive=True after the command_prefix in your client.
my bot is in 90 servers should i do it pls suggest me
90 is good
How many servers are considered a lot of servers? Over 100 or over 500?
dude 100 is max ;-;
@@Hiphop602y he means for the bot.
under 1k should be ok
@@CodeWithSwastik under 1 thousand discord servers?
Great vid! How would you go about adding a database to the code instead of using the .json file?
th-cam.com/video/fnsN1HZLyrk/w-d-xo.html
Cool video! If it had been posted a bit earlier my work would have been less. GoOD JOB THO~!
PogSwastik
Can you also start giving github source codes or something? I'd appreciate that:)
Could you make a tutorial on how to make a BOT create a voice channel, send users to the channel, and then delete it when the channel is empty?
You can't send users to a channel unless they are already in one. And I'll see if I can make a video on that
@@CodeWithSwastik bot can send user to the channel and delete it when it is empty by the help of audit logs. Like dnyo did user#0000 left the voice channel. Like that when the bot get that then it delete the channel but the reason is that don't know to get audit logs by the help of bot . So if I get this that how to get then I can do this.
deserves a like :)
Thank you so much! :)
@Code With Swastik can you tell how to make per command prefix. Pls Bro!
Hey Guys!!! ITSSSSS SWASTIK!
I have a very small doubt, I am new to python.
you have assigned the function 'get_prefix' in command_prefix without the parentheses, what is the difference between a function with and without parentheses?
Without the () you give it a reference to the function so it can call the function whenever it wants to get the prefix. You should read this for more info treyhunner.com/2020/01/passing-functions-as-arguments/
Video is super
Thanks
swear filter plz
Get an air filter and swear through it. Swear filter.
Big brain
I keep getting this error ">>> & C:/Users/Joe/AppData/Local/Programs/Python/Python39/python.exe "n:/Documents/Discord Bot/Bot.py"
File "", line 1
& C:/Users/Joe/AppData/Local/Programs/Python/Python39/python.exe "n:/Documents/Discord Bot/Bot.py"
^
SyntaxError: invalid syntax"
:PogBruh:
Why always custom prefixes, just freaking show how to create a prefix
Your mic quality went from good to bad.
I have been using the same mic for some time now, not sure why the quality decreased 🤔
Code With Swastik the mic’s ribbon must’ve deformed or degraded.
Nice
Lol
good vedio... but you were going a bit too fast
Спасибо!/Thank!
Lel that was the exact code I had before lol.
Lol
Thanks
This is the 100th comment
Hi. Love your tutorials. I would like to request, can you make a tutorial to show how to show how many servers your bot is in? Will be really helpful if you do as I am struggling with that one thing. Thanks! 👍
In this video itself I showed that, but I'll write it again:- len(client.guilds)
@@CodeWithSwastik I tried it. Is it like this though:
@client.command()
async def servers(ctx):
await ctx.send("I'm in " + len(client.guild) + " servers!")
but i got an error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Bot' object has no attribute 'guild'
@@icey6804 its guilds not guild also since that is a number you should convert it to a string, so
str(len(client.guilds))
@@CodeWithSwastik oh oops, yea thanks man. ahaha, sorry for the trouble and thx for your help!
hi
nice it helped
Glad to hear!
return prefixes[str(message.guild.id)]
KeyError: ' '
Im getting problems like the...
Yeah, I am getting the same error. I'm pretty sure it's because the bot can't find message.guild.id in the database. I just made a command that adds the prefix to all of the servers your bot is in. Delete it once you're done so people don't use it or make it so only you can use it. I hope this helps!
@bot.command()
async def mass_prefix(ctx):
for guild in bot.guilds:
with open("prefix.json", "r") as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = "!"
with open("prefix.json", "w") as f:
json.dump(prefixes,f)
print(f"Added the prefix `!` to {guild.name}!")
@@coler7975 this can help too. Basically what Coler six gamer said worked to me but in | async def on_ready |. I Full command looks like this (using bot instead of client)
@bot.event
async def on_ready():
for guild in bot.guilds:
with open("databases/prefixes.json", "r") as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = "#"
with open("databases/prefixes.json", "w") as f:
json.dump(prefixes,f)
print(f"Added the prefix `#` to {guild.name}!")for guild in bot.guilds:
look careful where you store the prefixes, in my case was | databases/prefixes.json |
@@jmonsa You helped me a lot. Thanks
@@th__gmr btw i sued "databases/prefixes.json", consider creating a folder called "databases or simply do open("prefixes.json"....)
@@jmonsa Yes, I know. I don't just copy and pasted it 😅
how i can do if setprefix doesnt have no one word, send "you doesnt have permissions"?