Per Server Custom Prefixes in Discord.py | Making a discord bot Part: 9

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ธ.ค. 2024

ความคิดเห็น • 114

  • @CodeWithSwastik
    @CodeWithSwastik  4 ปีที่แล้ว +51

    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

    • @dhruvakikkeri6217
      @dhruvakikkeri6217 3 ปีที่แล้ว +1

      Oookieee

    • @ifreaku
      @ifreaku 3 ปีที่แล้ว +1

      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🤣

  • @gamingwithgrubby7248
    @gamingwithgrubby7248 4 ปีที่แล้ว +14

    Somehow, you come out with every video that I've been needing!

  • @terabyte.9774
    @terabyte.9774 4 ปีที่แล้ว +10

    Could you make more per-server settings?

  • @CodeWithSwastik
    @CodeWithSwastik  4 ปีที่แล้ว +7

    Hope you enjoyed!
    If you did subscribe😀😀

    • @VdocVdoc
      @VdocVdoc 4 ปีที่แล้ว

      Hehehe

    • @VdocVdoc
      @VdocVdoc 4 ปีที่แล้ว

      My prefix code was the exact same he

    • @remy9593
      @remy9593 3 ปีที่แล้ว

      yea, but is there any source code?

  • @Robert-mx5wp
    @Robert-mx5wp 3 ปีที่แล้ว +2

    Thanks so much I was having an issue with implementing custom prefixes into my bot, but this fixed it!

  • @gamerdog6994
    @gamerdog6994 4 ปีที่แล้ว +2

    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
      @El_IrregularDX 4 ปีที่แล้ว

      i get the same error

    • @ilikefivem5162
      @ilikefivem5162 2 ปีที่แล้ว

      @@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)

  • @Nick-te5jg
    @Nick-te5jg 4 ปีที่แล้ว +9

    Keep up the good work! :))

  • @lizzieaoi7140
    @lizzieaoi7140 3 ปีที่แล้ว +1

    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

  • @JOKERASHGAMING
    @JOKERASHGAMING 3 ปีที่แล้ว +1

    How to edit custom embed message with message id

  • @EmpireAngels
    @EmpireAngels 2 ปีที่แล้ว

    what do i put in the package.json?

  • @amanshaikh6795
    @amanshaikh6795 2 ปีที่แล้ว

    It's not working in my phone ,does it only work with pcs?

  • @yudatriananda3558
    @yudatriananda3558 4 ปีที่แล้ว +2

    Help
    Key error at line "return prefixes[str(message.guild.id)]

    • @frostiiify9505
      @frostiiify9505 4 ปีที่แล้ว

      Read pinned msg

    • @zebrra4499
      @zebrra4499 3 ปีที่แล้ว

      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 :)

  • @imgreen4233
    @imgreen4233 4 ปีที่แล้ว +2

    in the add prefix command, guild is not found?

  •  2 ปีที่แล้ว

    Instead of replacing a prefix, what would it be like to add one more?

  • @imthesrpina
    @imthesrpina 10 หลายเดือนก่อน

    0:03 nosql vs sql?

  • @SGLLIVETV
    @SGLLIVETV 4 ปีที่แล้ว +1

    great vids :) been helping me alot

  • @romad8118
    @romad8118 2 ปีที่แล้ว

    Is it needed to invite the bot again

  • @enrique4012
    @enrique4012 3 ปีที่แล้ว

    When are you posting the database one?

  • @waiz8258
    @waiz8258 4 ปีที่แล้ว +1

    client = commands.Bot(command_prefix = get_prefix)
    NameError: name 'get_prefix' is not defined

    • @runger6432
      @runger6432 4 ปีที่แล้ว +1

      Do u put the part "def get_prefix" above client = commands.Bot(command_prefix = get_prefix) ?

    • @Chaos0945
      @Chaos0945 3 ปีที่แล้ว

      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)

  • @MontagemEclipse
    @MontagemEclipse 2 ปีที่แล้ว

    It is not working for me

  • @simatiwari2267
    @simatiwari2267 3 ปีที่แล้ว

    I need help someone please help in line 27 it's saying cannot assign to function call

  • @lucaless3135
    @lucaless3135 3 ปีที่แล้ว

    vs code:
    client is not defined
    *moves def prefix below client*
    get prefix is not defined

  • @peanutbutter291
    @peanutbutter291 4 ปีที่แล้ว

    Can you make a video on COGS? I've watched a few videos online, but none of them were that good.

  • @jai6470
    @jai6470 3 ปีที่แล้ว

    ping thing not working how to fix

  • @xianthulaman712
    @xianthulaman712 4 ปีที่แล้ว

    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

    • @CodeWithSwastik
      @CodeWithSwastik  4 ปีที่แล้ว

      Ah thanks I'll get a better mic soon

    • @xianthulaman712
      @xianthulaman712 4 ปีที่แล้ว +1

      @@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)

    • @TheRapidDev
      @TheRapidDev 3 ปีที่แล้ว

      @@xianthulaman712 I know im late(also kinda suck with bots) but did you define TOKEN, also can u show me the error?

  • @supergamer-nc7hv
    @supergamer-nc7hv 4 ปีที่แล้ว

    How do you get f to work? It just says undefined variable

  • @glitch7782
    @glitch7782 3 ปีที่แล้ว +1

    it says it wrong

  • @W1L7dev
    @W1L7dev 3 ปีที่แล้ว

    Is there a GitHub link pls?

  • @girijapaikra1654
    @girijapaikra1654 3 ปีที่แล้ว

    I am late but your videos are so amazing

  • @tykester5013
    @tykester5013 3 ปีที่แล้ว

    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

    • @tykester5013
      @tykester5013 3 ปีที่แล้ว

      i have now found a fix to this so dont worry!

  • @mhui410
    @mhui410 3 ปีที่แล้ว

    What if my prefix is alphabets? How to make it case insensitive?

    • @porcopion
      @porcopion 3 ปีที่แล้ว

      put case_insensitive=True after the command_prefix in your client.

  • @jaysurana5073
    @jaysurana5073 4 ปีที่แล้ว

    my bot is in 90 servers should i do it pls suggest me

    • @waiz8258
      @waiz8258 4 ปีที่แล้ว

      90 is good

  • @_cosmos_4219
    @_cosmos_4219 4 ปีที่แล้ว

    How many servers are considered a lot of servers? Over 100 or over 500?

    • @Hiphop602y
      @Hiphop602y 4 ปีที่แล้ว +1

      dude 100 is max ;-;

    • @mujtabanaveed3583
      @mujtabanaveed3583 4 ปีที่แล้ว

      @@Hiphop602y he means for the bot.

    • @CodeWithSwastik
      @CodeWithSwastik  4 ปีที่แล้ว

      under 1k should be ok

    • @_cosmos_4219
      @_cosmos_4219 4 ปีที่แล้ว +1

      @@CodeWithSwastik under 1 thousand discord servers?

  • @dickeyy
    @dickeyy 3 ปีที่แล้ว

    Great vid! How would you go about adding a database to the code instead of using the .json file?

    • @EmpireAngels
      @EmpireAngels 2 ปีที่แล้ว

      th-cam.com/video/fnsN1HZLyrk/w-d-xo.html

  • @spookyyy6603
    @spookyyy6603 4 ปีที่แล้ว

    Cool video! If it had been posted a bit earlier my work would have been less. GoOD JOB THO~!

  • @okt5805
    @okt5805 4 ปีที่แล้ว +3

    PogSwastik

  • @cometdiep
    @cometdiep 2 ปีที่แล้ว

    Can you also start giving github source codes or something? I'd appreciate that:)

  • @whattabadjackal9571
    @whattabadjackal9571 4 ปีที่แล้ว

    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?

    • @CodeWithSwastik
      @CodeWithSwastik  4 ปีที่แล้ว +1

      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

    • @kshamasrivastava5846
      @kshamasrivastava5846 4 ปีที่แล้ว

      @@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.

  • @rjm2439
    @rjm2439 3 ปีที่แล้ว

    deserves a like :)

  • @friendlyware2132
    @friendlyware2132 3 ปีที่แล้ว

    Thank you so much! :)

  • @bhaskarpatel4608
    @bhaskarpatel4608 4 ปีที่แล้ว

    @Code With Swastik can you tell how to make per command prefix. Pls Bro!

  • @Hiphop602y
    @Hiphop602y 4 ปีที่แล้ว

    Hey Guys!!! ITSSSSS SWASTIK!

  • @sauceontoes3457
    @sauceontoes3457 3 ปีที่แล้ว

    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?

    • @CodeWithSwastik
      @CodeWithSwastik  3 ปีที่แล้ว

      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/

  • @srisurya9410
    @srisurya9410 4 ปีที่แล้ว +1

    Video is super

  • @ae.cooper
    @ae.cooper 4 ปีที่แล้ว

    swear filter plz

    • @tapu_
      @tapu_ 4 ปีที่แล้ว +2

      Get an air filter and swear through it. Swear filter.

    • @CodeWithSwastik
      @CodeWithSwastik  4 ปีที่แล้ว +1

      Big brain

  • @joe_dodman
    @joe_dodman 3 ปีที่แล้ว

    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"

  • @neerukohli353
    @neerukohli353 4 ปีที่แล้ว +1

    :PogBruh:

  • @thenewido
    @thenewido 2 ปีที่แล้ว

    Why always custom prefixes, just freaking show how to create a prefix

  • @lazerz_
    @lazerz_ 4 ปีที่แล้ว

    Your mic quality went from good to bad.

    • @CodeWithSwastik
      @CodeWithSwastik  4 ปีที่แล้ว

      I have been using the same mic for some time now, not sure why the quality decreased 🤔

    • @lazerz_
      @lazerz_ 4 ปีที่แล้ว

      Code With Swastik the mic’s ribbon must’ve deformed or degraded.

  • @mambamentality1626
    @mambamentality1626 4 ปีที่แล้ว +1

    Nice

  • @yashvardhanhalwasiya1624
    @yashvardhanhalwasiya1624 3 ปีที่แล้ว

    good vedio... but you were going a bit too fast

  • @ztxxi
    @ztxxi 3 ปีที่แล้ว

    Спасибо!/Thank!

  • @VdocVdoc
    @VdocVdoc 4 ปีที่แล้ว +1

    Lel that was the exact code I had before lol.

  • @VdocVdoc
    @VdocVdoc 4 ปีที่แล้ว +1

    Thanks

  • @stellam404
    @stellam404 3 ปีที่แล้ว +1

    This is the 100th comment

  • @icey6804
    @icey6804 4 ปีที่แล้ว

    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
      @CodeWithSwastik  4 ปีที่แล้ว

      In this video itself I showed that, but I'll write it again:- len(client.guilds)

    • @icey6804
      @icey6804 4 ปีที่แล้ว +1

      @@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'

    • @CodeWithSwastik
      @CodeWithSwastik  4 ปีที่แล้ว

      @@icey6804 its guilds not guild also since that is a number you should convert it to a string, so
      str(len(client.guilds))

    • @icey6804
      @icey6804 4 ปีที่แล้ว

      @@CodeWithSwastik oh oops, yea thanks man. ahaha, sorry for the trouble and thx for your help!

  • @isabelaarmy2991
    @isabelaarmy2991 3 ปีที่แล้ว

    hi

  • @malligalliblaa9025
    @malligalliblaa9025 4 ปีที่แล้ว

    nice it helped

  • @vspython3385
    @vspython3385 3 ปีที่แล้ว +2

    return prefixes[str(message.guild.id)]
    KeyError: ' '
    Im getting problems like the...

    • @coler7975
      @coler7975 3 ปีที่แล้ว +1

      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}!")

    • @jmonsa
      @jmonsa 3 ปีที่แล้ว

      ​@@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 |

    • @th__gmr
      @th__gmr 3 ปีที่แล้ว

      @@jmonsa You helped me a lot. Thanks

    • @jmonsa
      @jmonsa 3 ปีที่แล้ว

      @@th__gmr btw i sued "databases/prefixes.json", consider creating a folder called "databases or simply do open("prefixes.json"....)

    • @th__gmr
      @th__gmr 3 ปีที่แล้ว

      @@jmonsa Yes, I know. I don't just copy and pasted it 😅

  • @maikeldestroier6828
    @maikeldestroier6828 3 ปีที่แล้ว

    how i can do if setprefix doesnt have no one word, send "you doesnt have permissions"?