I wanted to make a bot in Python which can automatically set up Servers for you. I know how to do everything with the bot except the part, where the Bot automatically adds you.
How can I authorize the application to allow it to add me to servers and how do I make it add me to a server afterwards?
Edit: I tried https://github.com/discord/discord-oauth2-example but that didn't work because of an invalid redirect URI.
I don't know about that, but I have a simple script that creates a guild, then creates an invite link and invites you to the guild that's been created.
All you have to do afterwards is "!create Guild_Name"
Here's the code;
#bot.command()
async def create(ctx, *, name):
guild = await bot.create_guild(name=name)
channel = await guild.create_text_channel('welcome')
invite_link = await channel.create_invite()
await ctx.send(f"Succesfully created **{guild.name}**. (ID: {guild.id})\n"
f"{invite_link}")
Related
This is my code, but no matter what I try it's only tagging the bot? Do you know what I'm doing wrong? I've spent hours on this and I've not been able to figure it out.
#client.command(pass_context=True)
async def test(ctx):
user = random.choice(ctx.message.channel.guild.members)
await ctx.send(f'{user.mention} Youre the best')
I'm trying to get it to tag any random user.
Most likely, you did not give the bot permission to receive users information (SERVER MEMBERS INTENT) on the site Discord Developers in the Privileged Gateway Intents section.
code is only mentioning the bot, which is executing the command!
Try this:
#client.command(pass_context=True)
async def test(ctx):
members = [member for member in ctx.message.channel.guild.members if not member.bot]
if not members:
await ctx.send("There are no non-bot members in this guild.")
return
user = random.choice(members)
await ctx.send(f'{user.mention} You're the best')
As #DK404 has already mention, you probably didn't enable the Server Members Intent on Discord Developers.
Also check if you have enabled the intent for your bot too. You can do that by adding the intents parameter to your bot instance:
client = discord.Client(..., intents = discord.Intents.all())
After you do that try running the bot again.
By the way ctx.message.channel.guild.members is the same as ctx.guild.members
I would like to create a bot which can delete number of recently chat and history chat
import discord
import random
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as {0}!'.format(self.user))
async def on_message(self, message):
channel = message.channel.name
restricted_channels = ["command-bot"]
prefix = "-" # Replace with your prefix
# If the message starts with the prefix
if message.content.startswith(prefix):
if channel in restricted_channels:
command = message.content[len(prefix):]
if command.startswith("clear"):
await message.delete()
I have try this
if command.startswith("clear"):
await message.delete()
But it only delete the chat which have command "clear"
First off, personally, I would change the structure/ layout of your code. This is so that it is easier to read, and easier to change and add commands/ different functions to. This is the way I have my bot setup and how I have seen many other bots setup as well:
import discord
client = commands.Bot(command_prefix='your prefix', intents=discord.Intents.all()) # creates client
#client.event # used commonly for different events such as on_ready, on_command_error, etc...
async def on_ready():
print('your bot is online') # basic on_ready event and print statement so you can see when your bot has gone online
Now that we've gone through that part, let's get onto the purge/ clear command you're trying to make. I have one in my bots which generally looks something like this:
#client.command() # from our client declaration earlier on at the start
#commands.has_permissions(moderate_members=True) # for if you want only certain server members to be able to clear messages. You can delete this if you'd like
async def purge(ctx, amount=2): # ctx is context. This is declared so that it will delete messages in the channel which it is used in. The default amount if none is specified in the command is set to 2 so that it will delete the command call message and the message before it
await ctx.channel.purge(limit=amount) # ctx.channel.purge is used to clear/ delete the amount of messages the user requests to be cleared/ deleted within a specific channel
Hope this helps! If you have any questions or issues just let me know and I'll help the best I can
I'm making a discord bot with discord.py and I want to a specific user when a user uses a specific command.
from discord import DMChannel
client = discord.Client()
client = commands.Bot(command_prefix=']')
#client.command(name='dmsend', pass_context=True)
async def dmsend(ctx, user_id):
user = await client.fetch_user("71123221123")
await DMChannel.send(user, "Put the message here")
When I give the command ]dmsend nothing happens. And I've tried dmsend also. But nothing happened.
A few things I noticed:
You defined client twice, that can only go wrong.
First) Remove client = discord.Client(), you don't need that anymore.
If you want to send a message to a specific user ID, you can't enclose it in quotes. Also, you should be careful with fetch, because then a request is sent to the API, and they are limited.
Second) Change await client.fetch_user("71123221123") to the following:
await client.get_user(71123221123) # No fetch
If you have the user you want the message to go to, you don't need to create another DMChannel.
Third) Make await DMChannel.send() into the following:
await user.send("YourMessageHere")
You may also need to enable the members Intent, here are some good posts on that:
https://discordpy.readthedocs.io/en/stable/intents.html
How do I get the discord.py intents to work?
A full code, after turning on Intents, could be:
intents = discord.Intents.all()
client = commands.Bot(command_prefix=']', intents=intents)
#client.command(name='dmsend', pass_context=True)
async def dmsend(ctx):
user = await client.get_user(71123221123)
await user.send("This is a test")
client.run("YourTokenHere")
Use await user.send("message")
I am new to working with Discord servers and I would like to make a private Discord server where only users that I invite can join. I read about a few ways that this can be achieved, but none of them are really what I have in mind. I was thinking about creating a Discord application that generated a specific amount of invite links to my server which can only be used once.
This means that if I want to invite 50 people to my Discord server, I would create 50 invite links that can only be used once so that I make sure that only the people I invite will join. I would like to put all of these links in an external text file so that I will later be able to work with them and eventually send them to people by email. In other words, I don't need to create a bot, but rather just use Python and the discord.py module to achieve all this outside of Discord.
I saw this on the discord.py documentation which looks something like what I need, but I don't really understand how that would work.
I can almost only find tutorials on how to create bots, on the Discord server itself, but that is not what I need. Would anyone be able to help me out?
Thank you very much in advance!
import discord
token = 'bot_token_goes_here'
client = discord.Client()
number_of_links = input('How many links do you want to create? ')
#client.event
async def on_ready():
g = client.guilds[guild_number goes here] # Choose the guild/server you want to use
c = g.get_channel(channel_id_goes_here) # Get channel ID
invites = await discord.abc.GuildChannel.invites(c) # list of all the invites in the server
while len(invites) < int(number_of_links):
print('CREATING INVITES')
for i in range(int(number_of_links)): # Create as many links as needed
i = await discord.abc.GuildChannel.create_invite(c, max_uses=1, max_age=0, unique=True) # Create the invite link
break
print('Finished. Exiting soon...')
exit()
client.run(token)
I made some quick modifications to FedeCuci's script, with the following differences:
Script asks for the total number of new invites you want, rather than depending on previously created invites
Script outputs the new invites to console
Does not throw an exception
Compatible with new intents API
Some additional debugging output
For either script, you will need to install discord.py, then run the script with python3. Don't forget to update the token & ID's as needed, and setup/join the bot to your target server via Discord's developer portal. It will need permissions for Create Instant Invite and Read Messages/View Channels
from discord.utils import get
import discord
token = 'bot-token-goes-here' # <-- fill this in
intents = discord.Intents.default()
intents.invites = True
client = discord.Client(intents=intents)
guild_id = guild-id-goes-here # <-- fill this in
number_of_links = input('How many links do you want to create? ')
#client.event
async def on_ready():
print('Bot is up and running.')
print(f'Logged in as {client.user}')
g = client.get_guild(int(guild_id))
print(f'Guild: {g.name}')
c = g.get_channel(channel-id-goes-here) # <-- fill this in
print(f'Channel: #{c}')
invites = []
print('CREATING INVITES')
for i in range(int(number_of_links)):
invite = await g.text_channels[0].create_invite(max_uses=1, max_age=0, unique=True)
print(f'{invite}')
print('Finished. Exiting soon...')
await client.close()
client.run(token)
I want to send a private message to a specific user with my discord bot. I am using discord==1.0.1
discord.py==1.3.1. I already tried using the docs (https://discordpy.readthedocs.io/en/latest/api.html?highlight=private%20message#discord.DMChannel) but i dont understand that. I tried following code, which didnt worked:
#client.command()
async def cmds(ctx):
embed = discord.Embed(title='Discord Server Befehle',
description='Description',
color=0x374883)
[...]
msg = await ctx.send(embed=embed)
await ctx.author.create_dm('Hi')
print(f'{command_prefix}help ausgeführt von {ctx.author}')
Try using the send function instead of the create_dm. create_dm only creates the channel between two users which discord does automatically as far as i know.
according to documentation
This should be rarely called, as this is done transparently for most people.
so it should be
ctx.author.send('Hi')