I'm wanting to have my bot send it's "I connected to discord" message to a specific discord channel. Here is my code currently
import random
import discord
import time
from discord.ext import commands, tasks
bot = commands.Bot(command_prefix='!')
TOKEN = ''
#bot.event
async def on_ready(ctx):
channel = bot.get_channel(774970601013379092)
await channel.send(f'{bot.user.name} has connected to Discord!')
#bot.command()
async def hello(ctx):
await ctx.send(f"Hello World")
#bot.command()
#commands.is_owner()
async def shutdown(ctx):
await ctx.send(f"{bot.user.name} is shutting down. . .")
await ctx.bot.logout()
print(f'{bot.user.name} has disconnected from Discord!')
# c_sharp_challenges channel
#bot.command()
async def csharp(ctx):
channel = bot.get_channel(774683687018037249)
await channel.send('Welcome To C#!')
# html_challenges channel
#bot.command()
async def html(ctx):
channel = bot.get_channel(774683714399371316)
await channel.send('Welcome To HTML!')
# java_challenges channel
#bot.command()
async def java(ctx):
channel = bot.get_channel(774683618307342417)
await channel.send('Welcome To Java!')
# java_script_challenges channel
#bot.command()
async def javascript(ctx):
channel = bot.get_channel(774683653728501761)
await channel.send('Welcome to Java Script!')
# python_challenges channel
#bot.command()
async def python(ctx):
channel = bot.get_channel(774683556077109258)
await channel.send('Welcome To Python!')
bot.run(TOKEN)
I can't seem to find anything on stackoverflow, or google in general, that can help me figure this out. it's in python, and I'm on ubuntu 20.04 if that matters
That the bot sends an message to some channel that it is online. If you want to change the channel you just have to change the channel_id given in the on_ready function.
I happened to notice that I had 00000 in for the channel ID
#bot.event
async def on_ready():
print("bot is ready")
channel = bot.get_channel(774970601013379092)
await channel.send("bot is online")
Related
Soo i want my bot to send a message when someone join but it is not workin
#client.event
async def on_member_join(member):
channel = discord.util.get(member.Guild, name='general')
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
and
i want my bot to take a massage from me and send it to the guy i say but this is not workin too
massage = await client.wait_for('massage', check=check)
await ctx.send(f'send massage to {member} ')
await member.send(f'{ctx.member.mention} has a massage for you: \n {massage}')
and
here is my WHOLE code:
import os, discord,asyncio
import keep_alive
from discord.ext import commands
client = commands.Bot(command_prefix="!")
token = os.environ.get('Token')
GUILD = os.environ.get('Guild')
#client.event
async def on_ready():
print(f'{client.user} is connected')
#client.command()
async def dm(ctx, member: discord.Member):
await ctx.send('what do u want to say bitch!')
def check(m):
return m.author.id == ctx.author.id
massage = await client.wait_for('massage', check=check)
await ctx.send(f'send massage to {member} ')
await member.send(f'{ctx.member.mention} has a massage for you: \n {massage}')
#client.event
async def on_member_join(member):
channel = discord.util.get(member.Guild, name='general')
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
keep_alive.keep_alive()
client.run(token)
For making a welcome channel, it is safer to use get_channel instead of get. Because in your code, every time you rename your channel, you need to change your code too, but channel ids cannot be changed until if you delete and create another one with the same name.
Code:
#client.event
async def on_member_join(member):
channel = client.get_channel(YOUR_CHANNEL_ID_GOES_HERE)
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
As for the dm command, I recommend you to get your message as a function parameter. Also you can check when you're DM'ing your bot with the isinstance() function. There is a * before the message parameter though. Its purpose is collecting all of your messages with or without spacing.
Code:
#client.command()
async def dm(ctx, member:discord.Member,*, message):
if isinstance(ctx.channel,discord.DMChannel):
await member.send(f'{ctx.member.mention} has a message for you: \n {message}')
#discordbot.py
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print('Bot is ready.')
#client.event
async def on_member_join(member):
print(f'{member} has joined a server.')
#client.event
async def on_member_remove(member):
print(f'{member} has left a server.')
I ran python with this script. But there aren't some changes on on_member_join(member): ~ and on_member_remove(member): ~. Why don't know why and I'm new with python. I hope you can help me.
Print will only print out the string in the console. I assume you want the message to appear in a designated channel so you can do something like this:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print('Bot is ready.')
#client.event
async def on_member_join(member):
channel = client.get_channel('insert the channel id of your choice')
await channel.send(f'{member} has joined a server.')
#client.event
async def on_member_remove(member):
channel = client.get_channel('insert the channel id of your choice')
await channel.send(f'{member} has left a server.')
I see your code and I can tell you that I had the same problem.
You just need to enable "Server Members Intent" into your developer page (https://discord.com/developers/applications/YOUR_BOT_ID/bot).
Then go into you bot and add some new lines:
intents = discord.Intents.default()
intents.members = True
and then, into the client var definition:
client = commands.Bot(command_prefix = '.', intents=intents)
and you're ready to go! I hope I helped you!
import discord
import random
from discord.ext import commands
client = commands.Bot(command_prefix = '!')
#client.event
async def on_message(message):
print('Message from {0.author}: {0.content}'.format(message))
#client.event
async def on_ready():
deneme=["Besim Tibuk Online!","Bu Kitap benim","Satacağız!","Özelleştireceğiz!"]
await client.get_channel(#########).send(random.choice(deneme))
#client.command()
async def ping(ctx):
await ctx.send("Pong")
"client.command()" side cant working.When ı write "ping" I cant get response
Its because of your on_message, you arent processing commands. To process commands, do await client.process_commands(message). Here is the reworked code.
#client.event
async def on_message(message):
print('Message from {0.author}: {0.content}'.format(message))
await client.process_commands(message)
i need to use my discord command in specific channel , I'm using #client.command()
I mean if i used my command in another channel will be not working
#client.command()
async def w(ctx):
channel = client.get_channel(775491463940669480)
await ctx.send("hello) ```
You can check if ctx.channel is equal to your specific channel.
#client.command()
async def w(ctx):
if ctx.channel.id == 775491463940669480:
await ctx.send("hello)
There is even an easier way to do it without getting the channel. And without having to indent the whole code.
#client.command()
async def w(ctx):
# exit command if not the desired channel.
if ctx.channel.id is not 775491463940669480:
return
#code here
await ctx.send("hello")
client = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print("bot is Online")
-
#client.event
async def on_message(message):
cmdChannel = client.get_channel(776179059355942943)
if message.content.lower().startswith('!w'):
if message.channel.id == cmdChannel.id:
#command invoked in command channel - execute it
await client.process_commands(message)
else:
#command attempted in non command channel - redirect user
await message.channel.send('Write this command in {}'.format(cmdChannel.mention))
#client.command()
async def w(ctx, amount):
await ctx.send(f"done {amount}")
async def is_channel(ctx):
return ctx.channel.id = 775491463940669480
#client.command()
#client.check(is_channel)
async def w(ctx):
await ctx.send("hello)
Making the check call the function is_channel makes the code simpler and you can resuse it multiple times.
As described in the title, I am new to Python(programming in general) and I tried making a bot, however the bot does not respond to commands. I followed/looked through multiple youtube tutorials & articles, but I cannot find a way to fix my problem.
import discord
from discord.ext.commands import Bot
bot = Bot(".")
#bot.event
async def on_ready():
print("kram is now online")
await bot.change_presence(activity=discord.Game(name="This bot is a WIP"))
#bot.event
async def on_message(message):
if message.author == bot.user:
#bot.command(aliases=["gp"])
async def ghostping(ctx, amount=2):
await ctx.send("#everyone")
await ctx.channel.purge(limit = amount)
#bot.command()
async def help(ctx):
await ctx.send("As of right now, .gp is the only working command.")
bot.run("I'm hiding my token")
Hey why don't you try this instead the same thing but i removed the on message
import discord
from discord.ext.commands import Bot
bot = Bot(".")
#bot.event
async def on_ready():
print("kram is now online")
await bot.change_presence(activity=discord.Game(name="This bot is a WIP"))
#bot.command(aliases=["gp"])
async def ghostping(ctx, amount=2):
await ctx.send("#everyone")
await ctx.channel.purge(limit = amount)
#bot.command()
async def help(ctx):
await ctx.send("As of right now, .gp is the only working command.")
bot.run("I'm hiding my token")
This should work as when using cogs and when you have a command there is no need to put it in the on_message event. i suggest you watch this series(Really helpful while starting out):
https://www.youtube.com/watch?v=yrHbGhem6I4&list=UUR-zOCvDCayyYy1flR5qaAg