How to join a server?
Question:
I’m trying to setup a discord bot with python. I have a pre-existing discord server that I would like the bot to join, but I’m having a hard time doing so.
import discord
import asyncio
import logging
logging.basicConfig(level=logging.INFO)
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
print(client)
@client.event
async def on_message(message):
print(message)
if message.content.startswith('!test'):
counter = 0
tmp = await client.send_message(message.channel, 'Calculating messages...')
async for log in client.logs_from(message.channel, limit=100):
if log.author == message.author:
counter += 1
await client.edit_message(tmp, 'You have {} messages.'.format(counter))
elif message.content.startswith('!sleep'):
await asyncio.sleep(5)
await client.send_message(message.channel, 'Done sleeping')
client.run('token')
This is essentially the basic discord.py script as given on the GitHub page. However, I can’t seem to figure out how to have it actually join my server. When inserting this line into the on_ready
function:
server = await client.accept_invite('instant-invite-code')
with “instant-invite-code” replaced with my actual instant invite code (I tried both discord.gg/code and code), I get
discord.errors.Forbidden: FORBIDDEN (status code: 403): Bots cannot use this endpoint
Logging does actually work; I get output with my username and id. My bot is registered with the discord API and I already have a token.
Answers:
I had some trouble with this as well. What you need to do is:
- Go to the Discord developer pages (login if you haven’t).
- Go to the application with the bot you want to add to your channel.
- Copy the Client/Application ID.
- Go to https://discordapp.com/oauth2/authorize?client_id=CLIENT_ID_GOES_HERE&scope=bot&permissions=0 < You can set permissions for the bot here. Permissions can be calculated here.
- Select server and click authorize.
Your bot will now be a member of the server and will respond to commands you give it. Ex. !test in the code you have given.
EDIT: You can now use the permissions link (1) to generate the entire URL needed.
I suggest editing the code like this:
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('Invite: https://discordapp.com/oauth2/authorize?client_id={}&scope=bot'.format(client.user.id))
print('------')
I think this is the best and easiest solution. It works for me.
EDIT: Discord actually made their own OAuth2 url generator, so use that: https://discordapp.com/developers/tools/oauth2-url-generator
It’s been 4 years since I asked the question and here’s how I deal with this problem nowadays.
I use https://discordapi.com/permissions.html where you just paste your bot’s ID (which you can get here: https://discord.com/developers/applications ) and it also has the benefit of calculating whatever permissions configuration you want for you.
If you’re lazy, this could be a decent solution for you too.
I’m trying to setup a discord bot with python. I have a pre-existing discord server that I would like the bot to join, but I’m having a hard time doing so.
import discord
import asyncio
import logging
logging.basicConfig(level=logging.INFO)
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
print(client)
@client.event
async def on_message(message):
print(message)
if message.content.startswith('!test'):
counter = 0
tmp = await client.send_message(message.channel, 'Calculating messages...')
async for log in client.logs_from(message.channel, limit=100):
if log.author == message.author:
counter += 1
await client.edit_message(tmp, 'You have {} messages.'.format(counter))
elif message.content.startswith('!sleep'):
await asyncio.sleep(5)
await client.send_message(message.channel, 'Done sleeping')
client.run('token')
This is essentially the basic discord.py script as given on the GitHub page. However, I can’t seem to figure out how to have it actually join my server. When inserting this line into the on_ready
function:
server = await client.accept_invite('instant-invite-code')
with “instant-invite-code” replaced with my actual instant invite code (I tried both discord.gg/code and code), I get
discord.errors.Forbidden: FORBIDDEN (status code: 403): Bots cannot use this endpoint
Logging does actually work; I get output with my username and id. My bot is registered with the discord API and I already have a token.
I had some trouble with this as well. What you need to do is:
- Go to the Discord developer pages (login if you haven’t).
- Go to the application with the bot you want to add to your channel.
- Copy the Client/Application ID.
- Go to https://discordapp.com/oauth2/authorize?client_id=CLIENT_ID_GOES_HERE&scope=bot&permissions=0 < You can set permissions for the bot here. Permissions can be calculated here.
- Select server and click authorize.
Your bot will now be a member of the server and will respond to commands you give it. Ex. !test in the code you have given.
EDIT: You can now use the permissions link (1) to generate the entire URL needed.
I suggest editing the code like this:
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('Invite: https://discordapp.com/oauth2/authorize?client_id={}&scope=bot'.format(client.user.id))
print('------')
I think this is the best and easiest solution. It works for me.
EDIT: Discord actually made their own OAuth2 url generator, so use that: https://discordapp.com/developers/tools/oauth2-url-generator
It’s been 4 years since I asked the question and here’s how I deal with this problem nowadays.
I use https://discordapi.com/permissions.html where you just paste your bot’s ID (which you can get here: https://discord.com/developers/applications ) and it also has the benefit of calculating whatever permissions configuration you want for you.
If you’re lazy, this could be a decent solution for you too.