Discord bot create unique invite links guild error - python

I started coding a bot which when prompted would give me a certain number of unique 1 time use invite links to a server in a pre-designated channel inside a server. I am getting error IndexError: list index out of range
the code:
import discord
token = 'my discord bot token'
client = discord.Client()
number_of_links = input('How many links do you want to create? ')
#client.event
async def on_ready():
g = client.guilds[809743502665056306] # getting the error here
c = g.get_channel(809803698212700190)
invites = await discord.abc.GuildChannel.invites(c)
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 tried to also use g = client.guilds.get(809743502665056306) but then I get AttributeError: 'list' object has no attribute 'get'.
Am I trying to declare the guild incorrectly?

You can use client.get_guild(809743502665056306) instead of client.guilds[809743502665056306] to solve the error. More details about the get_guild function here.
The on_ready function gets called many times while the bot is running, so you may want to make a command instead of using an event.

Related

TypeError: 'async_generator' object is not subscriptable in discord.py

Im trying to make a command for my discord bot that gets the message content from a specific channel without using discord.ext, I have tried putting it inside my async def on_message(message) function and it doesnt want to work. I know discord.ext will be better for this but I still want to what I have been using.
Heres my code:
TOKEN = 'MY_BOTS_TOKEN'
import discord
intents = discord.Intents.all()
client = discord.Client(intents=intents)
#client.event
async def on_message(message):
mychannel = client.get_channel(MYCHANNELID)
content = (mychannel.history(limit=1))[0].content
if message.content.startswith('.content'):
await message.channel.send(content)
client.run(TOKEN)
I tried putting the mychannel = client.get_channel(MYCHANNELID) content = (m7channel.history(limit=1))[0].content into a different function and then calling onto it in message.channel.send(). I wasnt expecting it to work and I guessed correctly.
I also tried putting (await mychannel.history(limit=1))[0].content) inside of message.channel.send() but it returned the same error as the title.

Using discord.py to create a server and get an invite

Ive made a guild using discord.py, but I cant work out how to get an invite to that guild! How can I get an invite?
I believe you can get the invite of the new guild by running create_invite(). Documentation link
my_guild = await client.create_guild("My Server!") # replace "client" with the name of your discord client variable
my_guild_invite = await my_guild.text_channels[0].create_invite() # makes invite for the first text channel and returns an Invite class
print(my_guild_invite.code)

How to create a specific amount of invite links that can only be used once for a Discord server in Python

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)

How to add a reaction to a message using the message ID

I want to make a Bot command, that adds a reaction to a message when given emoji and ID of the message.
It seems like I need to turn the string I get from the original message into the discord.Message class for the code to work, but I don't find any way of doing that.
I've already read through the documentation and it just says
"There should be no need to create one of these manually."
import discord
token = "Token Here"
client = discord.Client()
#client.event
async def on_message(message):
if message.content.lower().startswith("e react"):
msg_id = message.content[8:26] #This filters out the ID
emoji = message.content[27:] #This filters out the Emoji
await msg_id.add_reaction(emoji)
client.run(token)
I get the following error:
AttributeError: 'str' object has no attribute 'add_reaction'
This is made much easier by using converters. Specifically, we can use MessageConverter to get the target Message object and PartialEmojiConverter to get the emoji we want to react with:
from discord.ext.commands import Bot
from discord import Message, PartialEmoji
bot = Bot("e ")
#bot.command()
async def react(ctx, message: Message, emoji: PartialEmoji):
await message.add_reaction(emoji)
bot.run("TOKEN")

Discord.py invites_from not returning list as expected

I am trying to create a command to list all of the invites created by a user (including how many uses each link has) and return a total in chat. But from my current code all I get back is:
<generator object Client.invites_from at 0x7f877ecc5780>
Here is my code to see where I am going wrong:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
bot = discord.Client()
client = commands.Bot(command_prefix = "!")
#client.event
async def on_ready():
print("Bot Connected!")
#client.event
async def on_message(message):
if message.content == "!inv":
server = message.channel.server.id
invites = bot.invites_from(server)
await client.send_message(message.channel, invites)
client.run("TOKEN")
If I try to loop through the returned generator I get the following error:
for i in invites:
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/discord/client.py", line 2693, in invites_from
data = yield from self.http.invites_from(server.id)
AttributeError: 'str' object has no attribute 'id'
Thanks a lot for any help in advance
Your problem appears to be with this line in on_message(), as pointed out in this comment:
server = message.channel.server.id
The traceback indicates that the discord.py library is trying to use the id attribute of the server you pass it; currently, you're passing it the ID, but you probably need to pass the whole server object.
IOW, do this instead:
server = message.channel.server
Since invites_from is a coroutine, you'll also need to await it:
invites = await bot.invites_from(server)
I have managed to fix it, it was that I had client and bot the wrong way around so the bot couldn't provide the token to the API, Thanks for all the help everyone

Categories

Resources