Discord.py Welcome/Removed messages in chat - python

So I am trying to make my bot leave welcome and removed messages using discord.py.
import discord
from discord.ext import commands
import random
client = commands.Bot(command_prefix = '.')
#client.event
async def on_member_join(ctx, *, member):
channel = member.server.get_channel("channel id")
fmt = 'Welcome to the {1.name} Discord server, {0.mention}'
await ctx.send_message(channel, fmt.format(member, member.server))
#client.event
async def on_member_remove(ctx, *, member):
channel = member.server.get_channel("channel id")
fmt = '{0.mention} has left/been kicked from the server.'
await ctx.send_message(channel, fmt.format(member, member.server))
client.run('client id')
The error
File "C:\python\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
TypeError: on_member_join() missing 1 required keyword-only argument: 'member'
comes up.
I am not sure why it isn't working. I have very little experience with it, so I am not sure what I am doing wrong. Could anyone tell me what I am missing?

#client.event
async def on_member_join(member):
await client.get_channel(idchannel).send(f"{member.name} has joined")
#client.event
async def on_member_remove(member):
await client.get_channel(idchannel).send(f"{member.name} has left")
On event you can't get ctx, because it isn't a command, who must write. It is event

#client.event
async def on_member_join(member):
await client.get_channel(idchannel).send(f"{member.name} has joined")
#client.event
async def on_member_remove(member):
await client.get_channel(idchannel).send(f"{member.name} has left")
also keep in mind that idchannel is int, not string

Related

Bot not saying welcome when someone joins and bot not sending dm's when I want it to do

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}')

command cant working on discord bot at new version

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'm very new to making discord bots with python and for some reason the bot doesn't respond to commands

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

#bot.command() AttributeError: 'Client' object has no attribute 'command'

from discord.ext import commands
client = discord.Client(command_prefix='x!')
#client.event
async def on_ready():
print("Successfully booted the bot up!")
#client.event
async def on_message(message):
if message.content.find("minecraft") != -1:
await message.channel.send('#Kerina#4436 ajde majnkraft jebemlite')
#client.command()
async def nwordpass(ctx):
await ctx.send('Proof of you having the nword pass: https://lh3.googleusercontent.com/1HBSAiHdRdM1UVJJZlOUnMkihkiMOPPYSMTjI5WzHuvDVIBztueZR83rkUiHwIJvrfU')
client.run("NzA2MzE0MTc3NjQzNDEzNTY1.Xq4cYw.A-6MruzAgtLC1maW4VVIB2HlFM4")
Why doesn't it work?
I tried almost every common fix but didn't get any positive results
You need to use the Bot class from discord.ext.commands
from discord.ext import commands
client = commands.Bot(command_prefix='x!')
#client.event
async def on_ready():
print("Successfully booted the bot up!")
#client.event
async def on_message(message):
if message.content.find("minecraft") != -1:
await message.channel.send('#Kerina#4436 ajde majnkraft jebemlite')
await client.process_commands(message)
#client.command()
async def nwordpass(ctx):
await ctx.send('Proof of you having the nword pass: https://lh3.googleusercontent.com/1HBSAiHdRdM1UVJJZlOUnMkihkiMOPPYSMTjI5WzHuvDVIBztueZR83rkUiHwIJvrfU')
I also added the await client.process_commands(message) line so that your commands are processed.
The sample code at https://discordpy.readthedocs.io/en/latest/ext/commands/extensions.html#id1 indicates that the correct code would look like this:
#commands.command()
async def nwordpass(ctx):
await ctx.send('Proof of you having the nword pass: https://lh3.googleusercontent.com/1HBSAiHdRdM1UVJJZlOUnMkihkiMOPPYSMTjI5WzHuvDVIBztueZR83rkUiHwIJvrfU')
That is, the command() decorator is defined on the commands module, not on the Client instance objects.

discord.py bot not joining channel

ok so here is my code
import discord
from discord.ext import commands
TOKEN = 'THIS_IS_MY_BOT_TOKEN'
client = commands.Bot(command_prefix = '.')
#client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
#typing cat
if message.content.startswith('!cat'):
msg = 'https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif'.format(message)
await client.send_message(message.channel, msg)
#I dont need sleep i need awnsers
if message.content.startswith('!sleep'):
msg = 'https://i.kym-cdn.com/entries/icons/original/000/030/338/New.jpg'.format(message)
await client.send_message(message.channel, msg)
#murica
if message.content.startswith('!murica'):
msg = 'https://www.dictionary.com/e/wp-content/uploads/2018/08/Murica_1000x700.jpg'.format(message)
await client.send_message(message.channel, msg)
if message.content.startswith('!why'):
msg = 'https://drive.google.com/file/d/1rb132Y785zUjj2RP2G-a_yXBcNK5Ut9z/view?usp=sharing'.format(message)
await client.send_message(message.channel, msg)
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
#client.command(pass_context=True)
async def join(ctx):
if ctx.message.author.voice:
channel = ctx.message.author.voice.channel
await channel.connect()
client.run(TOKEN)
the bot joins the sever, but when I say .join, nothing happens
the voice channel I want to join is called Club Meeting if that helps
Not entirely sure why, I have no errors when I run it. Anyone have any idea whats going on?
I think the problem is your lack of Bot.process_commands You need to put that at the end of your on_message function. That seems to be the reason your command isn't working.
From The Docs:
Why does on_message make my commands stop working?
https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working

Categories

Resources