Turning invite code to url discord rewrite - url-rewriting

I set up a command on my bot to pull active invites from servers but cant seem to turn the results into a url.
Ive tried to turn {invites} to {invites.url} and it did'nt seem to work
#commands.is_owner()
#commands.command()
async def fi(self, ctx, id: int):
server = self.bot.get_guild(id)
invites = await server.invites()
fetching = await ctx.send("Fetching Invites...")
await fetching.edit(content=f"All Active Invites Codes: \n {invites}")
await asyncio.sleep(8)
await fetching.delete()
# Fetches invite codes

Guild.invites returns a list of Invites, so you need to get the url attribute of each Invite individually.
invites = await server.invites()
invites = [invite.url for invite in invites]
invites_message = "\n".join(invites)
await fetching.edit(content=f"All Active Invites Codes: \n{invites_message}")

Related

How do I make it so this message gets sent a specific channel? (Discord.py)

I want the message to get sent to a specific channel
async def on_message_edit(before, after):
await before.channel.send(
f'{before.author} edited a message.\n'
f'Before: {before.content}\n'
f'After: {after.content}'
)
To send a message in a specific channel you need first to fetch the choosed channel by using his id by using get_channel() like this:
specificChannel = bot.get_channel(int(YOUR_CHANNEL_ID_HERE))
Then your event block should look like this:
#client.event
async def on_message_edit(before, after):
specificChannel = bot.get_channel(int(YOUR_CHANNEL_ID_HERE))
await specificChannel.send(
f'{before.author} edited a message.\n'
f'Before: {before.content}\n'
f'After: {after.content}'
)
Useful link:
get_channel()
Discord.py documentation
maybe this can help u :)
#client.event
async def on_member_join(member:Member):
if member.bot:
guild:discord.Guild = member.guild
role = guild.get_role(config.botRoleID)
channel:discord.TextChannel = guild.get_channel(YOUR_CHANNEL_ID)
await member.add_roles(role)
await channel.send(f'Set Bot role to "{member.display_name}" 😌')

Using CTX in on_reaction_add event -- Discord.py

async def on_reaction_add(reaction, user):
emoji = reaction.emoji
if emoji == "💌":
await user.channel.send("HI")
I got problem here with user.
I want to use here with ctx like ctx.channel.send.
but also it occured error how to use ctx in here?
Instead of using the on_reaction_add event, it's better in this case to use a wait_for command event. This would mean the event can only be triggered once and only when the command was invoked. However with your current event, this allows anyone to react to a message with that emoji and the bot would respond.
By using client.wait_for("reaction_add"), this would allow you to control when a user can react to the emoji. You can also add checks, this means only the user would be able to use the reactions on the message the bot sends. Other parameters can be passed, but it's up to you how you want to style it.
In the example below shows, the user can invoke the command, then is asked to react. The bot already adds these reactions, so the user would only need to react. The wait_for attribute would wait for the user to either react with the specified emojis and your command would send a message.
#client.command()
async def react(ctx):
message = await ctx.send('React to this message!')
mail = '💌'
post = '📮'
await message.add_reaction(mail)
await message.add_reaction(post)
def check(reaction, user):
return user == ctx.author and str(
reaction.emoji) in [mail, post]
member = ctx.author
while True:
try:
reaction, user = await client.wait_for("reaction_add", timeout=10.0, check=check)
if str(reaction.emoji) == mail:
await ctx.send('Hi you just received')
if str(reaction.emoji) == post:
await ctx.send('Hi you just posted...')
You need to use reaction.message.channel.send
async def on_reaction_add(reaction, user):
emoji = reaction.emoji
if str(emoji) == "💌": await reaction.message.channel.send("HI")

Discord.py Reaction role command

I want my bot to make a reaction role when I say p!reactrole [emoji] [role] [message], but it isn't working. No errors or anything.
#client.command()
async def reactrole(ctx, emoji, role: discord.Role, *, message):
embedVar = discord.Embed(description=message)
msg = await ctx.channel.send(embed=embedVar)
await msg.add_reaction(emoji)
with open('reactrole.json') as json_file:
data = json.load(json_file)
new_react_role = {
'role_name':role.name,
'role_id':role.id,
'emoji':emoji,
'message_id':msg.id
}
data.append(new_react_role)
with open('reactrole.json', 'w') as j:
json.dump(data,j,indent=4)
The code looks good but you don't have gateway intents enabled. If your bot is verified, you'll have to email discord at dis.gd/contact. If your bot is not verified, you can enable them in the developer portal under the bot tab. Once you enable the intent, you'll have to also enable them in your code.
intents = discord.Intents.<name of intent or 'all' if you want all intents>()
client = commands.Bot(..., intents=intents)

How to make a discord.py reaction role code

I'm new to discord bot making, and I'd like to have the bot add a reaction to my message that, once pressed, allows the user reacting to pick up a role. What would be the code to allow me to do this? Thank you.
This is a very well written and formatted question! In order for the bot to detect that the reaction is on a specific message, you can do many ways. The easiest way would be to be by ID, so I'm just going to do this with a simple command.
messageIDs = []
#client.event
async def on_raw_reaction_add(payload):
global messageIDs
for messageID in messageIDs:
if messageID == payload.message_id:
user = payload.member
role = "roleName"
await user.add_roles(discord.utils.get(user.guild.roles, name = role))
#client.command()
async def addMessage(ctx, messageID):
global messageIDs
emoji = "👍"
channel = ctx.message.channel
try:
msg = await channel.fetch_message(messageID)
except:
await ctx.send("Invalid Message ID!")
return
await msg.add_reaction(emoji)
messageIDs.append(messageID)

How do I make my logs server specific | discord.py

Currently, I have a logs on my bot, It works fine but the problem is that it takes logs from all servers that the bot it in. So, how do I make it server specific?
#commands.Cog.listener()
async def on_message_delete(self, message):
embed = Embed(
description=f"Message deleted in {message.channel.mention}", color=0x4040EC
).set_author(name=message.author, url=Embed.Empty, icon_url=message.author.avatar_url)
embed.add_field(name="Message", value=message.content)
embed.timestamp = message.created_at
channel=self.bot.get_channel(channel_id)
await channel.send(embed=embed)
This is my code for message deleting
Compare message's guild id with yours
Below is the revised code:
#commands.Cog.listener()
async def on_message_delete(self, message):
if message.guild.id == YOUR_GUILD_ID:
embed = Embed(
description=f"Message deleted in {message.channel.mention}", color=0x4040EC
).set_author(name=message.author, url=Embed.Empty, icon_url=message.author.avatar_url)
embed.add_field(name="Message", value=message.content)
embed.timestamp = message.created_at
channel = self.bot.get_channel(channel_id)
await channel.send(embed=embed)

Resources