Ban command on message discord.py - python

import discord
from discord.ext import commands
client = commands.Bot(command_prefix='sk?')
token = ""
#client.event
async def on_message():
if 'WORD' in message.content:
await member.ban(reason = WORD)
client.run(token)
I get the error "takes 0 positional arguments but 1 was given" and I don't know what is wrong with my code.
I tried adding on_message(member): and member = message.author() above if 'WORD' in message.content:
I have no idea what to do or what I did wrong.
It also says that for all messages which is really annoying.

on_message takes the message argument, also member should be message.author
#client.event
async def on_message(message):
if "WORD" in message.content:
await message.author.ban(reason="WORD")
# Remember to add `process_commands`
await client.process_commands(message)
You should also add process_commands at the end of the event so commands work

Related

Python search for string from list in string

This is what i have so far:
from discord import Embed
import os
import discord
bot=discord.Client()
nichDat=["|","~",".",",","!","pls"]
#bot.event
async def on_message(message):
if message.content != "NothingButABot":
return
for guild in bot.guilds:
print(guild.name)
for channel in guild.text_channels:
if "bot" not in channel.name:
async for message in channel.history(limit=200):
if not message.author.bot:
for dings in nichDat:
if dings not in message.content:
print(message.content)
What it should do: Print every message once that the bot can see if it isnt connected to a bot (That means none of the strings from nichDat is in it or it is not written by a bot.
What it is doing: Printing every message, that was not written by a bot 5 times.
What can i do that it is doing the right stuff?
try replacing
for dings in nichDat:
if dings not in message.content:
print(message.content)
with
if any(dings not in message.content for dings in nichDat):
print(message.content)
Your code is setup as a loop in a way that it checks each of the strings on your nichtDat list individually and if that single string is not in the message it gets printed, so your filter should not be working correctly either.
I just fixed it by making a search function and completely changing how it works.
This is how:
from discord import Embed
import os
import discord
bot=discord.Client()
nichDat=["|","~",".",",","!","pls"]
def searchfor(dings1):
for dings in nichDat:
if dings1.startswith(dings) == False:
break
print(dings1)
#bot.event
async def on_message(message):
if message.content != "NothingButABot":
return
print(message)
await bot.change_presence(activity=discord.Streaming(name='24/7 chatting', url='https://www.youtube.com/watch?v=dQw4w9WgXcQ'))
print("2")
for guild in bot.guilds:
print(guild.name)
for channel in guild.text_channels:
print(channel.name)
if "bot" not in channel.name:
print("Oke")
async for message in channel.history(limit=200):
if not message.author.bot:
searchfor(message.content)

How to add spaces in async function discord.py

I am making a discord chat bot. I want to set command "How are You?" But my program only choose 'How' . I need some suggestions.TIA
import discord
from discord.ext import commands
client = commands.Bot(command_prefix= '=')
#client.event
async def on_ready():
print('Bot is online')
#client.command()
async def hello(ctx):
await ctx.send('hello')
#client.command()
async def how_are_you(ctx):
await ctx.send('I am good')
client.run('token')
If you want to use a command, i don't think you can register one with spaces in it, however there are a couple of workarounds:
First you can use the first word of your string as the command name and then check for the content of the message:
#client.command()
async def how(ctx):
if ctx.message.content.lower() == "=how are you":
await ctx.send("I'm fine thanks")
else:
pass
The = prefix is needed in the message, replace it when you want to change your prefix.
Or, the second option would be to set up an on_message listener.
#client.event
async def on_message(message):
if message.content.lower().startswith("=how are you"):
await message.channel.send("I'm fine thanks.")
await client.process_commands(message)

on.message () takes 0 positional arguments but 1 was given (discord.py bot)

So I have this simple discord bot written in python and when I try some commands, it gives me "on.message () takes 0 positional arguments but 1 was given". Here's the code of the bot.
import discord
import os
import requests
import json
from keep_alive import keep_alive
client = discord.Client ()
#client.event
async def on_ready () :
await client.change_presence(activity=discord.Game('Sivo nebo'))
print ('We have logged in as {0.user}'.format(client))
#client.event
async def on_message () :
if message.author == client.user :
return
if message.content.startswith('!hi') :
await message.channel.send('Hi!')
if message.content.startswith('!goodbye') :
await message.channel.send('Goodbye!')
keep_alive()
client.run(os.getenv('TOKEN'))
All events take an implicit context argument.
async def on_ready(ctx):
...
async def on_message(ctx):
...
Try to use async def on_message(message):
I'm quite sure that your current code won't work because message is undefined.
If you view the discord.py documentation, you'll see that the on_message() event reference implicitly takes on argument: message. So when you define on_message with not parameters, discord.py attempts to pass message, but is not able to.
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hi') :
await message.channel.send('Hi!')
if message.content.startswith('!goodbye') :
await message.channel.send('Goodbye!')
On a side note, I would recommend checking out the discord.ext.commands framework, which allows you to create commands and is better than using message.content.startswith() and variations of that.

Get message author in Discord.py

I am trying to make a fun bot for just me and my friends. I want to have a command that says what the authors username is, with or without the tag. I tried looking up how to do this, but none worked with the way my code is currently set up.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
##gets author.
await message.channel.send('You are', username)
client.run('token')
I hope this makes sense, all of the code i have seen is using the ctx or #client.command
The following works on discord.py v1.3.3
message.channel.send isn't like print, it doesn't accept multiple arguments and create a string from it. Use str.format to create one string and send that back to the channel.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are {}'.format(message.author.name))
client.run('token')
Or you can just:
import discord
from discord import commands
client = commands.Bot(case_insensitive=True, command_prefix='$')
#client.command()
async def whoAmI(ctx):
await ctx.send(f'You are {ctx.message.author}')
If you want to ping the user:
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are', message.author.mention)
client.run('token')

How to edit a message in discord.py

I would like to have my bot edit a message if it detects a keyword, i'm not sure how to edit the message though.
I've looked through the documentation but can't seem to figure it out. I'm using discord.py with python 3.6.
This is the code:
#bot.event
async def on_message(message):
if 'test' in message.content:
await edit(message, "testtest")
This is the error:
File "testthing.py", line 67, in on_message
await edit(message, "test")
NameError: name 'edit' is not defined
I would like the bot to edit a message to "testtest" if the message contains the word test, but i just get an error.
You can use the Message.edit coroutine. The arguments must be passed as keyword arguments content, embed, or delete_after. You may only edit messages that you have sent.
await message.edit(content="newcontent")
Here's a solution that worked for me.
#client.command()
async def test(ctx):
message = await ctx.send("hello")
await asyncio.sleep(1)
await message.edit(content="newcontent")
Did you do this:
from discord import edit
or this:
from discord import *
before using message.edit function?
If you did it, maybe the problem is with your discord.py version.
Try this:
print(discord.__version__)
If you are wanting to update responses in discord.py you have to use:
#tree.command(name = 'foobar', description = 'Send the word foo and update it to say bar')
async def self(interaction: discord.Interaction):
await interaction.response.send_message(f'foo', ephemeral = True)
time.sleep(1)
await interaction.edit_original_response(content=f'bar')
Assign the original message to a variable. Reference the variable with .edit(content='content').
(You need "content=" in there).
#bot.command()
async def test(ctx):
msg = await ctx.send('test')
await msg.edit(content='this message has been edited')
#bot.event
async def on_message(message):
if message.content == 'test':
messages=await message.channel.send("CONTENT")
await asyncio.sleep(INT)
await messages.edit(content="NEW CONTENT")
Please try to add def to your code like this:
#bot.event
async def on_message(message):
if 'test' in message.content:
await edit(message, "edited !")
This is what I did:
#bot.event
async def on_message(message):
if message.content == 'test':
await message.channel.send('Hello World!')
await message.edit(content='testtest')
I don't know if this will work for you, but try and see.

Categories

Resources