How to link a discord channel with python - python

So I am working on making a discord bot for a support server of my friends the one thing i am stuck up on is I want the bot to tell a user to go to a specific channel with the #channel that way they can click on it for convince I have tried some things but I am relatively new to coding any ideas. this is all that i have so far.
import discord
import os
import keep_alive
if message.content.startswith("!help"):
await message.channel.send("To contact a support person go to")

First off you should use a bot command decorator rather than looking for the command statement in the messages content.
In this command you should extract the target channel object through its ID, and mention that channel in your message. In this way users will be able to click on the mention and be redirected to that channel.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def help(ctx):
guild = ctx.guild # The guild to which the channel belongs to
target_channel = guild.get_channel(channel_ID_goes_here)
await ctx.send(f"To contact a support person go to {target_channel.mention}")

Related

Discord .py bot to assign role to any user who types the command

there. I'm brand new to coding in Python and I'm just not able to figure out what I'm doing wrong.
I want any user in a Discord server to be able to type my specified prefix and a command and have the bot assign the desired role.
For example, if a user want to add a role to themselves called Rook, I want them to be able to type:
%Rook
And have the role added to them by the bot.
I've tried piecing together so many different examples I've found on the internet and nothing works.
Is anyone able to help?
Here's my code:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = "%",intents=intents)
bot = commands.Bot(command_prefix="%", intents=discord.Intents.all())
#client.command()
async def Rook(ctx):
# if prefix is '-' you'd type: -rook
await user.remove_roles("1073162715499089981")
await user.add_roles("1073162715499089981")
await ctx.send("Rook role added!")
I tried piecing together various bits I could find from similar questions, but nothing is working. I type in the command %Rook and no role is assigned. I'd ideally NOT like the user to have to type the command %Rook #TheirName, but just %Rook.

Discord bot, The bot does not send messages about the arrival of a new member Python

I am new to creating bots for Discord and I take codes from different examples and I write on replit.
I want the bot to send a welcome message to a new member of the server, but when I invite my 2nd account, it doesn't send anything. I have included Intentions
Full code
import discord
from discord import Game
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
from webserver import keep_alive
import os
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
client = commands.Bot(command_prefix='!')
#client.event
async def on_member_join(member):
channel = client.get_channel(channel_id=870695778593701939)
embed = discord.Embed(
title=f"Welcome {member.name}",
description=f"Thanks for joining {member.guild.name}!") # F-Strings!
embed.set_thumbnail(
url=member.avatar_url
) # Set the embed's thumbnail to the member's avatar image!
await channel.send(embed=embed)
And that's all I'm writing on the phone and I don't understand how I can return this whole code and where, so sorry
I'm guessing I have included Intentions means that you have added the intents in the bot constructor and the developers portal
so first
channel = client.get_channel(channel_id=870695778593701939)
channel_id is not the argument name its id and its a positional argument so you can just do client.get_channel(channel_id_here) would work, just replace channel_id_here with the channel id
second when your setting url=member.avatar_url in the set_thumbnail function it would raise an error if the member has no avatar so i would suggest you check if the user has a avatar, if they don't just give it member.default_avatar_url, but if your using discord.py 2.0 (master branch) then its ALOT easier with member.display_avatar.url
this is just a suggestion because display_avatar returns the users/members avatar if they have one or the default discord gives them so no errors are raised

What is wrong with my code that sends a message when a discord bot joins a server?

I am trying to make a discord bot send the message like "hi guys" with discord.py whenever it joins a server. but the code that I am trying to use to send the message does not work. It doesn't show any errors however.
code: (some of the imports are for other stuff in the code)
import discord
import os
import random
import time
from discord import channel
from discord.message import DeletedReferencedMessage, Message
from discord.utils import find
client = discord.Client()
#client.event
async def on_member_join(member):
if member == client.user:
await channel.send('hi guys')
Please tell me what is wrong so that I can get it working. (I am fairly new to coding in python btw)
Change on_member_join to on_guild_join.
on_member_join: called when a member joins the guild
on_guild_join: called when the bot joins a guild

How do I send a message to a specific discord channel while in a task loop?

import discord
from discord.ext import commands, tasks
from discord_webhook import DiscordWebhook
client = discord.Client()
bot = commands.Bot(command_prefix="$")
#tasks.loop(seconds=15.0)
async def getAlert():
#do work here
channel = bot.get_channel(channel_id_as_int)
await channel.send("TESTING")
getAlert.start()
bot.run(token)
When I print "channel", I am getting "None" and the program crashes saying "AttributeError: 'NoneType' object has no attribute 'send' ".
My guess is that I am getting the channel before it is available, but I am unsure. Anybody know how I can get this to send a message to a specific channel?
Your bot cannot get the channel straight away, especially if it's not in the bot's cache. Instead, I would recommend to get the server id, make the bot get the server from the id, then get the channel from that server. Do view the revised code below.
#tasks.loop(seconds=15.0)
async def getAlert():
#do work here
guild = bot.get_guild(server_id_as_int)
channel = guild.get_channel(channel_id_as_int)
await channel.send("TESTING")
(Edit: Including answer from comments so others may refer to this)
You should also ensure that your getAlert.start() is in an on_ready() event, as the bot needs to start and enter discord before it would be able to access any guilds or channels.
#bot.event
async def on_ready():
getAlert.start()
print("Ready!")
Helpful links:
discord.ext.tasks.loop - discord.py docs
on_ready event - discord.py docs
bot.get_guild(id) - discord.py docs
guild.get_channel(id) - discord.py docs
"I can't get certain guild with discord.py" - Stackoverflow

Discord bot - old posts with old nicknames

I've prepared a discord bot that needs to post in name of other users, so I want it to change its nickname. I thought it went ok, but after I restart a client all posts changed to last nickname and no longer be recognizable by author.
Any ideas how to solve it?
Once I saw a discord bot (https://imgur.com/a/eMsmIB3) that posts tweets and changes the name depending on whom tweets it. So I guess it is possible.
So you could do something like this
import discord
import random
import asyncio
from discord.ext.commands import Bot
client = Bot(command_prefix='!', case_insensitive=True)
#client.command(pass_context=True)
async def nick(ctx):
await client.change_nickname(ctx.message.server.get_member("bot_id"),f"Name #{random.randrange(30)}")
embed = discord.Embed()
await asyncio.sleep(.01)
embed.add_field(name = "Poster: ",value = ctx.message.server.get_member("bot_id").nick)
await client.send_message(ctx.message.channel,embed = embed)
I'm not sure if you've tried this but the way that Twitter bot does it also resets its nickname to your client whenever you restart Discord
He also inserts the name of whoever is tweeting into the embed which lasts even after you restart the client.

Categories