I was using replit (maybe because replit doesnt work?) tryoing to make some code for a Discord bot when I saw this:
Type error: __init__ missing 1 required keyword-only argument: "intents"
Im not really sure what is means
Heres my code (BTW i used pip install discord in shell)
`
import discord
token = "mytoken (not revealing it i guess)"# but even with right token it doesnt work
client = discord.Client()
name = "MIIB.BOT_v1.0"
#client.event
async def on_ready():
print("Bot logged in as ", name, "!")
client.run(token)
on_ready()
#i did *****pip install discord****** in shell btw
`
I tried some variations but not much. I expected:
Bot logged in as botname#6969
Replace
client = discord.Client()
with
client = discord.Client(intents=discord.Intents.default())
As per recently(-ish), you need to specify the intents your bot uses when login to the API, so you should populate the Client argument with an intent object.
Calling the default intents, if you don't plan on doing anything with any other that you might need to enable, would look like this
bot_intents = discord.Intents.default()
client = discord.Client(intents=bot_intents)
Okay so I wanted to make a discord bot for my first python project. However, despite my seemingly correct code, the bot doesn't run:
I used this basic code to simply get the bot online but nothing happens. I haven't seen anyone else have this problem either.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("Bot is ready")
client.run("Token") # i removed the token for security
and this is the result of the program
Am I doing something wrong?
I am using this version of the discord botting API: https://github.com/Rapptz/discord.py
As #jwjhdev said, client.run is not supposed to be tabbed under on_ready. If you delete the tab, then everything should work perfectly.
I've been working on a discord bot using discord.py for the past few days. Now i'm trying to implement a command that displays the profile picture of anyone you mention on the server.
The problem is I can only get it to give the profile picture of the person who sends the command. I can't get it to take in a second parameter without encountering errors or the bot ignoring it.
#client.command(aliases=['getprofilepic','getprofilepicture','pfp','profilepic','profilepicture','avatar'])
async def getpfp(ctx):
await ctx.send(ctx.author.avatar_url)
The code above is a command to send the profile picture of the user who sends the command into the chat.
It ignores any extra parameters and thus can not display the profile picture of anyone else except the sender.
Changing getpfp(ctx): to getpfp(ctx, usernamepfp): and ctx.send(ctx.author.avatar_url) to ctx.send(ctx.usernamepfp.avatar_url) didn't work.
I also tried ctx.send(usernamepfp.avatar_url).
How would I implement the command such that when one types .getpfp #username in the chat it would send the profile picture of #username and not the sender?
from discord import Member
#bot.command()
async def getpfp(ctx, member: Member = None):
if not member:
member = ctx.author
await ctx.send(member.avatar_url)
You can use getpfp to display the avatar of user by passing 1) Mention/Tag 2) Display name/ Nick Name 3) Discord ID.
eg .getpfp #Someone
I am creating a Django website and trying to link a Discord account to the user, for listening to when the user is mentioned on the associated Discord server.
This is to be run with:
Python 3.7
Django 2.1.4
Discord.py 1.0.0(a)
Being still quite new to Django, I do not really know where to start (I have looked on Google but can't find anything helpful).
Here is the code for views.py:
import discord
token = open('token.txt', 'r').read()
client = discord.Client()
#client.event
async def on_ready():
print(f'We have logged in as {client.user}')
#client.event
async def on_message(message):
print(f'{message.channel}: {message.author.name} ({message.author}): \n
{message.content}')
client.run(token)
I have not been able to run it yet, not being able to figure out the Discord account linking.
I am in the process of making a discord bot using discord.py and asyncio. The bot has commands like kick and ban which obviously should not be available to normal users.
I want to make a simple system which will detect what permissions the user's role has using ctx.message.author to get the user who sent the command.
I do not want the bot to detect a specific role name as these vary across servers. I also prefer not to have multiple files for the bot to keep it simple.
I have seen the discord.py documentation and various other sources but none contain examples of how to implement the various methods they talk about.
As an example, here is a single command from my bot:
async def kick(ctx, userName: discord.User):
if True: #ctx.message.author.Permissions.administrator
await BSL.kick(userName)
else:
permission_error = str('Sorry ' + ctx.message.author + ' you do not have permissions to do that!')
await BSL.send_message(ctx.message.channel, permission_error)
Where the if else statement is my attempt of doing this on my own. The #ctx.message.author.Permissions.administrator is commented out as it does not work and replaced with True for testing purposes.
Thank you for any help and suggestions in advance.
Permissions is the name of the class. To get the message authors permissions, you should access the guild_permissions property of the author.
if ctx.message.author.guild_permissions.administrator:
# you could also use guild_permissions.kick_members
Update:
A better way to validate the permissions of the person invoking the commands is by using the check feature of the commands extension, specifically the has_permissions check. For example, if you wanted to open your command only to people who had either the manage_roles permission or the ban_members permission, you could write your command like this:
from discord import Member
from discord.ext.commands import has_permissions, MissingPermissions
#bot.command(name="kick", pass_context=True)
#has_permissions(manage_roles=True, ban_members=True)
async def _kick(ctx, member: Member):
await bot.kick(member)
#_kick.error
async def kick_error(ctx, error):
if isinstance(error, MissingPermissions):
text = "Sorry {}, you do not have permissions to do that!".format(ctx.message.author)
await bot.send_message(ctx.message.channel, text)
You could also use decorators.
#bot.command(name = "Kick")
#bot.has_permissions(kick_user = True)
#bot.bot_has_permissions(kick_user = True)
async def _kick(ctx, member: Member):
#Do stuff...
The advantage of checking user and bot permissions means it is easier to handle errors from either providing useful "Insufficient Permission" error messages.
The tips found the accepted answer may not work:
There may be compatibility issues with the rewrite version of the discord.py library and the pre-rewrite versions, which remain non-obsolete, non-deprecated, and still in use.
The bot should also check it's own permissions, to rule out one reason for the error.
If there is an error, or permissions for the bot itself are invalid, the bot should say something, correct?
Something needs to be implemented to prevent the bot from attempting to work this command in a DM or group context. It will almost always error.
I propose the following solution for pre-rewrite (assuming you use the command extension):
import discord
from discord.ext import commands
import time
#bot.command(pass_context=True,description="Kicks the given member. Please ensure both the bot and the command invoker have the permission 'Kick Members' before running this command.")
async def kick(ctx, target:discord.Member):
"""(GUILD ONLY) Boot someone outta the server. See 's!kick' for more."""
if not str(ctx.message.channel).startswith("Direct Message with "):
msg=await bot.say("Checking perms...")
time.sleep(0.5)
if ctx.message.server.me.server_permissions.kick_members:
if ctx.message.author.server_permissions.kick_members:
await bot.edit_message(msg,new_content="All permissions valid, checking issues with target...")
time.sleep(0.5)
if target==ctx.message.server.owner:
await bot.edit_message(msg, new_content="All permissions are correct, but you're attempting to kick the server owner, whom you can't kick no matter how hard you try. Whoops!")
else:
if target==ctx.message.server.me:
await bot.edit_message(msg, new_content="Whoops! All permissions are corrent, but you just tried to make me kick myself, which is not possible. Perhaps you meant someone else, not poor me?")
else:
await bot.edit_message(msg, new_content="All permissions correct, and no issues with target being self or server owner, attempting to kick.")
time.sleep(2)
try:
await bot.kick(target)
await bot.edit_message(msg, ":boom: BAM! ***kicc'd***")
except Exception:
await bot.edit_message(msg, new_content="I was unable to kick the passed member. The member may have a higher role than me, I may have crashed into a rate-limit, or an unknown error may have occured. In that case, try again.")
else:
await bot.edit_message(msg, new_content="I've the correct permissions, {}, but you do not. Perhaps ask for them?").format(ctx.message.author.mention)
else:
await bot.edit_message(msg, new_content="I'm just a poor bot with no permissions. Could you kindly grant me the permission `Kick Members`? Thanks! :slight_smile:")
else:
await bot.say("'Tis a DM! This command is for servers only... try this again in a server maybe? :slight_smile:")
Based on the current version of discord.py, using discord.ext.commands, you can do this:
Replace all <> with what it says.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix = "<this can be whatever you want>", intents = discord.intents.all())
#make sure to enable all intents in the discord dev portal. I use all intents to make things simple.
#bot.command()
#commands.has_permissions(<permission> = True)
async def ban():
#the ban commands go here
The permission has to be exactly like how it says in the api documentation here.
These permission ID's are for discord, but they are the same for discord.ext.
The ID for banning is ban_members. In the decorator #commands.has_permissions, remember to NOT put the ID in quotes. Also remember to add = True to the end of the id.